42 行
1.1 KiB
Python
42 行
1.1 KiB
Python
"""Event stream route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.services.event_stream_service import EventStreamService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated
|
|
|
|
|
|
def get_event_stream(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: EventStreamService | None = None,
|
|
) -> dict:
|
|
event_service = service or EventStreamService()
|
|
params = query or {}
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return event_service.recent_events(
|
|
event_type=_optional_str(params.get("event_type")),
|
|
limit=_int_or_default(params.get("limit"), 50),
|
|
)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _optional_str(value: Any) -> str | None:
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|
|
|
|
|
|
def _int_or_default(value: Any, default: int) -> int:
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return default
|