File: calendar_api.py

package info (click to toggle)
nc-py-api 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,320 kB
  • sloc: python: 12,415; makefile: 238; xml: 100; javascript: 56; sh: 14
file content (41 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download
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