1313from bandwidth .messaging .controllers .base_controller import BaseController
1414from bandwidth .http .auth .messaging_basic_auth import MessagingBasicAuth
1515from bandwidth .messaging .models .media import Media
16+ from bandwidth .messaging .models .bandwidth_messages_list import BandwidthMessagesList
1617from bandwidth .messaging .models .bandwidth_message import BandwidthMessage
1718from bandwidth .messaging .exceptions .messaging_exception import MessagingException
1819
@@ -32,9 +33,9 @@ def list_media(self,
3233 listMedia
3334
3435 Args:
35- user_id (string): TODO: type description here.
36- continuation_token (string, optional): TODO: type description
37- here .
36+ user_id (string): User's account ID
37+ continuation_token (string, optional): Continuation token used to
38+ retrieve subsequent media .
3839
3940 Returns:
4041 ApiResponse: An object with the response value as well as other
@@ -96,8 +97,8 @@ def get_media(self,
9697 getMedia
9798
9899 Args:
99- user_id (string): TODO: type description here.
100- media_id (string): TODO: type description here.
100+ user_id (string): User's account ID
101+ media_id (string): Media ID to retrieve
101102
102103 Returns:
103104 ApiResponse: An object with the response value as well as other
@@ -158,13 +159,15 @@ def upload_media(self,
158159 uploadMedia
159160
160161 Args:
161- user_id (string): TODO: type description here.
162- media_id (string): TODO: type description here.
163- content_length (long|int): TODO: type description here.
162+ user_id (string): User's account ID
163+ media_id (string): The user supplied custom media ID
164+ content_length (long|int): The size of the entity-body
164165 body (typing.BinaryIO): TODO: type description here.
165- content_type (string, optional): TODO: type description here.
166- Example: application/octet-stream
167- cache_control (string, optional): TODO: type description here.
166+ content_type (string, optional): The media type of the
167+ entity-body
168+ cache_control (string, optional): General-header field is used to
169+ specify directives that MUST be obeyed by all caching
170+ mechanisms along the request/response chain.
168171
169172 Returns:
170173 ApiResponse: An object with the response value as well as other
@@ -233,8 +236,8 @@ def delete_media(self,
233236 deleteMedia
234237
235238 Args:
236- user_id (string): TODO: type description here.
237- media_id (string): TODO: type description here.
239+ user_id (string): User's account ID
240+ media_id (string): The media ID to delete
238241
239242 Returns:
240243 ApiResponse: An object with the response value as well as other
@@ -281,16 +284,121 @@ def delete_media(self,
281284 # Return appropriate type
282285 return ApiResponse (_response )
283286
287+ def get_messages (self ,
288+ user_id ,
289+ message_id = None ,
290+ source_tn = None ,
291+ destination_tn = None ,
292+ message_status = None ,
293+ error_code = None ,
294+ from_date_time = None ,
295+ to_date_time = None ,
296+ page_token = None ,
297+ limit = None ):
298+ """Does a GET request to /users/{userId}/messages.
299+
300+ getMessages
301+
302+ Args:
303+ user_id (string): User's account ID
304+ message_id (string, optional): The ID of the message to search
305+ for. Special characters need to be encoded using URL encoding
306+ source_tn (string, optional): The phone number that sent the
307+ message
308+ destination_tn (string, optional): The phone number that received
309+ the message
310+ message_status (string, optional): The status of the message. One
311+ of RECEIVED, QUEUED, SENDING, SENT, FAILED, DELIVERED,
312+ DLR_EXPIRED
313+ error_code (int, optional): The error code of the message
314+ from_date_time (string, optional): The start of the date range to
315+ search in ISO 8601 format. Uses the message receive time. The
316+ date range to search in is currently 14 days.
317+ to_date_time (string, optional): The end of the date range to
318+ search in ISO 8601 format. Uses the message receive time. The
319+ date range to search in is currently 14 days.
320+ page_token (string, optional): A base64 encoded value used for
321+ pagination of results
322+ limit (int, optional): The maximum records requested in search
323+ result. Default 100. The sum of limit and after cannot be more
324+ than 10000
325+
326+ Returns:
327+ ApiResponse: An object with the response value as well as other
328+ useful information such as status codes and headers.
329+ successful operation
330+
331+ Raises:
332+ APIException: When an error occurs while fetching the data from
333+ the remote API. This exception includes the HTTP Response
334+ code, an error message, and the HTTP body that was received in
335+ the request.
336+
337+ """
338+
339+ # Prepare query URL
340+ _url_path = '/users/{userId}/messages'
341+ _url_path = APIHelper .append_url_with_template_parameters (_url_path , {
342+ 'userId' : {'value' : user_id , 'encode' : False }
343+ })
344+ _query_builder = self .config .get_base_uri (Server .MESSAGINGDEFAULT )
345+ _query_builder += _url_path
346+ _query_parameters = {
347+ 'messageId' : message_id ,
348+ 'sourceTn' : source_tn ,
349+ 'destinationTn' : destination_tn ,
350+ 'messageStatus' : message_status ,
351+ 'errorCode' : error_code ,
352+ 'fromDateTime' : from_date_time ,
353+ 'toDateTime' : to_date_time ,
354+ 'pageToken' : page_token ,
355+ 'limit' : limit
356+ }
357+ _query_builder = APIHelper .append_url_with_query_parameters (
358+ _query_builder ,
359+ _query_parameters
360+ )
361+ _query_url = APIHelper .clean_url (_query_builder )
362+
363+ # Prepare headers
364+ _headers = {
365+ 'accept' : 'application/json'
366+ }
367+
368+ # Prepare and execute request
369+ _request = self .config .http_client .get (_query_url , headers = _headers )
370+ MessagingBasicAuth .apply (self .config , _request )
371+ _response = self .execute_request (_request )
372+
373+ # Endpoint and global error handling using HTTP status codes.
374+ if _response .status_code == 400 :
375+ raise MessagingException ('400 Request is malformed or invalid' , _response )
376+ elif _response .status_code == 401 :
377+ raise MessagingException ('401 The specified user does not have access to the account' , _response )
378+ elif _response .status_code == 403 :
379+ raise MessagingException ('403 The user does not have access to this API' , _response )
380+ elif _response .status_code == 404 :
381+ raise MessagingException ('404 Path not found' , _response )
382+ elif _response .status_code == 415 :
383+ raise MessagingException ('415 The content-type of the request is incorrect' , _response )
384+ elif _response .status_code == 429 :
385+ raise MessagingException ('429 The rate limit has been reached' , _response )
386+ self .validate_response (_response )
387+
388+ decoded = APIHelper .json_deserialize (_response .text , BandwidthMessagesList .from_dictionary )
389+ _result = ApiResponse (_response , body = decoded )
390+ return _result
391+
284392 def create_message (self ,
285393 user_id ,
286- body = None ):
394+ body ):
287395 """Does a POST request to /users/{userId}/messages.
288396
289397 createMessage
290398
291399 Args:
292- user_id (string): TODO: type description here.
293- body (MessageRequest, optional ): TODO: type description here.
400+ user_id (string): User's account ID
401+ body (MessageRequest): TODO: type description here.
294402
295403 Returns:
296404 ApiResponse: An object with the response value as well as other
0 commit comments