ovdashboard/api/ovdashboard_api/routers/_common.py

90 lines
2.1 KiB
Python
Raw Normal View History

2022-09-02 13:22:35 +00:00
import re
from dataclasses import dataclass
2022-09-03 15:40:46 +00:00
from typing import Iterator, Protocol
2022-09-02 13:22:35 +00:00
2022-09-02 14:51:11 +00:00
from fastapi import HTTPException, status
2022-09-02 13:22:35 +00:00
from webdav3.exceptions import RemoteResourceNotFound
2022-09-04 22:30:40 +00:00
from ..dav_common import caldav_principal, webdav_list
2022-09-02 13:22:35 +00:00
2022-09-03 15:40:46 +00:00
@dataclass(frozen=True)
class NameLister(Protocol):
def __call__(self) -> Iterator[str]:
...
2022-09-02 13:22:35 +00:00
@dataclass(frozen=True)
class FileNameLister:
remote_path: str
re: re.Pattern[str]
2022-09-03 15:40:46 +00:00
async def __call__(self) -> Iterator[str]:
2022-09-02 13:22:35 +00:00
try:
2022-09-03 15:40:46 +00:00
file_names = await webdav_list(self.remote_path)
2022-09-02 13:22:35 +00:00
return (
name
for name in file_names
if self.re.search(name)
)
except RemoteResourceNotFound:
return iter(())
@dataclass(frozen=True)
class CalendarNameLister:
async def __call__(self) -> Iterator[str]:
return (
cal.name
2022-09-04 14:14:22 +00:00
for cal in caldav_principal().calendars()
)
2022-09-02 13:22:35 +00:00
@dataclass(frozen=True)
2022-09-03 15:40:46 +00:00
class PrefixFinder:
lister: NameLister
2022-09-02 13:22:35 +00:00
2022-09-03 15:40:46 +00:00
async def __call__(self, prefix: str) -> Iterator[str]:
2022-09-02 13:22:35 +00:00
return (
file_name
2022-09-03 15:40:46 +00:00
for file_name in (await self.lister())
2022-09-02 13:22:35 +00:00
if file_name.lower().startswith(prefix.lower())
)
2022-09-02 14:51:11 +00:00
@dataclass(frozen=True)
2022-09-04 14:14:22 +00:00
class PrefixUnique:
2022-09-03 15:40:46 +00:00
finder: PrefixFinder
2022-09-02 14:51:11 +00:00
@property
def responses(self) -> dict:
return {
status.HTTP_200_OK: {
"description": "Operation successful",
},
status.HTTP_404_NOT_FOUND: {
2022-09-04 14:14:22 +00:00
"description": "Prefix not found",
2022-09-02 14:51:11 +00:00
"content": None,
},
status.HTTP_409_CONFLICT: {
2022-09-04 14:14:22 +00:00
"description": "Ambiguous prefix",
2022-09-02 14:51:11 +00:00
"content": None,
},
}
2022-09-04 14:14:22 +00:00
async def __call__(self, prefix: str) -> str:
names = await self.finder(prefix)
2022-09-02 14:51:11 +00:00
2022-09-04 14:14:22 +00:00
try:
name = next(names)
except StopIteration:
2022-09-02 14:51:11 +00:00
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
2022-09-04 14:14:22 +00:00
if any(True for _ in names):
2022-09-02 14:51:11 +00:00
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
2022-09-04 14:14:22 +00:00
return name