decomposer: generate files for Maintain only the latest check result for each monitored target in concurrency-safe memory and expose it through the specified FastAPI current-status endpoint.
This commit is contained in:
44
src/current_status/api.py
Normal file
44
src/current_status/api.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user