""" Router "aggregate" provides: - listing aggregate calendars - finding aggregate calendars by name prefix - getting aggregate calendar events by name prefix """ from logging import getLogger from typing import Iterator from fastapi import APIRouter, Depends from ...core.caldav import CalDAV from ...core.calevent import CalEvent from ...core.config import Config, get_config from ._common import LM_AGGREGATES, LM_CALENDARS _logger = getLogger(__name__) router = APIRouter(prefix="/aggregate", tags=["calendar"]) @router.on_event("startup") async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") @router.get( "/list", responses=LM_AGGREGATES.lister.responses, ) async def list_aggregate_calendars( names: Iterator[str] = Depends(LM_AGGREGATES.lister.func), ) -> list[str]: return list(names) @router.get( "/find/{prefix}", responses=LM_AGGREGATES.filter.responses, ) async def find_aggregate_calendars( names: Iterator[str] = Depends(LM_AGGREGATES.filter.func), ) -> list[str]: return list(names) @router.get( "/get/{prefix}", responses=LM_AGGREGATES.getter.responses, ) async def get_aggregate_calendar( cfg: Config = Depends(get_config), name: str = Depends(LM_AGGREGATES.getter.func), ) -> list[CalEvent]: events: list[CalEvent] = [] for cal_prefix in cfg.calendar.aggregates[name]: cal_name = await LM_CALENDARS.getter.func(cal_prefix) events.extend(CalDAV.get_events(cal_name, cfg)) return sorted(events)