1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
"""Nextcloud Calendar DAV API."""
from ._session import NcSessionBasic
try:
from caldav.davclient import DAVClient, DAVResponse
class _CalendarAPI(DAVClient):
"""Class that encapsulates ``caldav.DAVClient`` to work with the Nextcloud calendar."""
def __init__(self, session: NcSessionBasic):
self._session = session
super().__init__(session.cfg.dav_endpoint)
@property
def available(self) -> bool:
"""Returns True if ``caldav`` package is avalaible, False otherwise."""
return True
def request(self, url, method="GET", body="", headers={}): # noqa pylint: disable=dangerous-default-value
if isinstance(body, str):
body = body.encode("UTF-8")
if body:
body = body.replace(b"\n", b"\r\n").replace(b"\r\r\n", b"\r\n")
r = self._session.adapter_dav.request(
method, url if isinstance(url, str) else str(url), content=body, headers=headers
)
return DAVResponse(r)
except ImportError:
class _CalendarAPI: # type: ignore
"""A stub class in case **caldav** is missing."""
def __init__(self, session: NcSessionBasic):
self._session = session
@property
def available(self) -> bool:
"""Returns True if ``caldav`` package is avalaible, False otherwise."""
return False
|