ovdashboard/api/ovdashboard_api/routers/v1/aggregate.py

66 lines
1.7 KiB
Python
Raw Normal View History

2022-09-06 22:42:28 +00:00
"""
2022-09-08 23:39:42 +00:00
Router "aggregate" provides:
2022-09-06 22:42:28 +00:00
- listing aggregate calendars
- finding aggregate calendars by name prefix
- getting aggregate calendar events by name prefix
"""
2022-09-08 14:07:33 +00:00
from logging import getLogger
2022-09-06 22:42:28 +00:00
from typing import Iterator
from fastapi import APIRouter, Depends
2023-10-17 12:55:38 +00:00
2022-09-06 22:42:28 +00:00
from ovdashboard_api.config import Config
2022-09-17 19:41:41 +00:00
from ...dav_calendar import CalEvent, DavCalendar
2022-09-08 23:39:42 +00:00
from ._common import AggregateNameLister, PrefixFinder, PrefixUnique
2022-09-06 23:50:42 +00:00
from .calendar import calendar_unique
2022-09-06 22:42:28 +00:00
2022-09-08 14:07:33 +00:00
_logger = getLogger(__name__)
2022-09-06 22:42:28 +00:00
router = APIRouter(prefix="/aggregate", tags=["calendar"])
2022-09-08 23:39:42 +00:00
aggregate_lister = AggregateNameLister()
aggregate_finder = PrefixFinder(aggregate_lister)
aggregate_unique = PrefixUnique(aggregate_finder)
2022-09-06 22:42:28 +00:00
2022-09-08 14:07:33 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
2022-09-06 22:42:28 +00:00
@router.get("/list", response_model=list[str])
async def list_aggregate_calendars(
2022-09-08 23:39:42 +00:00
names: Iterator[str] = Depends(aggregate_lister),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_aggregate_calendars(
2022-09-08 23:39:42 +00:00
names: Iterator[str] = Depends(aggregate_finder),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
@router.get("/get/{prefix}", response_model=list[CalEvent])
async def get_aggregate_calendar(
2022-09-08 23:39:42 +00:00
name: str = Depends(aggregate_unique),
2022-09-06 22:42:28 +00:00
) -> list[CalEvent]:
cfg = await Config.get()
2022-09-09 14:12:36 +00:00
aggregate = cfg.calendar.aggregates[name]
2022-09-06 22:42:28 +00:00
2022-09-06 23:50:42 +00:00
calendars = (
2023-10-17 12:55:38 +00:00
DavCalendar(await calendar_unique(cal_prefix)) for cal_prefix in aggregate
2022-09-06 23:50:42 +00:00
)
2023-10-17 12:55:38 +00:00
return sorted(
[
event
async for calendar in calendars # type: ignore
for event in (await calendar.events)
]
)