From 06abdfb95381ec6ad4bb2d7d5ede9a5619d08809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 22 Oct 2023 16:25:19 +0200 Subject: [PATCH] wip: functionify routers._common --- api/ovdashboard_api/routers/v1/_common.py | 47 ++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/api/ovdashboard_api/routers/v1/_common.py b/api/ovdashboard_api/routers/v1/_common.py index 8e277cf..4fa8ece 100644 --- a/api/ovdashboard_api/routers/v1/_common.py +++ b/api/ovdashboard_api/routers/v1/_common.py @@ -7,11 +7,11 @@ from dataclasses import dataclass from logging import getLogger from typing import Iterator, Protocol -from fastapi import HTTPException, status +from fastapi import Depends, HTTPException, status from webdav3.exceptions import RemoteResourceNotFound from ...core.caldav import CalDAV -from ...core.config import get_config +from ...core.config import Config, get_config from ...core.webdav import WebDAV _logger = getLogger(__name__) @@ -74,6 +74,33 @@ class FileNameLister: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) +async def get_remote_path( + path_name: str, + *, + cfg: Config = Depends(get_config), +) -> str: + return getattr(cfg, path_name) + + +async def list_files( + re: re.Pattern[str], + *, + path: str = Depends(get_remote_path), +) -> list[str]: + """ + List files in remote `path` matching the RegEx `re` + """ + try: + return await WebDAV.list_files(path, regex=re) + + except RemoteResourceNotFound: + _logger.error( + "WebDAV path %s lost!", + repr(path), + ) + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + + @dataclass(frozen=True, slots=True) class CalendarNameLister: """ @@ -84,6 +111,13 @@ class CalendarNameLister: return iter(await CalDAV.calendars) +async def list_calendar_names() -> list[str]: + """ + List calendar names + """ + return await CalDAV.calendars + + @dataclass(frozen=True, slots=True) class AggregateNameLister: """ @@ -96,6 +130,15 @@ class AggregateNameLister: return iter(cfg.calendar.aggregates.keys()) +async def list_aggregate_names( + cfg: Config = Depends(get_config), +) -> list[str]: + """ + List aggregate calendar names + """ + return list(cfg.calendar.aggregates.keys()) + + @dataclass(frozen=True, slots=True) class PrefixFinder: """