diff --git a/src/current_status/api.py b/src/current_status/api.py new file mode 100644 index 0000000..3f69a71 --- /dev/null +++ b/src/current_status/api.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +from typing import Any, Literal + +from fastapi import APIRouter, HTTPException +from pydantic import BaseModel + +from .coordinator import CurrentStatusCoordinator + + +class CheckResult(BaseModel): + payload: dict[str, Any] + checked_at: str + check_sequence: int + + +class CurrentStatusResponse(BaseModel): + target_id: str + state: Literal["not_checked", "checked"] + result: CheckResult | None + + +def build_current_status_router(coordinator: CurrentStatusCoordinator) -> APIRouter: + router = APIRouter() + + @router.get( + "/targets/{target_id}/status", + response_model=CurrentStatusResponse, + responses={404: {"description": "Target does not exist"}}, + ) + async def get_current_status(target_id: str) -> dict[str, Any]: + exists, result = await coordinator.read(target_id) + if not exists: + raise HTTPException( + status_code=404, + detail={"code": "target_not_found", "target_id": target_id}, + ) + return { + "target_id": target_id, + "state": "checked" if result is not None else "not_checked", + "result": result, + } + + return router