works again, needs some work in routers._common

This commit is contained in:
Jörn-Michael Miehe 2022-09-04 14:14:22 +00:00
parent 5034115281
commit 513a577914
5 changed files with 25 additions and 27 deletions

View file

@ -48,5 +48,6 @@ _CALDAV_CLIENT = caldav.DAVClient(
) )
@run_in_executor
def caldav_principal() -> caldav.Principal: def caldav_principal() -> caldav.Principal:
return _CALDAV_CLIENT.principal() return _CALDAV_CLIENT.principal()

View file

@ -5,7 +5,7 @@ from typing import Iterator, Protocol
from fastapi import HTTPException, status from fastapi import HTTPException, status
from webdav3.exceptions import RemoteResourceNotFound from webdav3.exceptions import RemoteResourceNotFound
from .. import webdav_list from .. import caldav_principal, webdav_list
from ..dav_file import DavFile from ..dav_file import DavFile
@ -34,6 +34,17 @@ class FileNameLister:
return iter(()) 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) @dataclass(frozen=True)
class PrefixFinder: class PrefixFinder:
lister: NameLister lister: NameLister

View file

@ -4,46 +4,31 @@ from typing import Iterator
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from .. import caldav_principal from .. import caldav_principal
from ._common import CalendarNameLister, PrefixFinder
router = APIRouter(prefix="/calendar", tags=["calendar"]) router = APIRouter(prefix="/calendar", tags=["calendar"])
_lister = CalendarNameLister()
async def get_calendar_names() -> Iterator[str]: _finder = PrefixFinder(_lister)
principal = caldav_principal()
return (
cal.name
for cal in principal.calendars()
)
@router.get("/list", response_model=list[str]) @router.get("/list", response_model=list[str])
async def list_calendars( async def list_calendars(
calendar_names: Iterator[str] = Depends(get_calendar_names), calendar_names: Iterator[str] = Depends(_lister),
) -> list[str]: ) -> list[str]:
return list(calendar_names) 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]) @router.get("/find/{prefix}", response_model=list[str])
async def find_calendars( async def find_calendars(
calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), calendar_names: Iterator[str] = Depends(_finder),
) -> list[str]: ) -> list[str]:
return list(calendar_names) return list(calendar_names)
@router.get("/get/{prefix}", response_model=list[str]) @router.get("/get/{prefix}", response_model=list[str])
async def get_calendar( async def get_calendar(
calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), calendar_names: Iterator[str] = Depends(_finder),
) -> list[str]: ) -> list[str]:
calendar_names = list(calendar_names) calendar_names = list(calendar_names)
@ -53,7 +38,8 @@ async def get_calendar(
elif len(calendar_names) > 1: elif len(calendar_names) > 1:
raise HTTPException(status_code=status.HTTP_409_CONFLICT) 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( events = calendar.date_search(
start=datetime.now(), start=datetime.now(),

View file

@ -7,7 +7,7 @@ from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ..dav_file import DavFile from ..dav_file import DavFile
from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
router = APIRouter(prefix="/image", tags=["image"]) router = APIRouter(prefix="/image", tags=["image"])
@ -19,7 +19,7 @@ _lister = FileNameLister(
), ),
) )
_finder = FilePrefixFinder(_lister) _finder = PrefixFinder(_lister)
_loader = FilePrefixLoader(_finder) _loader = FilePrefixLoader(_finder)

View file

@ -6,7 +6,7 @@ from markdown import markdown
from ..config import SETTINGS from ..config import SETTINGS
from ..dav_file import DavFile from ..dav_file import DavFile
from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
router = APIRouter(prefix="/text", tags=["text"]) router = APIRouter(prefix="/text", tags=["text"])
@ -18,7 +18,7 @@ _lister = FileNameLister(
), ),
) )
_finder = FilePrefixFinder(_lister) _finder = PrefixFinder(_lister)
_loader = FilePrefixLoader(_finder) _loader = FilePrefixLoader(_finder)