From cf684ee5f982b1bcd49299644704e21297af89b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 4 Sep 2022 14:14:22 +0000 Subject: [PATCH] PrefixUnique class, unified naming --- api/ovdashboard_api/routers/_common.py | 22 +++++++++++----------- api/ovdashboard_api/routers/calendar.py | 25 +++++++++---------------- api/ovdashboard_api/routers/image.py | 17 +++++++++-------- api/ovdashboard_api/routers/text.py | 18 +++++++++--------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py index 58cc388..b537a5e 100644 --- a/api/ovdashboard_api/routers/_common.py +++ b/api/ovdashboard_api/routers/_common.py @@ -6,7 +6,6 @@ from fastapi import HTTPException, status from webdav3.exceptions import RemoteResourceNotFound from .. import caldav_principal, webdav_list -from ..dav_file import DavFile @dataclass(frozen=True) @@ -56,7 +55,7 @@ class PrefixFinder: @dataclass(frozen=True) -class FilePrefixLoader: +class PrefixUnique: finder: PrefixFinder @property @@ -66,24 +65,25 @@ class FilePrefixLoader: "description": "Operation successful", }, status.HTTP_404_NOT_FOUND: { - "description": "file not found in " + - repr(self.finder.lister.remote_path), + "description": "Prefix not found", "content": None, }, status.HTTP_409_CONFLICT: { - "description": "ambiguous file name for " + - repr(self.finder.lister.remote_path), + "description": "Ambiguous prefix", "content": None, }, } - async def __call__(self, prefix: str) -> DavFile: - file_names = list(await self.finder(prefix)) + async def __call__(self, prefix: str) -> str: + names = await self.finder(prefix) - if not (file_names): + try: + name = next(names) + + except StopIteration: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - elif len(file_names) > 1: + if any(True for _ in names): raise HTTPException(status_code=status.HTTP_409_CONFLICT) - return DavFile(f"{self.finder.lister.remote_path}/{file_names[0]}") + return name diff --git a/api/ovdashboard_api/routers/calendar.py b/api/ovdashboard_api/routers/calendar.py index 60164df..c63e7ec 100644 --- a/api/ovdashboard_api/routers/calendar.py +++ b/api/ovdashboard_api/routers/calendar.py @@ -1,30 +1,31 @@ from datetime import datetime, timedelta from typing import Iterator -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends from pydantic import BaseModel from .. import caldav_principal -from ._common import CalendarNameLister, PrefixFinder +from ._common import CalendarNameLister, PrefixFinder, PrefixUnique router = APIRouter(prefix="/calendar", tags=["calendar"]) _lister = CalendarNameLister() _finder = PrefixFinder(_lister) +_unique = PrefixUnique(_finder) @router.get("/list", response_model=list[str]) async def list_calendars( - calendar_names: Iterator[str] = Depends(_lister), + names: Iterator[str] = Depends(_lister), ) -> list[str]: - return list(calendar_names) + return list(names) @router.get("/find/{prefix}", response_model=list[str]) async def find_calendars( - calendar_names: Iterator[str] = Depends(_finder), + names: Iterator[str] = Depends(_finder), ) -> list[str]: - return list(calendar_names) + return list(names) class CalEvent(BaseModel): @@ -36,17 +37,9 @@ class CalEvent(BaseModel): @router.get("/get/{prefix}", response_model=list[CalEvent]) async def get_calendar( - calendar_names: Iterator[str] = Depends(_finder), + name: Iterator[str] = Depends(_unique), ) -> list[CalEvent]: - calendar_names = list(calendar_names) - - if not (calendar_names): - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - - elif len(calendar_names) > 1: - raise HTTPException(status_code=status.HTTP_409_CONFLICT) - - calendar = caldav_principal().calendar(name=calendar_names[0]) + calendar = caldav_principal().calendar(name=name) return ( CalEvent( diff --git a/api/ovdashboard_api/routers/image.py b/api/ovdashboard_api/routers/image.py index 9dbd568..8a30600 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, FilePrefixLoader, PrefixFinder +from ._common import FileNameLister, PrefixFinder, PrefixUnique router = APIRouter(prefix="/image", tags=["image"]) @@ -20,32 +20,33 @@ _lister = FileNameLister( ) _finder = PrefixFinder(_lister) -_loader = FilePrefixLoader(_finder) +_unique = PrefixUnique(_finder) @router.get("/list", response_model=list[str]) async def list_images( - image_file_names: Iterator[str] = Depends(_lister), + names: Iterator[str] = Depends(_lister), ) -> list[str]: - return list(image_file_names) + return list(names) @router.get("/find/{prefix}", response_model=list[str]) async def find_images( - file_names: Iterator[str] = Depends(_finder), + names: Iterator[str] = Depends(_finder), ) -> list[str]: - return list(file_names) + return list(names) @router.get( "/get/{prefix}", response_class=StreamingResponse, - responses=_loader.responses, + responses=_unique.responses, ) async def get_image( prefix: str, - dav_file: DavFile = Depends(_loader), + name: str = Depends(_unique), ) -> str: + dav_file = DavFile(f"{_lister.remote_path}/{name}") img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB") img_buffer = BytesIO() diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index 7229a6f..0bb5940 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, FilePrefixLoader, PrefixFinder +from ._common import FileNameLister, PrefixFinder, PrefixUnique router = APIRouter(prefix="/text", tags=["text"]) @@ -19,7 +19,7 @@ _lister = FileNameLister( ) _finder = PrefixFinder(_lister) -_loader = FilePrefixLoader(_finder) +_unique = PrefixUnique(_finder) async def get_ticker_lines() -> Iterator[str]: @@ -53,27 +53,27 @@ async def get_ticker_content( @router.get("/list", response_model=list[str]) async def list_texts( - text_file_names: Iterator[str] = Depends(_lister), + names: Iterator[str] = Depends(_lister), ) -> list[str]: - return list(text_file_names) + return list(names) @router.get("/find/{prefix}", response_model=list[str]) async def find_texts( - file_names: Iterator[str] = Depends(_finder), + names: Iterator[str] = Depends(_finder), ) -> list[str]: - return list(file_names) + return list(names) @router.get( "/get/{prefix}", response_model=str, - responses=_loader.responses, + responses=_unique.responses, ) async def get_text( - dav_file: DavFile = Depends(_loader), + name: str = Depends(_unique), ) -> str: - text = await dav_file.string + text = await DavFile(f"{_lister.remote_path}/{name}").string return markdown( text