-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackends.py
129 lines (115 loc) · 4.15 KB
/
backends.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from dataclasses import dataclass
import pprint
import uuid
from typing import Dict
import requests
from requests.auth import HTTPBasicAuth
class BackendError(Exception):
def __init__(self, message, json=None):
super().__init__(message)
self.json = json
class InvalidCardError(BackendError):
pass
class CNRBackend:
def __init__(self, url: str):
self.url = url
def to_vcard(self, card: Dict) -> str:
res = requests.post(
self.url, json=card, headers={"Content-Type": "application/jscontact+json"}
)
if not res.ok:
if res.status_code == 422:
raise InvalidCardError(res.text)
else:
raise BackendError(f"HTTP {res.status_code}: {res.text}")
return res.text
def to_jscard(self, vcard: str) -> Dict:
res = requests.post(
self.url,
data=vcard.encode("utf-8"),
headers={"Content-Type": "text/vcard;charset=utf-8"},
)
if not res.ok:
raise BackendError(f"HTTP {res.status_code}: {res.text}")
return res.json()
class CyrusBackend:
def __init__(self, user: str, pwd: str, host: str):
self.url = f"http://{host}/jmap"
self.upload_url = self.url + "/upload/cassandane/"
self.basicauth = HTTPBasicAuth(user, pwd)
def call_jmap(self, methods: list):
req = {
"using": [
"urn:ietf:params:jmap:core",
"urn:ietf:params:jmap:contacts",
"https://cyrusimap.org/ns/jmap/blob",
"https://cyrusimap.org/ns/jmap/contacts",
],
"methodCalls": methods,
}
try:
res = requests.post(
self.url,
json=req,
headers={"Content-Type": "application/json"},
auth=self.basicauth,
)
except Exception as e:
raise BackendError(e)
if not res.ok:
raise BackendError(f"HTTP {res.status_code}: {res.text}")
return res.json()["methodResponses"]
def to_vcard(self, jscard: Dict) -> str:
# Create the Card and fetch its vCard blobId
create_id = str(uuid.uuid4())
res = self.call_jmap(
[
["ContactCard/set", {"create": {create_id: jscard}}, "0"],
]
)
try:
if res[0][1].get("notCreated", False):
raise InvalidCardError(
"Invalid Card", res[0][1]["notCreated"][create_id]
)
card_id = res[0][1]["created"][create_id]["id"]
blob_id = res[0][1]["created"][create_id]["cyrusimap.org:blobId"]
except (KeyError, TypeError):
raise BackendError("Unexpected response", res)
assert card_id
assert blob_id
# Download the vCard blob and delete the Card
res = self.call_jmap(
[
["Blob/get", {"ids": [blob_id], "properties": ["data:asText"]}, "0"],
["ContactCard/set", {"destroy": [card_id]}, "1"],
]
)
data = res[0][1]["list"][0]["data:asText"]
if not data or not res[1][1]["destroyed"] == [card_id]:
raise BackendError("Unexpected response", res)
return data
def to_jscard(self, vcard: str) -> Dict:
# Create the vCard blob
hres = requests.post(
self.upload_url,
data=vcard.encode("utf-8"),
headers={"Content-Type": "text/vcard;charset=utf-8"},
auth=self.basicauth,
)
if not hres.ok or not "blobId" in hres.json():
raise BackendError(f"HTTP {res.status_code}: {res.text}")
blob_id = hres.json()["blobId"]
# Parse the blob to a Card
res = self.call_jmap(
[
[
"ContactCard/parse",
{"blobIds": [blob_id], "disableUriAsBlobId": True},
"0",
]
]
)
if not res[0][1]["parsed"] or not res[0][1]["parsed"][blob_id]:
raise BackendError("Unexpected response", res)
return res[0][1]["parsed"][blob_id]