This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
/
elvanto.py
128 lines (106 loc) · 2.81 KB
/
elvanto.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
import json
import os
import requests
people_fields = [
'gender',
'birthday',
'anniversary',
'school_grade',
'marital_status',
'security_code',
'receipt_name',
'giving_number',
'mailing_address',
'mailing_address2',
'mailing_city',
'mailing_state',
'mailing_postcode',
'mailing_country',
'home_address',
'home_address2',
'home_city',
'home_state',
'home_postcode',
'home_country',
'reports_to',
# the following array fields are not handled nicely
# 'access_permissions',
# 'departments',
# 'service_types',
# 'demographics',
# 'locations',
# 'family',
# TODO handle custom fields
]
group_fields = [
'people',
# 'categories',
# 'departments',
# 'demographics',
# 'locations',
]
class ElvantoApiException(Exception):
"""Generic Elvanto Exception."""
pass
s = requests.Session()
def retry_request(url, http_method, *args, **kwargs):
"""Retry failed requests 3 times."""
assert http_method in ['get', 'post', 'delete', 'patch', 'put']
MAX_TRIES = 3
r_func = getattr(s, http_method)
tries = 0
while True:
resp = r_func(url, *args, **kwargs)
if resp.status_code != 200 and tries < MAX_TRIES:
tries += 1
continue
break
return resp
def e_api(end_point, **kwargs):
"""Elvanto api client."""
ELVANTO_KEY = os.environ.get('ELVANTO_KEY')
base_url = 'https://api.elvanto.com/v1/'
e_url = '{0}{1}.json'.format(base_url, end_point)
resp = retry_request(e_url, 'post', json=kwargs, auth=(ELVANTO_KEY, '_'))
data = json.loads(resp.text)
if data['status'] == 'ok':
return data
else:
raise ElvantoApiException(data['error'])
def pull_data(name, end_point, fields, ident):
"""Page through elvanto resource."""
page = 1
data = e_api(
end_point,
page_size=1000,
fields=fields,
)
if data['status'] != 'ok':
raise elvanto.ElvantoApiException
items = data[name][ident]
num_synced = data[name]['on_this_page']
while num_synced < data[name]['total']:
page += 1
more_data = e_api(
end_point,
page=page,
page_size=1000,
fields=fields,
)
items += more_data[name][ident]
num_synced += more_data[name]['on_this_page']
return {x['id']: x for x in items}
def pull_people():
"""Pull people from elvanto."""
name = 'people'
end_point = 'people/getAll'
fields = people_fields
ident = 'person'
return pull_data(name, end_point, fields, ident)
def pull_groups():
"""Pull groups from elvanto."""
name = 'groups'
end_point = 'groups/getAll'
fields = group_fields
ident = 'group'
return pull_data(name, end_point, fields, ident)