This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
/
contacts.py
154 lines (123 loc) · 4.22 KB
/
contacts.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
"""
Experimental client for Vumi Go's contacts API.
TODO:
* Factor out common API-level code, such as auth.
* Implement more of the API as the server side grows.
"""
import json
import requests
class ContactsApiClient(object):
"""
Client for Vumi Go's contacts API.
:param str auth_token:
An OAuth 2 access token. NOTE: This will be replaced by a proper
authentication system at some point.
:param str api_url:
The full URL of the HTTP API. Defaults to
``http://go.vumi.org/api/v1/go``.
:type session:
:class:`requests.Session`
:param session:
Requests session to use for HTTP requests. Defaults to a new session.
"""
def __init__(self, auth_token, api_url=None, session=None):
self.auth_token = auth_token
if api_url is None:
api_url = "http://go.vumi.org/api/v1/go"
self.api_url = api_url.rstrip('/')
if session is None:
session = requests.Session()
self.session = session
def _api_request(self, method, api_collection, api_path, data=None):
url = "%s/%s/%s" % (self.api_url, api_collection, api_path)
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Bearer %s" % (self.auth_token,),
}
if data is not None:
data = json.dumps(data)
r = self.session.request(method, url, data=data, headers=headers)
r.raise_for_status()
return r.json()
def contacts(self, start_cursor=None):
"""
Retrieve all contacts.
This uses the API's paginated contact download.
:param start_cursor:
An optional parameter that declares the cursor to start fetching
the contacts from.
:returns:
An iterator over all contacts.
"""
if start_cursor:
page = self._api_request(
"GET", "contacts", "?cursor=%s" % start_cursor)
else:
page = self._api_request("GET", "contacts", "")
while True:
for contact in page['data']:
yield contact
if page['cursor'] is None:
break
page = self._api_request(
"GET", "contacts", "?cursor=%s" % page['cursor'])
def create_contact(self, contact_data):
"""
Create a contact.
:param dict contact_data:
Data for new contact.
"""
return self._api_request("POST", "contacts", "", contact_data)
def get_contact(self, contact_key):
"""
Get a contact.
:param str contact_key:
Key for the contact to get.
"""
return self._api_request("GET", "contacts", contact_key)
def update_contact(self, contact_key, update_data):
"""
Update a contact.
:param str contact_key:
Key for the contact to update.
:param dict update_data:
Fields to modify.
"""
return self._api_request("PUT", "contacts", contact_key, update_data)
def delete_contact(self, contact_key):
"""
Delete a contact.
:param str contact_key:
Key for the contact to delete.
"""
return self._api_request("DELETE", "contacts", contact_key)
def create_group(self, group_data):
"""
Create a group.
:param dict group_data:
Data for new group.
"""
return self._api_request("POST", "groups", "", group_data)
def get_group(self, group_key):
"""
Get a group
:param str group_key:
Key for the group to get
"""
return self._api_request("GET", "groups", group_key)
def update_group(self, group_key, update_data):
"""
Update a group.
:param str group_key
Key for the group to update.
:param str update_data
Fields to modify.
"""
return self._api_request("PUT", "groups", group_key, update_data)
def delete_group(self, group_key):
"""
Delete a group.
:param str group_key
Key for the group to delete.
"""
return self._api_request("DELETE", "groups", group_key)