Spaces:
Running on T4
Running on T4
Download backend/app/modules/api_audit/controller.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/api_audit/controller.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/api_audit/controller.py
-
curl -L -o controller.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/api_audit/controller.py
2.06 kB
| """Operator-only audit browsing using an explicit server-side account allowlist.""" | |
| from datetime import datetime | |
| from typing import Annotated, Literal | |
| from uuid import UUID | |
| from fastapi import APIRouter, Depends, Query, Request, Response | |
| from app.common.schemas import Page | |
| from app.core.config import get_settings | |
| from app.core.dependencies import CurrentAccount, Db, Paging | |
| from app.core.errors import Forbidden | |
| from app.modules.api_audit.schemas import AuditLogDetail, AuditLogSummary | |
| from app.modules.api_audit.service import AuditLogService | |
| def require_audit_access(request: Request, response: Response, account: CurrentAccount) -> None: | |
| if account.id not in get_settings().api_audit_reader_account_ids: | |
| raise Forbidden("You do not have access to API logs.") | |
| request.state.audit_omit_response = True | |
| response.headers["Cache-Control"] = "no-store" | |
| router = APIRouter(prefix="/v1/admin/api-logs", tags=["API logs"], dependencies=[Depends(require_audit_access)]) | |
| async def list_logs( | |
| db: Db, paging: Paging, | |
| request_id: Annotated[str | None, Query(max_length=128)] = None, | |
| session_id: UUID | None = None, | |
| analysis_id: Annotated[str | None, Query(max_length=256)] = None, | |
| account_id: UUID | None = None, | |
| status_code: Annotated[int | None, Query(ge=100, le=599)] = None, | |
| method: Literal["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"] | None = None, | |
| route: Annotated[str | None, Query(max_length=512)] = None, | |
| since: datetime | None = None, until: datetime | None = None, | |
| ) -> Page[AuditLogSummary]: | |
| return await AuditLogService(db).list(paging, { | |
| "request_id": request_id, "session_id": session_id, "analysis_id": analysis_id, | |
| "account_id": account_id, "status_code": status_code, "method": method, "route": route, | |
| }, since, until) | |
| async def get_log(log_id: UUID, db: Db) -> AuditLogDetail: | |
| return await AuditLogService(db).get(log_id) | |