forked from nylas/nylas-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
251 lines (191 loc) · 8.02 KB
/
client.py
File metadata and controls
251 lines (191 loc) · 8.02 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import requests
import json
from collections import namedtuple
from base64 import b64encode
API_BASE = "https://api.inboxapp.com/n/"
class InboxAPIObject(dict):
attrs = []
def __init__(self):
pass
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
@classmethod
def from_dict(cls, dct):
obj = cls()
for attr in cls.attrs:
if attr in dct:
obj[attr] = dct[attr]
return obj
class Message(InboxAPIObject):
attrs = ["bcc", "body", "date", "files", "from", "id", "namespace",
"object", "subject", "thread", "to", "unread"]
class Tag(InboxAPIObject):
attrs = ["id", "name", "namespace", "object"]
class Thread(InboxAPIObject):
attrs = ["drafts", "id", "messages", "namespace", "object", "participants",
"snippet", "subject", "subject_date", "tags"]
class Draft(InboxAPIObject):
attrs = Message.attrs + ["state"]
class File(InboxAPIObject):
attrs = ["content_type", "filename", "id", "is_embedded", "message",
"namespace", "object", "size"]
class Namespace(InboxAPIObject):
attrs = ["account", "email_address", "id", "namespace", "object",
"provider"]
class InboxClient(object):
"""A basic client for the Inbox API."""
Message = namedtuple('Message', 'id thread')
@classmethod
def from_email(cls, email_address):
namespaces = cls.namespaces()
for ns in namespaces:
if ns["email_address"] == email_address:
return cls(ns["id"])
return None
@classmethod
def request_code(cls, app_id, hint, redirect_uri):
return ("https://beta.inboxapp.com/oauth/authorize?client_id="
"%s&response_type=code&scope=email&login_hint="
"%s&redirect_uri=%s") % (app_id,
hint,
redirect_uri)
@classmethod
def get_access_token(cls, app_id, app_secret, code):
url = "https://www.inboxapp.com/oauth/token"
params = {"client_id": app_id,
"client_secret": app_secret,
"grant_type": 'authorization_code',
"code": code}
headers = {'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain'}
r = requests.post(url, data=params).json()
access_token = r['access_token']
return access_token
@classmethod
def from_token(cls, token, apiBase=API_BASE):
return cls(token=token)
@classmethod
def from_namespace(cls, namespace, apiBase=API_BASE):
"""Only used for testing the API locally"""
return cls(namespace=namespace, apiBase=apiBase)
def __init__(self, namespace=None, apiBase=API_BASE, token=None):
self.default_namespace = namespace
self.apiBase = apiBase
self.token = token
self.session = requests.Session()
if token is not None:
# We're using the client against the authenticated API.
# add the necessary headers.
try:
token_encoded = b64encode(token + ':')
except TypeError:
token_encoded = b64encode(bytes(token + ':', 'UTF-8')).decode('UTF-8')
headers = {'Authorization': 'Basic ' + token_encoded,
'X-Inbox-API-Wrapper': 'Python'}
self.session.headers.update(headers)
# Add a default namespace
if namespace is None:
self.default_namespace = self.get_namespaces(apiBase=apiBase)[0]
def _get_resources(self, resource, cls, **kwargs):
namespace = self.default_namespace
if 'namespace' in kwargs:
namespace = kwargs.pop('namespace')
url = "%s%s/%s?" % (self.apiBase, namespace, resource)
for arg in kwargs:
url += "%s=%s&" % (arg, kwargs[arg])
response = self.session.get(url)
if response.status_code != 200:
response.raise_for_status()
result = response.json()
ret = []
for entry in result:
ret.append(cls.from_dict(entry))
return ret
def _get_resource(self, resource, cls, resource_id, **kwargs):
"""Get an individual REST resource"""
namespace = self.default_namespace
if 'namespace' in kwargs:
namespace = kwargs.pop('namespace')
url = "%s%s/%s/%s?" % (self.apiBase, namespace, resource,
resource_id)
for arg in kwargs:
url += "%s=%s&" % (arg, kwargs[arg])
url = url[:-1]
response = self.session.get(url)
if response.status_code != 200:
response.raise_for_status()
result = response.json()
return cls.from_dict(result)
def _create_resource(self, resource, cls, contents):
namespace = self.default_namespace
if 'namespace' in kwargs:
namespace = kwargs.pop('namespace')
url = "%s%s/%s" % (self.apiBase, namespace, resource)
response = self.session.post(url, data=json.dumps(contents))
result = response.json()
return cls.from_dict(result)
def _create_resources(self, resource, cls, contents):
"""batch resource creation and parse the returned list"""
namespace = self.default_namespace
if 'namespace' in kwargs:
namespace = kwargs.pop('namespace')
url = "%s%s/%s" % (self.apiBase, namespace, resource)
response = self.session.post(url, data=json.dumps(contents))
result = response.json()
ret = []
for entry in result:
ret.append(cls.from_dict(entry))
return ret
def _update_resource(self, resource, cls, id, data):
namespace = self.default_namespace
if 'namespace' in kwargs:
namespace = kwargs.pop('namespace')
url = "%s%s/%s" % (self.apiBase, namespace, resource)
response = self.session.post(url, data=json.dumps(data))
if response.status_code != 200:
response.raise_for_status()
result = response.json()
return cls.from_dict(result)
def get_namespaces(self, **kwargs):
response = self.session.get(self.apiBase)
result = response.json()
ret = []
for entry in result:
ret.append(Namespace.from_dict(entry))
return ret
def get_messages(self, **kwargs):
return self._get_resources("messages", Message, **kwargs)
def get_threads(self, **kwargs):
return self._get_resources("threads", Thread, **kwargs)
def get_thread(self, id, **kwargs):
return self._get_resource("threads", Thread, id, **kwargs)
def get_drafts(self, **kwargs):
return self._get_resources("drafts", Draft, **kwargs)
def get_draft(self, id, **kwargs):
return self._get_resource("drafts", Draft, id, **kwargs)
def get_files(self, **kwargs):
return self._get_resources("files", File, id, **kwargs)
def get_file(self, id, **kwargs):
return self._get_resource("files", File, id, **kwargs)
def create_draft(self, body):
return self._create_resource("drafts", Draft, body)
def create_tag(self, tagname):
return self._create_resource("tags", Tag, {"name": tagname})
def create_files(self, body):
url = "%s%s/files" % (self.apiBase, self.namespace)
response = self.session.post(url, files=body)
result = response.json()
ret = []
for entry in result:
ret.append(File.from_dict(entry))
return ret
def update_tags(self, thread_id, tags):
url = "%s%s/threads/%s" % (self.apiBase, self.namespace, thread_id)
return self.session.put(url, data=json.dumps(tags))
def send_message(self, message):
url = "%s%s/send" % (self.apiBase, self.namespace)
send_req = self.session.post(url, data=json.dumps(message))
return send_req
def send_draft(self, draft_id):
return self._update_resource("send", Draft, id, {"draft_id": draft_id})