Skip to content

Commit 703ad6b

Browse files
feat(jmap): add AsyncJMAPClient mirroring public methods as coroutines
1 parent 02a88f1 commit 703ad6b

5 files changed

Lines changed: 1112 additions & 65 deletions

File tree

caldav/jmap/__init__.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
JMAP calendar support for python-caldav.
33
4-
Provides a synchronous JMAP client with the same public API as the
5-
CalDAV client, so user code works regardless of server protocol.
4+
Provides synchronous and asynchronous JMAP clients with the same public API as
5+
the CalDAV client, so user code works regardless of server protocol.
66
77
Basic usage::
88
@@ -14,8 +14,20 @@
1414
password="secret",
1515
)
1616
calendars = client.get_calendars()
17+
18+
Async usage::
19+
20+
from caldav.jmap import get_async_jmap_client
21+
22+
async with get_async_jmap_client(
23+
url="https://jmap.example.com/.well-known/jmap",
24+
username="alice",
25+
password="secret",
26+
) as client:
27+
calendars = await client.get_calendars()
1728
"""
1829

30+
from caldav.jmap.async_client import AsyncJMAPClient
1931
from caldav.jmap.client import JMAPClient
2032
from caldav.jmap.error import (
2133
JMAPAuthError,
@@ -24,6 +36,8 @@
2436
JMAPMethodError,
2537
)
2638

39+
_JMAP_KEYS = {"url", "username", "password", "auth", "auth_type", "timeout"}
40+
2741

2842
def get_jmap_client(**kwargs) -> JMAPClient | None:
2943
"""Create a :class:`JMAPClient` from configuration.
@@ -47,17 +61,36 @@ def get_jmap_client(**kwargs) -> JMAPClient | None:
4761
conn_params = get_connection_params(**kwargs)
4862
if conn_params is None:
4963
return None
64+
return JMAPClient(**{k: v for k, v in conn_params.items() if k in _JMAP_KEYS})
65+
66+
67+
def get_async_jmap_client(**kwargs) -> AsyncJMAPClient | None:
68+
"""Create an :class:`AsyncJMAPClient` from configuration.
5069
51-
# Strip CalDAV-only keys that JMAPClient does not accept.
52-
_JMAP_KEYS = {"url", "username", "password", "auth", "auth_type", "timeout"}
53-
jmap_params = {k: v for k, v in conn_params.items() if k in _JMAP_KEYS}
70+
Accepts the same arguments and reads configuration from the same sources
71+
as :func:`get_jmap_client`. Returns ``None`` if no configuration is found.
5472
55-
return JMAPClient(**jmap_params)
73+
Example::
74+
75+
async with get_async_jmap_client(
76+
url="https://jmap.example.com/.well-known/jmap",
77+
username="alice", password="secret"
78+
) as client:
79+
calendars = await client.get_calendars()
80+
"""
81+
from caldav.config import get_connection_params
82+
83+
conn_params = get_connection_params(**kwargs)
84+
if conn_params is None:
85+
return None
86+
return AsyncJMAPClient(**{k: v for k, v in conn_params.items() if k in _JMAP_KEYS})
5687

5788

5889
__all__ = [
5990
"JMAPClient",
91+
"AsyncJMAPClient",
6092
"get_jmap_client",
93+
"get_async_jmap_client",
6194
"JMAPError",
6295
"JMAPCapabilityError",
6396
"JMAPAuthError",

0 commit comments

Comments
 (0)