Source code for codegrade.models.heartbeat_response
"""The module that defines the ``HeartbeatResponse`` model.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import datetime
import typing as t
from dataclasses import dataclass, field
import cg_request_args as rqa
from .. import parsers
from ..utils import to_dict
from .heartbeat_verdict import HeartbeatVerdict
[docs]
@dataclass(kw_only=True)
class HeartbeatResponse:
"""Response returned by the heartbeat endpoint."""
#: What the exam environment said about this heartbeat. Only `verified`
#: refreshed the session; the other two skipped the refresh, so the session
#: lapses at `expires_at` unless a later heartbeat verifies.
verdict: HeartbeatVerdict
#: The session's expires_at after the refresh. When heartbeat enforcement
#: is not active, this is the unchanged current value.
expires_at: datetime.datetime
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: rqa.FixedMapping(
rqa.RequiredArgument(
"verdict",
rqa.EnumValue(HeartbeatVerdict),
doc="What the exam environment said about this heartbeat. Only `verified` refreshed the session; the other two skipped the refresh, so the session lapses at `expires_at` unless a later heartbeat verifies.",
),
rqa.RequiredArgument(
"expires_at",
rqa.RichValue.DateTime,
doc="The session's expires_at after the refresh. When heartbeat enforcement is not active, this is the unchanged current value.",
),
)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"verdict": to_dict(self.verdict),
"expires_at": to_dict(self.expires_at),
}
return res
@classmethod
def from_dict(
cls: t.Type[HeartbeatResponse], d: t.Dict[str, t.Any]
) -> HeartbeatResponse:
parsed = cls.data_parser.try_parse(d)
res = cls(
verdict=parsed.verdict,
expires_at=parsed.expires_at,
)
res.raw_data = d
return res