diff --git a/api/ovdashboard_api/__init__.py b/api/ovdashboard_api/__init__.py index ed4b250..0647a75 100644 --- a/api/ovdashboard_api/__init__.py +++ b/api/ovdashboard_api/__init__.py @@ -1,9 +1,21 @@ -from webdav3.client import Client +import functools +from typing import Any + +from webdav3.client import Client, Resource from .config import SETTINGS -CLIENT = Client({ +_WEBDAV_CLIENT = Client({ "webdav_hostname": SETTINGS.webdav_url, "webdav_login": SETTINGS.dav_username, "webdav_password": SETTINGS.dav_password, }) + + +@functools.lru_cache +def webdav_resource(remote_path: Any) -> Resource: + return _WEBDAV_CLIENT.resource(remote_path) + + +def webdav_list(remote_path: str) -> list: + return _WEBDAV_CLIENT.list(remote_path) diff --git a/api/ovdashboard_api/dav_file.py b/api/ovdashboard_api/dav_file.py index b44ac7a..ac966ed 100644 --- a/api/ovdashboard_api/dav_file.py +++ b/api/ovdashboard_api/dav_file.py @@ -8,7 +8,7 @@ from typing import Any, Optional from async_lru import alru_cache from webdav3.client import Resource -from . import CLIENT +from . import webdav_resource _logger = logging.getLogger(__name__) @@ -53,7 +53,7 @@ async def _get_buffer( resource.write_to(buffer) return buffer - resource = CLIENT.resource(remote_path) + resource = webdav_resource(remote_path) return await buffer_inner(resource) diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py index 3589147..63f1a54 100644 --- a/api/ovdashboard_api/routers/_common.py +++ b/api/ovdashboard_api/routers/_common.py @@ -5,7 +5,7 @@ from typing import Iterator from fastapi import HTTPException, status from webdav3.exceptions import RemoteResourceNotFound -from .. import CLIENT +from .. import webdav_list from ..dav_file import DavFile @@ -16,7 +16,7 @@ class FileNameLister: def __call__(self) -> Iterator[str]: try: - file_names = CLIENT.list(self.remote_path) + file_names = webdav_list(self.remote_path) return ( name