51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
"""Backend API client — sends VLM analysis results to the LockInBro server.
|
|
|
|
POST /distractions/analyze-result with the enriched JSON payload.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import httpx
|
|
|
|
from argus.config import BACKEND_BASE_URL, BACKEND_JWT
|
|
from argus.vlm import VLMResult
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
async def send_analysis(
|
|
result: VLMResult,
|
|
session_id: str,
|
|
*,
|
|
jwt: str | None = None,
|
|
base_url: str | None = None,
|
|
) -> dict:
|
|
"""Post VLM analysis result to the backend.
|
|
|
|
Returns the backend response body on success, or an error dict.
|
|
"""
|
|
url = f"{base_url or BACKEND_BASE_URL}/distractions/analyze-result"
|
|
token = jwt or BACKEND_JWT
|
|
payload = result.to_backend_payload(session_id)
|
|
|
|
headers = {}
|
|
if token:
|
|
headers["Authorization"] = f"Bearer {token}"
|
|
|
|
log.info("Sending analysis to %s on_task=%s", url, result.on_task)
|
|
log.debug("Payload: %s", payload)
|
|
|
|
try:
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
|
resp = await client.post(url, json=payload, headers=headers)
|
|
resp.raise_for_status()
|
|
return resp.json()
|
|
except httpx.HTTPStatusError as exc:
|
|
log.error("Backend returned %s: %s", exc.response.status_code, exc.response.text)
|
|
return {"error": str(exc), "status_code": exc.response.status_code}
|
|
except httpx.RequestError as exc:
|
|
log.error("Backend unreachable: %s", exc)
|
|
return {"error": str(exc)}
|