Unify parsing workers, analytics API with PostgreSQL, map UI, and PI distribution into centers/ with Docker Compose. Co-authored-by: Cursor <cursoragent@cursor.com>
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..database import get_db
|
|
from ..schemas import EventRead
|
|
from ..services.filtering import apply_consumer_filter, find_consumer_by_api_key
|
|
|
|
router = APIRouter(prefix="/api/v1", tags=["distribution"])
|
|
|
|
|
|
def get_consumer_from_api_key(
|
|
authorization: str | None = Header(default=None),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
if not authorization or not authorization.startswith("Bearer "):
|
|
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
|
|
|
|
api_key = authorization.removeprefix("Bearer ").strip()
|
|
consumer = find_consumer_by_api_key(db, api_key)
|
|
if not consumer:
|
|
raise HTTPException(status_code=401, detail="Invalid API key")
|
|
return consumer
|
|
|
|
|
|
@router.get("/events", response_model=list[EventRead])
|
|
def list_filtered_events(
|
|
limit: int = Query(default=100, ge=1, le=1000),
|
|
offset: int = Query(default=0, ge=0),
|
|
consumer=Depends(get_consumer_from_api_key),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
events = apply_consumer_filter(db, consumer)
|
|
return events[offset : offset + limit]
|