diff --git a/api/ovdashboard_api/__init__.py b/api/ovdashboard_api/__init__.py index 4fa6c6e..edeb9eb 100644 --- a/api/ovdashboard_api/__init__.py +++ b/api/ovdashboard_api/__init__.py @@ -48,5 +48,6 @@ _CALDAV_CLIENT = caldav.DAVClient( ) +@run_in_executor def caldav_principal() -> caldav.Principal: return _CALDAV_CLIENT.principal() diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py index 9c36bd1..b6f47f7 100644 --- a/api/ovdashboard_api/routers/_common.py +++ b/api/ovdashboard_api/routers/_common.py @@ -5,7 +5,7 @@ from typing import Iterator, Protocol from fastapi import HTTPException, status from webdav3.exceptions import RemoteResourceNotFound -from .. import webdav_list +from .. import caldav_principal, webdav_list from ..dav_file import DavFile @@ -34,6 +34,17 @@ class FileNameLister: return iter(()) +@dataclass(frozen=True) +class CalendarNameLister: + async def __call__(self) -> Iterator[str]: + principal = await caldav_principal() + + return ( + cal.name + for cal in principal.calendars() + ) + + @dataclass(frozen=True) class PrefixFinder: lister: NameLister diff --git a/api/ovdashboard_api/routers/calendar.py b/api/ovdashboard_api/routers/calendar.py index 00e545a..85493cb 100644 --- a/api/ovdashboard_api/routers/calendar.py +++ b/api/ovdashboard_api/routers/calendar.py @@ -4,46 +4,31 @@ from typing import Iterator from fastapi import APIRouter, Depends, HTTPException, status from .. import caldav_principal +from ._common import CalendarNameLister, PrefixFinder router = APIRouter(prefix="/calendar", tags=["calendar"]) - -async def get_calendar_names() -> Iterator[str]: - principal = caldav_principal() - return ( - cal.name - for cal in principal.calendars() - ) +_lister = CalendarNameLister() +_finder = PrefixFinder(_lister) @router.get("/list", response_model=list[str]) async def list_calendars( - calendar_names: Iterator[str] = Depends(get_calendar_names), + calendar_names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(calendar_names) -async def find_calendars_by_prefix( - prefix: str, - calendar_names: Iterator[str] = Depends(get_calendar_names), -) -> Iterator[str]: - return ( - name - for name in calendar_names - if name.lower().startswith(prefix.lower()) - ) - - @router.get("/find/{prefix}", response_model=list[str]) async def find_calendars( - calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), + calendar_names: Iterator[str] = Depends(_finder), ) -> list[str]: return list(calendar_names) @router.get("/get/{prefix}", response_model=list[str]) async def get_calendar( - calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), + calendar_names: Iterator[str] = Depends(_finder), ) -> list[str]: calendar_names = list(calendar_names) @@ -53,7 +38,8 @@ async def get_calendar( elif len(calendar_names) > 1: raise HTTPException(status_code=status.HTTP_409_CONFLICT) - calendar = caldav_principal().calendar(name=calendar_names[0]) + principal = await caldav_principal() + calendar = principal.calendar(name=calendar_names[0]) events = calendar.date_search( start=datetime.now(), diff --git a/api/ovdashboard_api/routers/image.py b/api/ovdashboard_api/routers/image.py index f6c7283..9dbd568 100644 --- a/api/ovdashboard_api/routers/image.py +++ b/api/ovdashboard_api/routers/image.py @@ -7,7 +7,7 @@ from fastapi.responses import StreamingResponse from PIL import Image from ..dav_file import DavFile -from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader +from ._common import FileNameLister, FilePrefixLoader, PrefixFinder router = APIRouter(prefix="/image", tags=["image"]) @@ -19,7 +19,7 @@ _lister = FileNameLister( ), ) -_finder = FilePrefixFinder(_lister) +_finder = PrefixFinder(_lister) _loader = FilePrefixLoader(_finder) diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index ad5f45d..7229a6f 100644 --- a/api/ovdashboard_api/routers/text.py +++ b/api/ovdashboard_api/routers/text.py @@ -6,7 +6,7 @@ from markdown import markdown from ..config import SETTINGS from ..dav_file import DavFile -from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader +from ._common import FileNameLister, FilePrefixLoader, PrefixFinder router = APIRouter(prefix="/text", tags=["text"]) @@ -18,7 +18,7 @@ _lister = FileNameLister( ), ) -_finder = FilePrefixFinder(_lister) +_finder = PrefixFinder(_lister) _loader = FilePrefixLoader(_finder)