github-actions[bot]
Automated Deploy: 4a6a58c601a16c2489b51a7644dbeed2347a2eb2
be69d1e
Raw History Blame Contribute Delete
4.14 kB
"""H1: POST /v1/sessions/{session_id}/swing-report (contract 1.0/1.1) and /pitch-report (pitch-1.1).
The body is the ML API's payload, unchanged. Pitch-1.1 returns the pitching report extension.
By default the report is built in the background: the response is 202 with the session status, and the owner gets
a "result" notification when the report is saved (or when the build failed). ?queue=false builds it in the request
and returns the report (200), for a caller that waits on screen.
"""
import uuid
from typing import Annotated, Any
from uuid import UUID
from ai_coach.pitch_report.v1_1.schemas import PitchReport
from fastapi import APIRouter, Body, Depends, Request
from fastapi.responses import JSONResponse
from app.common.schemas import ResponseModel
from app.core.dependencies import CurrentAccount, Db
from app.core.errors import TooLarge
from app.modules.swing_reports import queue as report_queue
from app.modules.swing_reports.schemas import SwingReportOut
from app.modules.swing_reports.service import SwingReportService
router = APIRouter(prefix="/v1", tags=["swing reports"])
pitch_router = APIRouter(prefix="/v1", tags=["pitch reports"])
MAX_PAYLOAD_BYTES = 256 * 1024 # a contract payload is a few kilobytes
class ReportQueued(ResponseModel):
session_id: UUID
status: str # "queued", or "done" when this revision already has its report
QUEUED = {202: {"model": ReportQueued, "description": "Queued (the default): the report is built in the background"}}
def get_swing_report_service(db: Db) -> SwingReportService:
return SwingReportService(db)
async def _respond(db: Db, session_id: UUID, report: dict | None, queue: bool):
if not queue:
return report
if report is None:
await db.commit() # the worker must see the queued payload before it is woken
report_queue.kick()
return JSONResponse(ReportQueued(session_id=session_id, status="queued" if report is None else "done")
.model_dump(mode="json"), status_code=202)
@pitch_router.post("/sessions/{session_id}/pitch-report/retry-coaching", response_model=PitchReport,
response_model_exclude_unset=True)
async def retry_pitch_coaching(
session_id: UUID, request: Request, account: CurrentAccount,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
refresh: bool = False,
) -> dict:
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await service.retry_pitch_coaching(account, session_id, request_id, refresh=refresh)
async def payload_size_ok(request: Request) -> None:
length = request.headers.get("content-length")
if length and length.isdigit() and int(length) > MAX_PAYLOAD_BYTES:
raise TooLarge("The payload is too large.")
@router.post("/sessions/{session_id}/swing-report", response_model=SwingReportOut, responses=QUEUED,
dependencies=[Depends(payload_size_ok)])
async def create_swing_report(
session_id: UUID, request: Request, account: CurrentAccount, db: Db,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
payload: Annotated[Any, Body()], queue: bool = True,
):
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await _respond(db, session_id, await service.create(account, session_id, payload, request_id, queue=queue),
queue)
@pitch_router.post("/sessions/{session_id}/pitch-report", response_model=PitchReport | SwingReportOut,
response_model_exclude_unset=True, responses=QUEUED,
dependencies=[Depends(payload_size_ok)])
async def create_pitch_report(
session_id: UUID, request: Request, account: CurrentAccount, db: Db,
service: Annotated[SwingReportService, Depends(get_swing_report_service)],
payload: Annotated[Any, Body()], queue: bool = True,
):
request_id = getattr(request.state, "request_id", None) or uuid.uuid4().hex
return await _respond(
db, session_id, await service.create_pitch(account, session_id, payload, request_id, queue=queue), queue)