forked from simonvc/mondo-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmondo.py
333 lines (270 loc) · 11 KB
/
mondo.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import requests
import json
import datetime
API_ERRORS = {
400: "400: Bad Req. Your request has missing parameters or is malformed",
401: "401: Unauthorized. Your request is not authenticated.",
403: "403: Forbidden. Your request is authenticated \
but has insufficient permissions.",
405: "405: Method Not Allowed. You are using the incorrect HTTP verb. \
Double check whether it should be POST/GET/DELETE/etc.",
404: "404: Page Not Found. The endpoint requested does not exist.",
406: "406: Not Acceptable. Your application does not accept the content \
format returned according to the Accept headers sent in the request.",
429: "425: Too Many Requests. Your application is exceeding its rate limit. \
Back off, buddy.",
500: "500: Internal Server Error. Something is wrong on our end. Whoopsie",
504: "504 - Gateway Timeout Something has timed out on our end. Whoopsie"
}
class MondoClient():
"""
Create a client connection and get the token for a specific account
using either data in config.json or email / password in the class call
e.g. connection = MondoClient('[email protected]', 'passw0rd') or
connection = MondoClient() - which uses info from config.json
"""
url = 'https://production-api.gmon.io/'
def __init__(self, account_id=None, acc_password=None, url=None):
if url is not None:
self.url = url
# grab the access token on instantiating the class
# Using config.json to supply client, secret
# ... and optionally account & password
try:
with open('./config.json') as data_file:
config = json.load(data_file)
except IOError:
raise Exception('No config.json file found')
try:
self.client = config['client']
self.secret = config['secret']
except KeyError:
raise Exception('define API client / secret in config.json')
if account_id is None:
if 'account' in config:
self.account = config['account']
else:
raise Exception('No account email supplied')
else:
self.account = account_id
if acc_password is None:
if 'password' in config:
self.password = config['password']
else:
raise Exception('No account password supplied')
else:
self.password = acc_password
# With all the variables in place try to get a token for the connection
try:
token_response = self.get_token()
self.token = token_response['access_token']
self.refresh_token = token_response['refresh_token']
# set the token expiry time based on time + token expiry in seconds
# This is used in deliver_token() later
now = datetime.datetime.now()
delta = datetime.timedelta(seconds=token_response['expires_in'])
self.token_expires = now + delta
except TypeError:
raise Exception(token_response)
def get_token(self, client_id=None, client_secret=None,
username=None, password=None):
"""
Acquire an access token - uses config.JSON file for data by default
"""
if client_id is None:
client_id = self.client
if client_secret is None:
client_secret = self.secret
if username is None:
username = self.account
if password is None:
password = self.password
payload = {'grant_type': 'password',
'client_id': client_id,
'client_secret': client_secret,
'username': username,
'password': password}
r = requests.post(self.url + '/oauth2/token', payload)
if r.status_code == 200:
response = r.json()
self.token = response['access_token']
self.refresh_token = response['refresh_token']
return response
else:
return API_ERRORS[r.status_code]
def token_refresh(self, client_id=None, client_secret=None,
refresh_token=None):
"""
Refresh a previously acquired token
use config.json data by default
"""
if client_id is None:
client_id = self.client
if client_secret is None:
client_secret = self.secret
if refresh_token is None:
refresh_token = self.refresh_token
payload = {'grant_type': 'refresh_token',
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token}
r = requests.post(self.url + '/oauth2/token', payload)
if r.status_code == 200:
response = r.json()
self.token = response['access_token']
self.refresh_token = response['refresh_token']
return response
else:
return API_ERRORS[r.status_code]
def get_transaction(self, transaction_id, access_token=None, merchant=True):
"""
Get details about a transaction
Uses config.json secrets by default
"""
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
params = {}
if merchant:
params['expand[]'] = 'merchant'
r = requests.get(self.url + '/transactions/' + transaction_id,
params=params, headers=headers)
if r.status_code == 200:
return r.json()['transaction']
else:
return API_ERRORS[r.status_code]
def get_transactions(self, account_id, limit=100, since=None,
before=None, access_token=None):
"""
List transactions
"""
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
params = {'limit': limit, "account_id": account_id}
if since is not None:
params['since'] = since
if before is not None:
params['before'] = before
r = requests.get(self.url + '/transactions',
params=params, headers=headers)
if r.status_code == 200:
return r.json()['transactions']
else:
return API_ERRORS[r.status_code]
def authenticate(self, access_token=None, client_id=None, user_id=None):
"""
authenticate user
"""
if access_token is None:
access_token = self.deliver_token()
if client_id is None:
client_id = self.client
if user_id is None:
user_id = self.account
headers = {'Authorization': 'Bearer ' + str(access_token)}
r = requests.get(self.url + '/ping/whoami', headers=headers)
if r.status_code == 200:
return r.json()
else:
return API_ERRORS[r.status_code]
def get_accounts(self, access_token=None):
"""
detailed information about customer's accounts
uses config.json data by default
"""
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
r = requests.get(self.url + '/accounts', headers=headers)
if r.status_code == 200:
return r.json()['accounts']
else:
return API_ERRORS[r.status_code]
def get_primary_accountID(self, access_token=None):
"""
Get ID from the first account listed against an access token
"""
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
r = requests.get(self.url + '/accounts', headers=headers)
if r.status_code == 200:
return r.json()['accounts'][0]['id']
else:
return API_ERRORS[r.status_code]
def create_feed_item(self, title, image_url, background_color='#FCF1EE',
body_color='#FCF1EE', title_color='#333',
body='', account_id=None, access_token=None):
"""
publish a new feed entry
"""
if access_token is None:
access_token = self.deliver_token()
if account_id is None:
account_id = self.get_primary_accountID()
headers = {'Authorization': 'Bearer ' + access_token}
payload = {
"account_id": account_id,
"type": "basic",
"params[title]": title,
"params[image_url]": image_url,
"params[background_color]": background_color,
"params[body_color]": body_color,
"params[title_color]": title_color,
"params[body]": body
}
r = requests.post(self.url + '/feed', data=payload, headers=headers)
if r.status_code == 200:
return r.json()
else:
return API_ERRORS[r.status_code]
def register_webhook(self, url, account_id=None, access_token=None):
"""
register a webhook
instance.register_webhook(account_id, url, [access_token])
"""
if access_token is None:
access_token = self.deliver_token()
if account_id is None:
account_id = self.get_primary_accountID()
headers = {'Authorization': 'Bearer ' + access_token}
payload = {"account_id": account_id, "url": url}
r = requests.post(self.url + '/webhooks', data=payload, headers=headers)
if r.status_code == 200:
return r.json()
else:
return API_ERRORS[r.status_code]
def list_webhooks(self, account_id=None, access_token=None):
"""
List webhooks registered against an account
instance.list_webhooks([account_id], [access_token])
"""
if account_id is None:
account_id = self.get_primary_accountID()
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
params = {'account_id': account_id}
r = requests.get(self.url + '/webhooks', params=params, headers=headers)
if r.status_code == 200:
return r.json()['webhooks']
else:
return API_ERRORS[r.status_code]
def delete_webhook(self, webhook_id, access_token=None):
"""
delete a webhook
instance.delete_webhook(webhook_id, [access_token])
"""
if access_token is None:
access_token = self.deliver_token()
headers = {'Authorization': 'Bearer ' + access_token}
r = requests.delete(self.url + '/webhooks/' + webhook_id, headers=headers)
if r.status_code == 200:
return r.json()
else:
return API_ERRORS[r.status_code]
def deliver_token(self):
if datetime.datetime.now() > self.token_expires:
self.token_refresh()
return self.token