from __future__ import annotations import time from api.contracts.captcha_schemas import CaptchaActionResponse, CaptchaStatusResponse from api.repositories.task_store import TaskRecord class CaptchaMapper: @staticmethod def to_status(rec: TaskRecord) -> CaptchaStatusResponse: remaining: float | None = None if rec.captcha_deadline is not None: remaining = max(0.0, rec.captcha_deadline - time.time()) return CaptchaStatusResponse( task_id=rec.task_id, state=rec.captcha_state, captcha_kind=rec.captcha_kind, reason=rec.captcha_reason, browser_view_url=rec.captcha_view_url, notified_at=rec.captcha_notified_at, solved_at=rec.captcha_solved_at, deadline=rec.captcha_deadline, extra_seconds=rec.captcha_extra_seconds, remaining_seconds=remaining, ) @staticmethod def to_action(rec: TaskRecord, message: str | None = None, accepted: bool = True) -> CaptchaActionResponse: return CaptchaActionResponse( task_id=rec.task_id, state=rec.captcha_state, accepted=accepted, message=message, )