ovdashboard/api/ovdashboard_api/dav_common.py

70 lines
1.6 KiB
Python
Raw Normal View History

2022-09-05 12:54:02 +00:00
"""
Definition of WebDAV and CalDAV clients.
"""
2022-09-04 22:30:40 +00:00
from functools import lru_cache
from typing import Any, Iterator
2022-09-04 22:30:40 +00:00
2022-09-04 22:41:46 +00:00
from caldav import DAVClient as CalDAVclient
from caldav import Principal as CalDAVPrincipal
from webdav3.client import Client as WebDAVclient
from webdav3.client import Resource as WebDAVResource
2022-09-04 22:30:40 +00:00
from .async_helpers import run_in_executor
from .settings import SETTINGS
2022-09-04 22:41:46 +00:00
_WEBDAV_CLIENT = WebDAVclient({
2022-09-05 14:39:34 +00:00
"webdav_hostname": SETTINGS.webdav.url,
"webdav_login": SETTINGS.webdav.username,
"webdav_password": SETTINGS.webdav.password,
2022-09-04 22:30:40 +00:00
})
2022-09-06 07:54:44 +00:00
@lru_cache(maxsize=SETTINGS.cache_size)
2022-09-04 22:41:46 +00:00
def webdav_resource(remote_path: Any) -> WebDAVResource:
2022-09-05 12:54:02 +00:00
"""
Gets a resource using the main WebDAV client.
"""
2022-09-05 20:17:27 +00:00
return _WEBDAV_CLIENT.resource(
f"{SETTINGS.webdav_prefix}/{remote_path}"
)
2022-09-04 22:30:40 +00:00
@run_in_executor
2022-09-05 12:54:02 +00:00
def webdav_list(remote_path: str) -> list[str]:
"""
Asynchroneously lists a WebDAV path using the main WebDAV client.
"""
2022-09-05 20:17:27 +00:00
return _WEBDAV_CLIENT.list(
f"{SETTINGS.webdav_prefix}/{remote_path}"
)
2022-09-04 22:30:40 +00:00
2022-09-04 22:41:46 +00:00
_CALDAV_CLIENT = CalDAVclient(
2022-09-05 14:39:34 +00:00
url=SETTINGS.caldav.url,
username=SETTINGS.caldav.username,
password=SETTINGS.caldav.password,
2022-09-04 22:30:40 +00:00
)
2022-09-04 22:41:46 +00:00
def caldav_principal() -> CalDAVPrincipal:
2022-09-05 12:54:02 +00:00
"""
Gets the `Principal` object of the main CalDAV client.
"""
2022-09-04 22:30:40 +00:00
return _CALDAV_CLIENT.principal()
@run_in_executor
def caldav_list() -> Iterator[str]:
"""
Asynchroneously lists all calendars using the main WebDAV client.
"""
return (
cal.name
for cal in caldav_principal().calendars()
)