Skip to content

Commit fb947c2

Browse files
committed
Update list methods to receive limit and offset by parameter
1 parent 380069a commit fb947c2

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

messagebird/client.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
from messagebird.conversation import Conversation, ConversationList
1616
from messagebird.conversation_webhook import ConversationWebhook, ConversationWebhookList
1717

18-
try:
19-
from urllib.parse import urlencode
20-
except ImportError:
21-
from urllib import urlencode
22-
2318
ENDPOINT = 'https://rest.messagebird.com'
2419
CLIENT_VERSION = '1.3.1'
2520
PYTHON_VERSION = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
@@ -45,7 +40,7 @@ def __init__(self, access_key, http_client=None):
4540
self.access_key = access_key
4641
self.http_client = http_client
4742

48-
def getHttpClient(self, type=REST_TYPE):
43+
def _get_http_client(self, type=REST_TYPE):
4944
if self.http_client:
5045
return self.http_client
5146

@@ -56,7 +51,7 @@ def getHttpClient(self, type=REST_TYPE):
5651

5752
def request(self, path, method='GET', params=None, type=REST_TYPE):
5853
"""Builds a request, gets a response and decodes it."""
59-
response_text = self.getHttpClient(type).request(path, method, params)
54+
response_text = self._get_http_client(type).request(path, method, params)
6055

6156
if not response_text:
6257
return response_text
@@ -70,7 +65,7 @@ def request(self, path, method='GET', params=None, type=REST_TYPE):
7065

7166
def request_plain_text(self, path, method='GET', params=None, type=REST_TYPE):
7267
"""Builds a request, gets a response and returns the body."""
73-
response_text = self.getHttpClient(type).request(path, method, params)
68+
response_text = self._get_http_client(type).request(path, method, params)
7469

7570
try:
7671
# Try to decode the response to JSON to see if the API returned any
@@ -173,7 +168,7 @@ def contact_update(self, id, params=None):
173168
self.request_plain_text('contacts/' + str(id), 'PATCH', params)
174169

175170
def contact_list(self, limit=10, offset=0):
176-
query = 'limit='+str(limit)+'&offset='+str(offset)
171+
query = self._format_query(limit, offset)
177172
return ContactList().load(self.request('contacts?'+query, 'GET', None))
178173

179174
def group(self, id):
@@ -188,7 +183,7 @@ def group_delete(self, id):
188183
self.request_plain_text('groups/' + str(id), 'DELETE', None)
189184

190185
def group_list(self, limit=10, offset=0):
191-
query = 'limit=' + str(limit) + '&offset=' + str(offset)
186+
query = self._format_query(limit, offset)
192187
return GroupList().load(self.request('groups?'+query, 'GET', None))
193188

194189
def group_update(self, id, name, params=None):
@@ -209,11 +204,8 @@ def __group_add_contacts_query(self, contactIds):
209204
def group_remove_contact(self, groupId, contactId):
210205
self.request_plain_text('groups/' + str(groupId) + '/contacts/' + str(contactId), 'DELETE', None)
211206

212-
def conversation_list(self, options=None):
213-
uri = CONVERSATION_PATH
214-
if options is not None:
215-
uri += '?' + urlencode(options)
216-
207+
def conversation_list(self, limit=10, offset=0):
208+
uri = CONVERSATION_PATH + '?' + self._format_query(limit, offset)
217209
return ConversationList().load(self.request(uri, 'GET', None, CONVERSATION_TYPE))
218210

219211
def conversation_start(self, start_request):
@@ -228,11 +220,9 @@ def conversation_read(self, id):
228220
uri = CONVERSATION_PATH + '/' + str(id)
229221
return Conversation().load(self.request(uri, 'GET', None, CONVERSATION_TYPE))
230222

231-
def conversation_list_messages(self, conversation_id, options=None):
223+
def conversation_list_messages(self, conversation_id, limit=10, offset=0):
232224
uri = CONVERSATION_PATH + '/' + str(conversation_id) + '/' + CONVERSATION_MESSAGES_PATH
233-
234-
if options is not None:
235-
uri += '?' + urlencode(options)
225+
uri += '?' + self._format_query(limit, offset)
236226

237227
return ConversationMessageList().load(self.request(uri, 'GET', None, CONVERSATION_TYPE))
238228

@@ -251,13 +241,14 @@ def conversation_delete_webhook(self, id):
251241
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
252242
self.request(uri, 'DELETE', None, CONVERSATION_TYPE)
253243

254-
def conversation_list_webhooks(self, options=None):
255-
uri = CONVERSATION_WEB_HOOKS_PATH
256-
if options is not None:
257-
uri += '?' + urlencode(options)
244+
def conversation_list_webhooks(self, limit=10, offset=0):
245+
uri = CONVERSATION_WEB_HOOKS_PATH + '?' + self._format_query(limit, offset)
258246

259247
return ConversationWebhookList().load(self.request(uri, 'GET', None, CONVERSATION_TYPE))
260248

261249
def conversation_read_webhook(self, id):
262250
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
263251
return ConversationWebhook().load(self.request(uri, 'GET', None, CONVERSATION_TYPE))
252+
253+
def _format_query(self, limit, offset):
254+
return 'limit='+str(limit)+'&offset='+str(offset)

tests/test_conversation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_conversation_list(self):
4141

4242
Client('', http_client).conversation_list()
4343

44-
http_client.request.assert_called_once_with('conversations', 'GET', None)
44+
http_client.request.assert_called_once_with('conversations?limit=10&offset=0', 'GET', None)
4545

4646
def test_conversation_read(self):
4747
http_client = Mock()

tests/test_conversation_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_conversation_list_messages(self):
2121
self.assertEqual(1, msg.count)
2222
self.assertEqual('54445534', msg.items[0].id)
2323

24-
http_client.request.assert_called_once_with('conversations/54567/messages', 'GET', None)
24+
http_client.request.assert_called_once_with('conversations/54567/messages?limit=10&offset=0', 'GET', None)
2525

2626
def test_conversation_read_message(self):
2727
http_client = Mock()

tests/test_conversation_webhook.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ def test_conversation_webhook_list(self):
4242
http_client.request.return_value = '{"offset":0,"limit":10,"count":2,"totalCount":2,"items":[{"id":"57b96dbe0fda40f0a814f5e3268c30a9","contactId":"8846d44229094c20813cf9eea596e680","contact":{"id":"8846d44229094c20813cf9eea596e680","href":"https://contacts.messagebird.com/v2/contacts/8846d44229094c20813cf9eea596e680","msisdn":31617110163,"displayName":"31617110163","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:54:39Z","updatedDatetime":"2019-04-02T08:54:40Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:54:38Z","updatedDatetime":"2019-04-02T14:24:09.192202886Z","lastReceivedDatetime":"2019-04-02T14:24:09.14826339Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":2,"href":"https://conversations.messagebird.com/v1/conversations/57b96dbe0fda40f0a814f5e3268c30a9/messages"}},{"id":"07e823fdb36a462fb5e187d6d7b96493","contactId":"459a35432b0c4195abbdae353eb19359","contact":{"id":"459a35432b0c4195abbdae353eb19359","href":"https://contacts.messagebird.com/v2/contacts/459a35432b0c4195abbdae353eb19359","msisdn":31615164888,"displayName":"31615164888","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-03T07:35:47.35395356Z","lastReceivedDatetime":"2019-04-02T12:02:22.707634424Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":16,"href":"https://conversations.messagebird.com/v1/conversations/07e823fdb36a462fb5e187d6d7b96493/messages"}}]}'
4343

4444
Client('', http_client).conversation_list_webhooks()
45-
http_client.request.assert_called_once_with('webhooks', 'GET', None)
45+
http_client.request.assert_called_once_with('webhooks?limit=10&offset=0', 'GET', None)
4646

4747
def test_conversation_webhook_list_pagination(self):
4848
http_client = Mock()
4949
http_client.request.return_value = '{"offset":0,"limit":10,"count":2,"totalCount":2,"items":[{"id":"57b96dbe0fda40f0a814f5e3268c30a9","contactId":"8846d44229094c20813cf9eea596e680","contact":{"id":"8846d44229094c20813cf9eea596e680","href":"https://contacts.messagebird.com/v2/contacts/8846d44229094c20813cf9eea596e680","msisdn":31617110163,"displayName":"31617110163","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:54:39Z","updatedDatetime":"2019-04-02T08:54:40Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:54:38Z","updatedDatetime":"2019-04-02T14:24:09.192202886Z","lastReceivedDatetime":"2019-04-02T14:24:09.14826339Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":2,"href":"https://conversations.messagebird.com/v1/conversations/57b96dbe0fda40f0a814f5e3268c30a9/messages"}},{"id":"07e823fdb36a462fb5e187d6d7b96493","contactId":"459a35432b0c4195abbdae353eb19359","contact":{"id":"459a35432b0c4195abbdae353eb19359","href":"https://contacts.messagebird.com/v2/contacts/459a35432b0c4195abbdae353eb19359","msisdn":31615164888,"displayName":"31615164888","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-03T07:35:47.35395356Z","lastReceivedDatetime":"2019-04-02T12:02:22.707634424Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":16,"href":"https://conversations.messagebird.com/v1/conversations/07e823fdb36a462fb5e187d6d7b96493/messages"}}]}'
5050

51-
params = {'offset': 1, 'limit': 2}
52-
Client('', http_client).conversation_list_webhooks(params)
53-
http_client.request.assert_called_once_with('webhooks?offset=1&limit=2', 'GET', None)
51+
Client('', http_client).conversation_list_webhooks(2, 1)
52+
http_client.request.assert_called_once_with('webhooks?limit=2&offset=1', 'GET', None)
5453

5554
def test_conversation_webhook_read(self):
5655
http_client = Mock()

0 commit comments

Comments
 (0)