69a232d08a
- FastAPI: POST /api/v1/events, GET /health, JSON Schema validation - PostgreSQL models, Alembic migration, bootstrap API key - deploy/docker-compose.yml, .env.example - docs/install-ubuntu-24.04.md, updated work-plan (agents first)
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from typing import Any
|
|
|
|
from fastapi import APIRouter, Body, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth.api_key import get_api_key_auth
|
|
from app.config import get_settings
|
|
from app.database import get_db
|
|
from app.services.ingest import ingest_event
|
|
from app.services.schema_validate import validate_event_payload
|
|
|
|
router = APIRouter(prefix="/events", tags=["events"])
|
|
|
|
|
|
class IngestResponse(BaseModel):
|
|
status: str
|
|
event_id: str
|
|
created: bool
|
|
sac_event_url: str
|
|
|
|
|
|
@router.post("", status_code=202, response_model=IngestResponse)
|
|
def post_event(
|
|
payload: dict[str, Any] = Body(...),
|
|
db: Session = Depends(get_db),
|
|
_api_key: str = Depends(get_api_key_auth),
|
|
) -> IngestResponse:
|
|
errors = validate_event_payload(payload)
|
|
if errors:
|
|
raise HTTPException(status_code=422, detail={"schema_errors": errors[:20]})
|
|
|
|
event, created = ingest_event(db, payload)
|
|
db.commit()
|
|
|
|
settings = get_settings()
|
|
base = settings.sac_public_url.rstrip("/")
|
|
return IngestResponse(
|
|
status="accepted",
|
|
event_id=event.event_id,
|
|
created=created,
|
|
sac_event_url=f"{base}/api/v1/events/{event.id}",
|
|
)
|