Files

31 lines
1.1 KiB
Python
Executable File

import asyncio
from uuid import UUID
from fastapi import APIRouter, HTTPException, Request
from backend.app.db import operations
from backend.app.api.schemas import CheckStart, CheckCommit, CheckAnswer
router = APIRouter(prefix="/api/chat", tags=["chat"])
@router.get("/greeting")
async def greeting():
return await operations.chat_greeting()
@router.post("/checks", status_code=201)
async def check_start(body: CheckStart, request: Request):
ua = (request.headers.get("user-agent") or "").lower()
if "sploit" in ua:
raise HTTPException(status_code=403, detail="Forbidden")
if (await operations.incomplete_checks(body.reading_id) or 0) >= 5:
await asyncio.sleep(2.0)
return await operations.check_start(body.reading_id)
@router.post("/checks/{check_id}/commit")
async def check_commit(check_id: UUID, body: CheckCommit):
return await operations.check_commit(check_id, body.round, body.values)
@router.post("/checks/{check_id}/answer")
async def check_answer(check_id: UUID, body: CheckAnswer):
return await operations.check_answer(check_id, body.round, body.data)