Source code for codegrade._api.exam_calendar
"""The endpoints for exam_calendar objects.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import os
import typing as t
import cg_request_args as rqa
from cg_maybe import Maybe, Nothing
from cg_maybe.utils import maybe_from_nullable
from .. import paginated, parsers, utils
if t.TYPE_CHECKING or os.getenv("CG_EAGERIMPORT", False):
from .. import client
from ..models.exam_calendar_entry import ExamCalendarEntry
_ClientT = t.TypeVar("_ClientT", bound="client._BaseClient")
[docs]
class ExamCalendarService(t.Generic[_ClientT]):
__slots__ = ("__client",)
def __init__(self, client: _ClientT) -> None:
self.__client = client
[docs]
def get_all(
self: ExamCalendarService[client.AuthenticatedClient],
*,
page_size: int = 20,
) -> paginated.Response[ExamCalendarEntry]:
"""Get the exams that are scheduled, or have just finished.
The exams returned are the ones of the courses the caller may see, so a
global admin gets every tenant, a tenant admin their own tenant, and
everybody else the courses they are enrolled in.
:param page_size: The size of a single page, maximum is 50.
:returns: The exams that have not ended more than a day ago, the ones
starting first at the front.
"""
url = "/api/v1/exam_calendar/"
params: t.Dict[str, str | int | bool] = {
"page-size": page_size,
}
if t.TYPE_CHECKING:
import httpx
def do_request(next_token: str | None) -> httpx.Response:
if next_token is None:
params.pop("next-token", "")
else:
params["next-token"] = next_token
with self.__client as client:
resp = client.http.get(url=url, params=params)
utils.log_warnings(resp)
return resp
def parse_response(
resp: httpx.Response,
) -> t.Sequence[ExamCalendarEntry]:
if utils.response_code_matches(resp.status_code, 200):
from ..models.exam_calendar_entry import ExamCalendarEntry
return parsers.JsonResponseParser(
rqa.List(parsers.ParserFor.make(ExamCalendarEntry))
).try_parse(resp)
from ..models.any_error import AnyErrorParser
raise utils.get_error(
resp, (((400, 409, 401, 403, 404, 429, 500), AnyErrorParser),)
)
return paginated.Response(do_request, parse_response)