|
3 | 3 |
|
4 | 4 | from messagebird.base import Base |
5 | 5 | from messagebird.balance import Balance |
| 6 | +from messagebird.contact import Contact, ContactList |
6 | 7 | from messagebird.error import Error |
7 | 8 | from messagebird.hlr import HLR |
8 | 9 | from messagebird.http_client import HttpClient |
@@ -43,6 +44,24 @@ def request(self, path, method='GET', params=None): |
43 | 44 |
|
44 | 45 | return response_json |
45 | 46 |
|
| 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 | + |
46 | 65 | def balance(self): |
47 | 66 | """Retrieve your balance.""" |
48 | 67 | return Balance().load(self.request('balance')) |
@@ -109,3 +128,22 @@ def verify_create(self, recipient, params=None): |
109 | 128 | def verify_verify(self, id, token): |
110 | 129 | """Verify the token of a specific verification.""" |
111 | 130 | 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