Skip to content

Commit dbf1d25

Browse files
committed
Add Contact endpoints
1 parent 24fbcbe commit dbf1d25

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

messagebird/client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from messagebird.base import Base
55
from messagebird.balance import Balance
6+
from messagebird.contact import Contact, ContactList
67
from messagebird.error import Error
78
from messagebird.hlr import HLR
89
from messagebird.http_client import HttpClient
@@ -43,6 +44,24 @@ def request(self, path, method='GET', params=None):
4344

4445
return response_json
4546

47+
def request_plain_text(self, path, method='GET', params=None):
48+
"""Builds a request, gets a response and returns the body."""
49+
response_text = self.http_client.request(path, method, params)
50+
51+
try:
52+
# Try to decode the response to JSON to see if the API returned any
53+
# errors.
54+
response_json = json.loads(response_text)
55+
56+
if 'errors' in response_json:
57+
raise (ErrorException([Error().load(e) for e in response_json['errors']]))
58+
except ValueError:
59+
# Do nothing: json.loads throws if the input string is not valid JSON,
60+
# which is expected. We'll just return the response body below.
61+
pass
62+
63+
return response_text
64+
4665
def balance(self):
4766
"""Retrieve your balance."""
4867
return Balance().load(self.request('balance'))
@@ -109,3 +128,22 @@ def verify_create(self, recipient, params=None):
109128
def verify_verify(self, id, token):
110129
"""Verify the token of a specific verification."""
111130
return Verify().load(self.request('verify/' + str(id), params={'token': token}))
131+
132+
def contact(self, id):
133+
"""Retrieve the information of a specific contact."""
134+
return Contact().load(self.request('contacts/' + str(id)))
135+
136+
def contact_create(self, phonenumber, params=None):
137+
if params is None: params = {}
138+
params.update({'msisdn': phonenumber})
139+
return Contact().load(self.request('contacts', 'POST', params))
140+
141+
def contact_delete(self, id):
142+
self.request_plain_text('contacts/' + str(id), 'DELETE')
143+
144+
def contact_update(self, id, params=None):
145+
self.request('contacts/' + str(id), 'PATCH', params)
146+
147+
def contact_list(self, limit=0, offset=0):
148+
query = 'limit='+str(limit)+'&offset='+str(offset)
149+
return ContactList().load(self.request('contacts?'+query, 'GET', None))

0 commit comments

Comments
 (0)