This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathareas.py
540 lines (419 loc) · 18.7 KB
/
areas.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# -*- coding: utf-8 -*-
try:
import json
except ImportError:
import simplejson as json
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
class Area(object):
"""Represents a Podio Area"""
def __init__(self, transport):
self.transport = transport
@staticmethod
def sanitize_id(item_id):
if isinstance(item_id, int):
return str(item_id)
return item_id
@staticmethod
def get_options(silent=False, hook=True):
"""
Generate a query string with the appropriate options.
:param silent: If set to true, the object will not be bumped up in the stream and
notifications will not be generated.
:type silent: bool
:param hook: True if hooks should be executed for the change, false otherwise.
:type hook: bool
:return: The generated query string
:rtype: str
"""
options_ = {}
if silent:
options_['silent'] = silent
if not hook:
options_['hook'] = hook
if options_:
return '?' + urlencode(options_).lower()
else:
return ''
class Embed(Area):
def __init__(self, *args, **kwargs):
super(Embed, self).__init__(*args, **kwargs)
def create(self, attributes):
if type(attributes) != dict:
return ApiErrorException('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/embed/', body=attributes, type='application/json')
class Contact(Area):
def __init__(self, *args, **kwargs):
super(Contact, self).__init__(*args, **kwargs)
def create(self, space_id, attributes):
if type(attributes) != dict:
return ApiErrorException('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/contact/space/%d/' % space_id, body=attributes, type='application/json')
class Search(Area):
def __init__(self, *args, **kwargs):
super(Search, self).__init__(*args, **kwargs)
def searchApp(self, app_id, attributes):
if type(attributes) != dict:
return ApiErrorException('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/search/app/%d/' % app_id, body=attributes, type='application/json')
class Item(Area):
def find(self, item_id, basic=False, **kwargs):
"""
Get item
:param item_id: Item ID
:type item_id: int
:return: Item info
:rtype: dict
"""
if basic:
return self.transport.GET(url='/item/%d/basic' % item_id)
return self.transport.GET(kwargs, url='/item/%d' % item_id)
def filter(self, app_id, attributes, **kwargs):
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url="/item/app/%d/filter/" % app_id, body=attributes,
type="application/json", **kwargs)
def find_all_by_external_id(self, app_id, external_id):
return self.transport.GET(url='/item/app/%d/v2/?external_id=%r' % (app_id, external_id))
def revisions(self, item_id):
return self.transport.GET(url='/item/%d/revision/' % item_id)
def revision_difference(self, item_id, revision_from_id, revision_to_id):
return self.transport.GET(url='/item/%d/revision/%d/%d' % (item_id, revision_from_id,
revision_to_id))
def values(self, item_id):
return self.transport.GET(url='/item/%s/value' % item_id)
def values_v2(self, item_id):
return self.transport.GET(url='/item/%s/value/v2' % item_id)
def create(self, app_id, attributes, silent=False, hook=True):
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(body=attributes,
type='application/json',
url='/item/app/%d/%s' % (app_id,
self.get_options(silent=silent,
hook=hook)))
def update(self, item_id, attributes, silent=False, hook=True):
"""
Updates the item using the supplied attributes. If 'silent' is true, Podio will send
no notifications to subscribed users and not post updates to the stream.
Important: webhooks will still be called.
"""
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.PUT(body=attributes,
type='application/json',
url='/item/%d%s' % (item_id, self.get_options(silent=silent,
hook=hook)))
def delete(self, item_id, silent=False, hook=True):
return self.transport.DELETE(url='/item/%d%s' % (item_id,
self.get_options(silent=silent,
hook=hook)),
handler=lambda x, y: None)
def bulk_delete(self, app_id, attributes, silent=False):
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(body=attributes,
type='application/json',
url='/item/app/%d/delete/%s' % (app_id,
self.get_options(silent=silent)))
class Application(Area):
def activate(self, app_id):
"""
Activates the application with app_id
:param app_id: Application ID
:type app_id: str or int
:return: Python dict of JSON response
:rtype: dict
"""
return self.transport.POST(url='/app/%s/activate' % app_id)
def create(self, attributes):
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/app/', body=attributes, type='application/json')
def add_field(self, app_id, attributes):
"""
Adds a new field to app with app_id
:param app_id: Application ID
:type app_id: str or int
:param attributes: Refer to API.
:type attributes: dict
:return: Python dict of JSON response
:rtype: dict
"""
if not isinstance(attributes, dict):
raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/app/%s/field/' % app_id, body=attributes,
type='application/json')
def deactivate(self, app_id):
"""
Deactivates the application with app_id
:param app_id: Application ID
:type app_id: str or int
:return: Python dict of JSON response
:rtype: dict
"""
return self.transport.POST(url='/app/%s/deactivate' % app_id)
def delete(self, app_id):
"""
Deletes the app with the given id.
:param app_id: Application ID
:type app_id: str or int
"""
return self.transport.DELETE(url='/app/%s' % app_id)
def find(self, app_id):
"""
Finds application with id app_id.
:param app_id: Application ID
:type app_id: str or int
:return: Python dict of JSON response
:rtype: dict
"""
return self.transport.GET(url='/app/%s' % app_id)
def dependencies(self, app_id):
"""
Finds application dependencies for app with id app_id.
:param app_id: Application ID
:type app_id: str or int
:return: Python dict of JSON response with the apps that the given app depends on.
:rtype: dict
"""
return self.transport.GET(url='/app/%s/dependencies/' % app_id)
def get_items(self, app_id, **kwargs):
return self.transport.GET(url='/item/app/%s/' % app_id, **kwargs)
def list_in_space(self, space_id):
"""
Returns a list of all the visible apps in a space.
:param space_id: Space ID
:type space_id: str
"""
return self.transport.GET(url='/app/space/%s/' % space_id)
class Task(Area):
def get(self, **kwargs):
"""
Get tasks endpoint. QueryStrings are kwargs
"""
return self.transport.GET('/task/', **kwargs)
def delete(self, task_id):
"""
Deletes the app with the given id.
:param task_id: Task ID
:type task_id: str or int
"""
return self.transport.DELETE(url='/task/%s' % task_id)
def complete(self, task_id):
"""
Mark the given task as completed.
:param task_id: Task ID
:type task_id: str or int
"""
return self.transport.POST(url='/task/%s/complete' % task_id)
def create(self, attributes, silent=False, hook=True):
"""
https://developers.podio.com/doc/tasks/create-task-22419
Creates the task using the supplied attributes. If 'silent' is true,
Podio will send no notifications to subscribed users and not post
updates to the stream. If 'hook' is false webhooks will not be called.
"""
#if not isinstance(attributes, dict):
# raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(url='/task/%s' % self.get_options(silent=silent, hook=hook),
body=attributes,
type='application/json')
def create_for(self, ref_type, ref_id, attributes, silent=False, hook=True):
"""
https://developers.podio.com/doc/tasks/create-task-with-reference-22420
If 'silent' is true, Podio will send no notifications and not post
updates to the stream. If 'hook' is false webhooks will not be called.
"""
#if not isinstance(attributes, dict):
# raise TypeError('Must be of type dict')
attributes = json.dumps(attributes)
return self.transport.POST(body=attributes,
type='application/json',
url='/task/%s/%s/%s' % (ref_type, ref_id,
self.get_options(silent=silent,
hook=hook)))
class User(Area):
def current(self):
return self.transport.get(url='/user/')
class Org(Area):
def get_all(self):
return self.transport.get(url='/org/')
class Status(Area):
def find(self, status_id):
return self.transport.GET(url='/status/%s' % status_id)
def create(self, space_id, attributes):
attributes = json.dumps(attributes)
return self.transport.POST(url='/status/space/%s/' % space_id,
body=attributes, type='application/json')
class Space(Area):
def find(self, space_id):
return self.transport.GET(url='/space/%s' % space_id)
def find_by_url(self, space_url, id_only=True):
"""
Returns a space ID given the URL of the space.
:param space_url: URL of the Space
:return: space_id: Space url
:rtype: str
"""
resp = self.transport.GET(url='/space/url?%s' % urlencode({'url': space_url}))
if id_only:
return resp['space_id']
return resp
def find_all_for_org(self, org_id):
"""
Find all of the spaces in a given org.
:param org_id: Orginization ID
:type org_id: str
:return: Details of spaces
:rtype: dict
"""
return self.transport.GET(url='/org/%s/space/' % org_id)
def create(self, attributes):
"""
Create a new space
:param attributes: Refer to API. Pass in argument as dictionary
:type attributes: dict
:return: Details of newly created space
:rtype: dict
"""
if not isinstance(attributes, dict):
raise TypeError('Dictionary of values expected')
attributes = json.dumps(attributes)
return self.transport.POST(url='/space/', body=attributes, type='application/json')
class Stream(Area):
"""
The stream API will supply the different streams. Currently
supported is the global stream, the organization stream and the
space stream.
For details, see: https://developers.podio.com/doc/stream/
"""
def find_all_by_app_id(self, app_id):
"""
Returns the stream for the given app. This includes items from
the app and tasks on the app.
For details, see: https://developers.podio.com/doc/stream/get-app-stream-264673
"""
return self.transport.GET(url='/stream/app/%s/' % app_id)
def find_all(self):
"""
Returns the global stream. The types of objects in the stream
can be either "item", "status", "task", "action" or
"file". The data part of the result depends on the type of
object and is specified on this page:
https://developers.podio.com/doc/stream/get-global-stream-80012
"""
return self.transport.GET(url='/stream/')
def find_all_by_org_id(self, org_id):
"""
Returns the activity stream for the given organization.
For details, see: https://developers.podio.com/doc/stream/get-organization-stream-80038
"""
return self.transport.GET(url='/stream/org/%s/' % org_id)
def find_all_personal(self):
"""
Returns the personal stream from personal spaces and sub-orgs.
For details, see: https://developers.podio.com/doc/stream/get-personal-stream-1656647
"""
return self.transport.GET(url='/stream/personal/')
def find_all_by_space_id(self, space_id):
"""
Returns the activity stream for the space.
For details, see: https://developers.podio.com/doc/stream/get-space-stream-80039
"""
return self.transport.GET(url='/stream/space/%s/' % space_id)
def find_by_ref(self, ref_type, ref_id):
"""
Returns an object of type "item", "status" or "task" as a
stream object. This is useful when a new status has been
posted and should be rendered directly in the stream without
reloading the entire stream.
For details, see: https://developers.podio.com/doc/stream/get-stream-object-80054
"""
return self.transport.GET(url='/stream/%s/%s' % (ref_type, ref_id))
class Hook(Area):
def create(self, hookable_type, hookable_id, attributes):
attributes = json.dumps(attributes)
return self.transport.POST(url='/hook/%s/%s/' % (hookable_type, hookable_id),
body=attributes, type='application/json')
def verify(self, hook_id):
return self.transport.POST(url='/hook/%s/verify/request' % hook_id)
def validate(self, hook_id, code):
return self.transport.POST(url='/hook/%s/verify/validate' % hook_id, code=code)
def delete(self, hook_id):
return self.transport.DELETE(url='/hook/%s' % hook_id)
def find_all_for(self, hookable_type, hookable_id):
return self.transport.GET(url='/hook/%s/%s/' % (hookable_type, hookable_id))
class Connection(Area):
def create(self, attributes):
attributes = json.dumps(attributes)
return self.transport.POST(url='/connection/', body=attributes, type='application/json')
def find(self, conn_id):
return self.transport.GET(url='/connection/%s' % conn_id)
def delete(self, conn_id):
return self.transport.DELETE(url='/connection/%s' % conn_id)
def reload(self, conn_id):
return self.transport.POST(url='/connection/%s/load' % conn_id)
class Notification(Area):
def find(self, notification_id):
return self.transport.GET(url='/notification/%s' % notification_id)
def find_all(self):
return self.transport.GET(url='/notification/')
def get_inbox_new_count(self):
return self.transport.GET(url='/notification/inbox/new/count')
def mark_as_viewed(self, notification_id):
return self.transport.POST(url='/notification/%s/viewed' % notification_id)
def mark_all_as_viewed(self):
return self.transport.POST(url='/notification/viewed')
def star(self, notification_id):
return self.transport.POST(url='/notification/%s/star' % notification_id)
def unstar(self, notification_id):
return self.transport.DELETE(url='/notification/%s/star' % notification_id)
class Conversation(Area):
def find_all(self):
return self.transport.GET(url='/conversation/')
def find(self, conversation_id):
return self.transport.GET(url='/conversation/%s' % conversation_id)
def create(self, attributes):
attributes = json.dumps(attributes)
return self.transport.POST(url='/conversation/', body=attributes, type='application/json')
def star(self, conversation_id):
return self.transport.POST(url='/conversation/%s/star' % conversation_id)
def unstar(self, conversation_id):
return self.transport.DELETE(url='/conversation/%s/star' % conversation_id)
def leave(self, conversation_id):
return self.transport.POST(url='/conversation/%s/leave' % conversation_id)
class Files(Area):
def find(self, file_id):
pass
def find_raw(self, file_id):
"""Returns raw file as string. Pass to a file object"""
raw_handler = lambda resp, data: data
return self.transport.GET(url='/file/%d/raw' % file_id, handler=raw_handler)
def attach(self, file_id, ref_type, ref_id):
attributes = {
'ref_type': ref_type,
'ref_id': ref_id
}
return self.transport.POST(url='/file/%s/attach' % file_id, body=json.dumps(attributes),
type='application/json')
def create(self, filename, filedata):
"""Create a file from raw data"""
attributes = {'filename': filename,
'source': filedata}
return self.transport.POST(url='/file/v2/', body=attributes, type='multipart/form-data')
def copy(self, file_id):
"""Copy a file to generate a new file_id"""
return self.transport.POST(url='/file/%s/copy' % file_id)