1515from messagebird .conversation import Conversation , ConversationList
1616from messagebird .conversation_webhook import ConversationWebhook , ConversationWebhookList
1717
18- try :
19- from urllib .parse import urlencode
20- except ImportError :
21- from urllib import urlencode
22-
2318ENDPOINT = 'https://rest.messagebird.com'
2419CLIENT_VERSION = '1.3.1'
2520PYTHON_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 )
0 commit comments