From d8ca1da9cb60a212d2ecef4e898e79fad3538f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 5 Sep 2022 00:23:00 +0000 Subject: [PATCH] HTTP 404 if directory doesn't exist --- api/ovdashboard_api/routers/_common.py | 34 +++++++++++++++++++++++--- api/ovdashboard_api/routers/image.py | 12 +++++++-- api/ovdashboard_api/routers/text.py | 12 +++++++-- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py index ccbc6c2..2ea5373 100644 --- a/api/ovdashboard_api/routers/_common.py +++ b/api/ovdashboard_api/routers/_common.py @@ -14,11 +14,28 @@ class NameLister(Protocol): ... +_RESPONSE_OK = { + status.HTTP_200_OK: { + "description": "Operation successful", + }, +} + + @dataclass(frozen=True) class FileNameLister: remote_path: str re: re.Pattern[str] + @property + def responses(self) -> dict: + return { + **_RESPONSE_OK, + status.HTTP_404_NOT_FOUND: { + "description": f"{self.remote_path!r} not found", + "content": None, + }, + } + async def __call__(self) -> Iterator[str]: try: file_names = await webdav_list(self.remote_path) @@ -30,7 +47,7 @@ class FileNameLister: ) except RemoteResourceNotFound: - return iter(()) + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) @dataclass(frozen=True) @@ -46,6 +63,17 @@ class CalendarNameLister: class PrefixFinder: lister: NameLister + @property + def responses(self) -> dict: + return { + **_RESPONSE_OK, + status.HTTP_404_NOT_FOUND: { + "description": "Failure in lister " + + repr(self.lister.__class__.__name__), + "content": None, + }, + } + async def __call__(self, prefix: str) -> Iterator[str]: return ( file_name @@ -61,9 +89,7 @@ class PrefixUnique: @property def responses(self) -> dict: return { - status.HTTP_200_OK: { - "description": "Operation successful", - }, + **_RESPONSE_OK, status.HTTP_404_NOT_FOUND: { "description": "Prefix not found", "content": None, diff --git a/api/ovdashboard_api/routers/image.py b/api/ovdashboard_api/routers/image.py index 8a30600..ec8fe62 100644 --- a/api/ovdashboard_api/routers/image.py +++ b/api/ovdashboard_api/routers/image.py @@ -23,14 +23,22 @@ _finder = PrefixFinder(_lister) _unique = PrefixUnique(_finder) -@router.get("/list", response_model=list[str]) +@router.get( + "/list", + response_model=list[str], + responses=_lister.responses, +) async def list_images( names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(names) -@router.get("/find/{prefix}", response_model=list[str]) +@router.get( + "/find/{prefix}", + response_model=list[str], + responses=_finder.responses, +) async def find_images( names: Iterator[str] = Depends(_finder), ) -> list[str]: diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index 683a250..168eb2a 100644 --- a/api/ovdashboard_api/routers/text.py +++ b/api/ovdashboard_api/routers/text.py @@ -55,14 +55,22 @@ async def get_ticker_content( ) -@router.get("/list", response_model=list[str]) +@router.get( + "/list", + response_model=list[str], + responses=_lister.responses, +) async def list_texts( names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(names) -@router.get("/find/{prefix}", response_model=list[str]) +@router.get( + "/find/{prefix}", + response_model=list[str], + responses=_finder.responses, +) async def find_texts( names: Iterator[str] = Depends(_finder), ) -> list[str]: