diff --git a/cnam_demo.py b/cnam_demo.py index 583a5f9..9bc1f43 100644 --- a/cnam_demo.py +++ b/cnam_demo.py @@ -1,8 +1,15 @@ #!/usr/bin/env python import pprint import os +import random +import string from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import FlowroutenumbersandmessagingClient + +# Helper function for random strings +def random_generator(size=4, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for x in range(size)) + # Set up your api credentials and test mobile number for outbound SMS or MMS basic_auth_user_name = os.environ.get('FR_ACCESS_KEY') basic_auth_password = os.environ.get('FR_SECRET_KEY') @@ -11,8 +18,7 @@ basic_auth_password = os.environ.get('FR_SECRET_KEY') client = FlowroutenumbersandmessagingClient(basic_auth_user_name, basic_auth_password) numbers_controller = client.numbers cnams_controller = client.cnams - - +cnam_id = None print("--List CNAM Records") limit = 10 @@ -20,34 +26,44 @@ offset = None result = cnams_controller.list_cnams(limit, offset) pprint.pprint(result) - print("\n--List Approved CNAM Records") result = cnams_controller.list_cnams(limit, offset, is_approved=True) pprint.pprint(result) -cnam_id = result['data'][0]['id'] +if len(result['data']): + cnam_id = result['data'][0]['id'] -print("\n--List CNAM Detail") -cnam_id = result['data'][0]['id'] -result = cnams_controller.get_cnam(cnam_id) -pprint.pprint(result) + print("\n--List CNAM Detail") + result = cnams_controller.get_cnam(cnam_id) + pprint.pprint(result) + if len(result['data']): + cnam_id = result['data'][0]['id'] print("\n--Search for CNAM Record") result = cnams_controller.search_cnams(contains='CHRIS') pprint.pprint(result) -# print("\n--Create a CNAM Record") -# result = cnams_controller.create_cnam_record('CJL') -# pprint.pprint(result) -# cnam_id = result['data']['id'] +print("\n--Create a CNAM Record") +cnam_value = 'FR ' + random_generator() +result = cnams_controller.create_cnam_record(cnam_value) +pprint.pprint(result) +print("\nNOTE: Newly created CNAM records are NOT available to association " + "until they are approved.\n") print("\n--Associate a CNAM Record to a DID") -result = cnams_controller.associate_cnam(cnam_id, '12066417659') -pprint.pprint(result) +our_numbers = numbers_controller.list_account_phone_numbers() +did_id = our_numbers['data'][0]['id'] -print("\n--Unassociate a CNAM Record from a DID") -result = cnams_controller.unassociate_cnam('12066417659') -pprint.pprint(result) +if cnam_id is None: + print("Create some CNAM records and wait for approval before trying" + " to associate them with a DID") +else: + result = cnams_controller.associate_cnam(cnam_id, did_id) + pprint.pprint(result) -print("\n--Remove a CNAM Record from your account") -result = cnams_controller.remove_cnam(cnam_id) -pprint.pprint(result) + print("\n--Unassociate a CNAM Record from a DID") + result = cnams_controller.unassociate_cnam(did_id) + pprint.pprint(result) + + print("\n--Remove a CNAM Record from your account") + result = cnams_controller.remove_cnam(cnam_id) + pprint.pprint(result) diff --git a/flowroutenumbersandmessaging/controllers/base_controller.py b/flowroutenumbersandmessaging/controllers/base_controller.py index d6422f9..7ab2ea3 100644 --- a/flowroutenumbersandmessaging/controllers/base_controller.py +++ b/flowroutenumbersandmessaging/controllers/base_controller.py @@ -10,7 +10,9 @@ from ..api_helper import APIHelper from ..http.http_context import HttpContext from ..http.requests_client import RequestsClient +from ..exceptions.error_exception import ErrorException from ..exceptions.api_exception import APIException +from ..http.auth.basic_auth import BasicAuth class BaseController(object): @@ -98,3 +100,25 @@ class BaseController(object): if (context.response.status_code < 200) or \ (context.response.status_code > 208): raise APIException('HTTP response not OK.', context) + + # Process request and status code and response text + def handle_request_and_response(self, request): + BasicAuth.apply(request) + context = self.execute_request(request) + + # Endpoint and global error handling using HTTP status codes. + if context.response.status_code == 401: + raise ErrorException('Unauthorized – There was an issue with your ' + 'API credentials.', context) + elif context.response.status_code == 403: + raise ErrorException('Forbidden – You don\'t have permission to ' + 'access this resource.', context) + elif context.response.status_code == 404: + raise ErrorException('The specified resource was not found', + context) + elif context.response.status_code == 422: + raise ErrorException('Unprocessable Entity - You tried to enter an' + ' incorrect value.', context) + self.validate_response(context) + + return APIHelper.json_deserialize(context.response.raw_body) diff --git a/flowroutenumbersandmessaging/controllers/cnams_controller.py b/flowroutenumbersandmessaging/controllers/cnams_controller.py index 4a5c88c..bf84cd1 100644 --- a/flowroutenumbersandmessaging/controllers/cnams_controller.py +++ b/flowroutenumbersandmessaging/controllers/cnams_controller.py @@ -3,16 +3,15 @@ """ flowroutenumbersandmessaging.controllers.cnams_controller - This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ). + This file was automatically generated by APIMATIC v2.0 (https://apimatic.io). """ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth -from ..exceptions.error_exception import ErrorException from .numbers_controller import NumbersController + class CNAMsController(BaseController): """A Controller to access Endpoints in the @@ -57,23 +56,16 @@ class CNAMsController(BaseController): if is_approved is not None: _query_parameters['is_approved'] = is_approved - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def get_cnam(self, cnam_id): """Does a GET request to /v2/cnams/. @@ -101,19 +93,11 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) - def search_cnams(self, limit=10, offset=0, starts_with=None, contains=None, ends_with=None): + def search_cnams(self, limit=10, offset=0, starts_with=None, contains=None, + ends_with=None): """Does a GET request to /v2/cnams?. Searches for CNAM Records that match the criteria @@ -125,9 +109,15 @@ class CNAMsController(BaseController): specified value. For example, if you have 4 cnam records and you entered 1 as your offset value, then only 3 of your cnams will be displayed in the response. + starts_with (string, optional): matching value must start with + these characters + contains (string, optional): matching value must contain these + characters + ends_with (string, optional): matching value must end with + these characters Returns: - mixed: Response from the API. A JSON object of of an E911 record + mixed: Response from the API. A JSON object of of an CNAM record that satisfy your search criteria. Raises: @@ -155,17 +145,8 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def create_cnam_record(self, value): """Does a POST request to /v2/cnams. @@ -194,6 +175,7 @@ class CNAMsController(BaseController): _query_builder = Configuration.base_uri _query_builder += '/v2/cnams' _query_url = APIHelper.clean_url(_query_builder) + # Prepare headers _headers = { 'accept': 'application/json' @@ -202,21 +184,13 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, parameters=body) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def associate_cnam(self, cnam_id, phone_number): # first, verify the number belongs to the user - did = NumbersController().list_account_phone_numbers(contains=phone_number) + did = NumbersController().list_account_phone_numbers( + contains=phone_number) if did is None: error_string = "Error, this phone number does not belong to you." @@ -226,8 +200,10 @@ class CNAMsController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri - _query_builder += '/v2/numbers/{}/relationships/cnam/{}'.format(did, cnam_id) + _query_builder += '/v2/numbers/{}/relationships/cnam/{}'.format(did, + cnam_id) _query_url = APIHelper.clean_url(_query_builder) + # Prepare headers _headers = { 'accept': 'application/json' @@ -235,21 +211,13 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.patch(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def unassociate_cnam(self, phone_number): # first, verify the number belongs to the user - did = NumbersController().list_account_phone_numbers(contains=phone_number) + did = NumbersController().list_account_phone_numbers( + contains=phone_number) if did is None: error_string = "Error, this phone number does not belong to you." @@ -268,17 +236,8 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def remove_cnam(self, cnam_id): # Prepare query URL @@ -292,14 +251,5 @@ class CNAMsController(BaseController): # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/controllers/e911s_controller.py b/flowroutenumbersandmessaging/controllers/e911s_controller.py index 83309c6..50861f8 100644 --- a/flowroutenumbersandmessaging/controllers/e911s_controller.py +++ b/flowroutenumbersandmessaging/controllers/e911s_controller.py @@ -3,14 +3,12 @@ """ flowroutenumbersandmessaging.controllers.e911s_controller - This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ). + This file was automatically generated by APIMATIC v2.0 (https://apimatic.io) """ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth -from ..exceptions.error_exception import ErrorException class E911sController(BaseController): @@ -57,23 +55,15 @@ class E911sController(BaseController): 'offset': offset, 'state': state } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def get_e911(self, e911_id): """Does a GET request to /v2/e911s/. @@ -101,17 +91,8 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def validate_address(self, label, @@ -149,19 +130,22 @@ class E911sController(BaseController): the request. """ - body = dict() - body['data'] = dict() - body['data']['type'] = 'e911' - body['data']['attributes'] = {} - body['data']['attributes']['label'] = label - body['data']['attributes']['first_name'] = first_name - body['data']['attributes']['last_name'] = last_name - body['data']['attributes']['street_name'] = street_name - body['data']['attributes']['street_number'] = street_number - body['data']['attributes']['city'] = city - body['data']['attributes']['state'] = state - body['data']['attributes']['country'] = country - body['data']['attributes']['zip'] = zipcode + body = { + 'data': { + 'type': 'e911', + 'attributes': { + 'label': label, + 'first_name': first_name, + 'last_name': last_name, + 'street_name': street_name, + 'street_number': street_number, + 'city': city, + 'state': state, + 'country': country, + 'zip': zipcode + } + } + } # Prepare query URL _query_builder = Configuration.base_uri @@ -177,19 +161,10 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, - parameters=APIHelper.json_serialize(body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) + parameters=APIHelper.json_serialize( + body)) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def create_address(self, label, @@ -203,7 +178,8 @@ class E911sController(BaseController): zipcode): """Does a POST request to /v2/e911s. - Creates an address record that can then be associated with 1 or more DIDs + Creates an address record that can then be associated + with 1 or more DIDs Args: label (string): the alias or friendly name of your entry @@ -214,10 +190,11 @@ class E911sController(BaseController): city (string): state (2 character string): country (string USA or Canada): - zip (string postal code) + zipcode (string postal code) Returns: - mixed: Response from the API. A JSON object containing the new record information. + mixed: Response from the API. A JSON object containing the new + record information. Raises: APIException: When an error occurs while fetching the data from @@ -226,20 +203,22 @@ class E911sController(BaseController): the request. """ - body = dict() - body['data'] = dict() - body['data']['type'] = 'e911' - body['data']['attributes'] = {} - body['data']['attributes']['label'] = label - body['data']['attributes']['first_name'] = first_name - body['data']['attributes']['last_name'] = last_name - body['data']['attributes']['street_name'] = street_name - body['data']['attributes']['street_number'] = street_number - body['data']['attributes']['city'] = city - body['data']['attributes']['state'] = state - body['data']['attributes']['country'] = country - body['data']['attributes']['zip'] = zipcode - print(body) + body = { + 'data': { + 'type': 'e911', + 'attributes': { + 'label': label, + 'first_name': first_name, + 'last_name': last_name, + 'street_name': street_name, + 'street_number': street_number, + 'city': city, + 'state': state, + 'country': country, + 'zip': zipcode + } + } + } # Prepare query URL _query_builder = Configuration.base_uri @@ -255,19 +234,10 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, - parameters=APIHelper.json_serialize(body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) + parameters=APIHelper.json_serialize( + body)) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def update_address(self, e911_id, @@ -298,7 +268,8 @@ class E911sController(BaseController): zipcode (string postal code, optional) Returns: - mixed: Response from the API. A JSON object containing the new record information. + mixed: Response from the API. A JSON object containing the + new record information. Raises: APIException: When an error occurs while fetching the data from @@ -310,9 +281,7 @@ class E911sController(BaseController): cur_record = self.get_e911(e911_id) record_data = cur_record - record_data['data']['attributes']['zip_code'] = str(record_data['data']['attributes']['zip']) - record_data['data']['attributes']['house_number'] = str(record_data['data']['attributes']['street_number']) - + # Only update the fields specified if label is not None: record_data['data']['attributes']['label'] = label if first_name is not None: @@ -322,8 +291,8 @@ class E911sController(BaseController): if street_name is not None: record_data['data']['attributes']['street_name'] = street_name if street_number is not None: - record_data['data']['attributes']['street_number'] = str(street_number) - record_data['data']['attributes']['house_number'] = str(street_number) + record_data['data']['attributes']['street_number'] = \ + str(street_number) if city is not None: record_data['data']['attributes']['city'] = city if state is not None: @@ -340,8 +309,6 @@ class E911sController(BaseController): record_data['data']['attributes'].pop('address_type', None) record_data['data']['attributes'].pop('address_type_number', None) - print(record_data) - # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/e911s/{}'.format(e911_id) @@ -353,25 +320,16 @@ class E911sController(BaseController): _headers = { 'accept': 'application/json' } - print("Calling {}".format(_query_url)) + # Prepare and execute request _request = self.http_client.patch(_query_url, headers=_headers, - parameters=APIHelper.json_serialize(record_data)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) + parameters=APIHelper.json_serialize( + record_data)) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def delete_address(self, e911_id): - """Does a DELETE request to /v2/e911s/. + """Performs a DELETE request to /v2/e911s/. Removes the existing address record and any associations it may have @@ -395,7 +353,6 @@ class E911sController(BaseController): # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) - print("Calling {}".format(_query_url)) # Prepare headers _headers = { @@ -404,18 +361,8 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def associate(self, e911_id, did): """Does a PATCH request to /v2/numbers//relationships/e911s/. @@ -439,7 +386,8 @@ class E911sController(BaseController): """ # Prepare query URL _query_builder = Configuration.base_uri - _query_builder += '/v2/numbers/{}/relationships/e911s/{}'.format(did, e911_id) + _query_builder += '/v2/numbers/{}/relationships/e911s/{}'.format(did, + e911_id) # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) @@ -448,22 +396,11 @@ class E911sController(BaseController): _headers = { 'accept': 'application/json' } - print("Calling {}".format(_query_url)) # Prepare and execute request _request = self.http_client.patch(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def disconnect(self, did): """Does a DELETE request to /v2/numbers//relationships/e911s. @@ -490,7 +427,6 @@ class E911sController(BaseController): # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) - print("Calling {}".format(_query_url)) # Prepare headers _headers = { @@ -499,18 +435,8 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def list_dids_for_e911(self, e911_id): """Does a GET request to /v2/e911s//relationships/numbers @@ -537,14 +463,6 @@ class E911sController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) + return self.handle_request_and_response(_request) - return APIHelper.json_deserialize(_context.response.raw_body) diff --git a/flowroutenumbersandmessaging/controllers/messages_controller.py b/flowroutenumbersandmessaging/controllers/messages_controller.py index d0f760a..a2a66bb 100644 --- a/flowroutenumbersandmessaging/controllers/messages_controller.py +++ b/flowroutenumbersandmessaging/controllers/messages_controller.py @@ -9,21 +9,20 @@ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth from ..models.mdr_2 import MDR2 -from ..exceptions.error_exception import ErrorException import json class MessagesController(BaseController): - """A Controller to access Endpoints in the flowroutenumbersandmessaging API.""" + """A Controller to access Endpoints in the + flowroutenumbersandmessaging API.""" def look_up_a_set_of_messages(self, - start_date, - end_date=None, - limit=None, - offset=None): + start_date, + end_date=None, + limit=None, + offset=None): """Does a GET request to /v2.1/messages. Retrieves a list of Message Detail Records (MDRs) within a specified @@ -57,7 +56,7 @@ class MessagesController(BaseController): parsed_end_date = None if end_date is not None: - parsed_end_date = APIHelper.RFC3339DateTime(end_date) + parsed_end_date = APIHelper.RFC3339DateTime(end_date) # Prepare query URL _query_builder = Configuration.base_uri @@ -68,8 +67,10 @@ class MessagesController(BaseController): 'limit': limit, 'offset': offset } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers @@ -79,18 +80,8 @@ class MessagesController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def look_up_a_message_detail_record(self, id): @@ -118,9 +109,10 @@ class MessagesController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2.1/messages/{id}' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers @@ -130,18 +122,8 @@ class MessagesController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def send_a_message(self, body): """Does a POST request to /v2.1/messages. @@ -175,23 +157,11 @@ class MessagesController(BaseController): } # Prepare and execute request - _request = self.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(json.loads(body))) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 403: - raise ErrorException('Forbidden – You don\'t have permission to access this resource.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - elif _context.response.status_code == 422: - raise ErrorException('Unprocessable Entity - You tried to enter an incorrect value.', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + _request = self.http_client.post(_query_url, + headers=_headers, + parameters=APIHelper.json_serialize(json.loads(body))) + + return self.handle_request_and_response(_request) def set_account_level_sms_callback(self, url): """Does a PUT request to /v2.1/messages/sms_callback. @@ -234,22 +204,8 @@ class MessagesController(BaseController): _request = self.http_client.put(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 403: - raise ErrorException('Forbidden – You don\'t have permission to access this resource.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - elif _context.response.status_code == 422: - raise ErrorException('Unprocessable Entity - You tried to enter an incorrect value.', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + + return self.handle_request_and_response(_request) def set_account_level_mms_callback(self, url): """Does a PUT request to /v2.1/messages/mms_callback. @@ -293,29 +249,8 @@ class MessagesController(BaseController): headers=_headers, parameters=APIHelper.json_serialize( body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException( - 'Unauthorized – There was an issue with your API credentials.', - _context) - elif _context.response.status_code == 403: - raise ErrorException( - 'Forbidden – You don\'t have permission to access this resource.', - _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', - _context) - elif _context.response.status_code == 422: - raise ErrorException( - 'Unprocessable Entity - You tried to enter an incorrect value.', - _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + + return self.handle_request_and_response(_request) def set_account_level_dlr_callback(self, url): """Does a PUT request to /v2.1/messages/dlr_callback. @@ -359,26 +294,5 @@ class MessagesController(BaseController): headers=_headers, parameters=APIHelper.json_serialize( body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException( - 'Unauthorized – There was an issue with your API credentials.', - _context) - elif _context.response.status_code == 403: - raise ErrorException( - 'Forbidden – You don\'t have permission to access this resource.', - _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', - _context) - elif _context.response.status_code == 422: - raise ErrorException( - 'Unprocessable Entity - You tried to enter an incorrect value.', - _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + + return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/controllers/numbers_controller.py b/flowroutenumbersandmessaging/controllers/numbers_controller.py index 4acc5c9..64de4ab 100644 --- a/flowroutenumbersandmessaging/controllers/numbers_controller.py +++ b/flowroutenumbersandmessaging/controllers/numbers_controller.py @@ -9,15 +9,13 @@ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth from ..models.number_26 import Number26 -from ..exceptions.error_exception import ErrorException -from ..exceptions.api_exception import APIException class NumbersController(BaseController): - """A Controller to access Endpoints in the flowroutenumbersandmessaging API.""" + """A Controller to access Endpoints in the + flowroutenumbersandmessaging API.""" def list_available_exchange_codes(self, limit=None, @@ -42,8 +40,9 @@ class NumbersController(BaseController): area code. Returns: - mixed: Response from the API. A JSON object of Central Office (exchange) codes containing - purchasable phone numbers that satisfy your search criteria. + mixed: Response from the API. A JSON object of Central Office + (exchange) codes containing + purchasable phone numbers that satisfy your search criteria. Raises: APIException: When an error occurs while fetching the data from @@ -62,23 +61,16 @@ class NumbersController(BaseController): 'max_setup_cost': max_setup_cost, 'areacode': areacode } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def list_available_area_codes(self, limit=None, @@ -119,25 +111,16 @@ class NumbersController(BaseController): 'offset': offset, 'max_setup_cost': max_setup_cost } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - elif _context.response.status_code == 422: - raise ErrorException('Invalid Query', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def search_for_purchasable_phone_numbers(self, starts_with=None, @@ -194,8 +177,10 @@ class NumbersController(BaseController): 'rate_center': rate_center, 'state': state } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers @@ -205,25 +190,15 @@ class NumbersController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def list_account_phone_numbers(self, - starts_with=None, - ends_with=None, - contains=None, - limit=None, - offset=None): + starts_with=None, + ends_with=None, + contains=None, + limit=None, + offset=None): """Does a GET request to /v2/numbers. Returns a list of all phone numbers currently on your Flowroute @@ -267,8 +242,10 @@ class NumbersController(BaseController): 'limit': limit, 'offset': offset } - _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, - _query_parameters, Configuration.array_serialization) + _query_builder = APIHelper.append_url_with_query_parameters( + _query_builder, + _query_parameters, + Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers @@ -278,21 +255,10 @@ class NumbersController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) - def purchase_a_phone_number(self, - id): + def purchase_a_phone_number(self, id): """Does a POST request to /v2/numbers/{id}. Lets you purchase a phone number from available Flowroute inventory. @@ -315,9 +281,10 @@ class NumbersController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{id}' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) @@ -328,21 +295,10 @@ class NumbersController(BaseController): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) - def list_phone_number_details(self, - id): + def list_phone_number_details(self, id): """Does a GET request to /v2/numbers/{id}. Lists all of the information associated with any of the phone numbers @@ -368,9 +324,10 @@ class NumbersController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{id}' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers @@ -380,18 +337,8 @@ class NumbersController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise APIException('Unauthorized', _context) - elif _context.response.status_code == 404: - raise APIException('Not Found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def release_a_did(self, id): """Does a DELETE request to /v2/numbers/{id}. @@ -410,15 +357,15 @@ class NumbersController(BaseController): the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. - """ # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{id}' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) @@ -430,18 +377,8 @@ class NumbersController(BaseController): # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def set_did_alias(self, id, alias): """Does a PATCH request to /v2/numbers/{id}. @@ -467,9 +404,10 @@ class NumbersController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{id}' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) @@ -487,18 +425,8 @@ class NumbersController(BaseController): _request = self.http_client.patch(_query_url, headers=_headers, parameters=APIHelper.json_serialize( body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def set_did_callback(self, id, url): """Does a POST request to /v2/numbers/{id}/relationships/dlr_callback. @@ -524,9 +452,10 @@ class NumbersController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{id}/relationships/dlr_callback' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { - 'id': id - }) + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { + 'id': id + }) # Return appropriate type _query_url = APIHelper.clean_url(_query_builder) @@ -547,15 +476,5 @@ class NumbersController(BaseController): _request = self.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize( body)) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/controllers/porting_controller.py b/flowroutenumbersandmessaging/controllers/porting_controller.py index 9b37e49..d72d691 100644 --- a/flowroutenumbersandmessaging/controllers/porting_controller.py +++ b/flowroutenumbersandmessaging/controllers/porting_controller.py @@ -9,9 +9,7 @@ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth -from ..exceptions.error_exception import ErrorException -from .numbers_controller import NumbersController + class PortingController(BaseController): @@ -52,104 +50,5 @@ class PortingController(BaseController): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, parameters=body) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) - - def associate_cnam(self, cnam_id, phone_number): - # first, verify the number belongs to the user - did = NumbersController().list_account_phone_numbers(contains=phone_number) - - if did is None: - error_string = "Error, this phone number does not belong to you." - return error_string - - did = did['data'][0]['id'] - - # Prepare query URL - _query_builder = Configuration.base_uri - _query_builder += '/v2/numbers/{}/relationships/cnam/{}'.format(did, cnam_id) - _query_url = APIHelper.clean_url(_query_builder) - # Prepare headers - _headers = { - 'accept': 'application/json' - } - - # Prepare and execute request - _request = self.http_client.patch(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) - - def unassociate_cnam(self, phone_number): - # first, verify the number belongs to the user - did = NumbersController().list_account_phone_numbers(contains=phone_number) - - if did is None: - error_string = "Error, this phone number does not belong to you." - return error_string - - did = did['data'][0]['id'] - - # Prepare query URL - _query_builder = Configuration.base_uri - _query_builder += '/v2/numbers/{}/relationships/cnam'.format(did) - _query_url = APIHelper.clean_url(_query_builder) - # Prepare headers - _headers = { - 'accept': 'application/json' - } - - # Prepare and execute request - _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - - return APIHelper.json_deserialize(_context.response.raw_body) - - def remove_cnam(self, cnam_id): - # Prepare query URL - _query_builder = Configuration.base_uri - _query_builder += '/v2/cnams/{}'.format(cnam_id) - _query_url = APIHelper.clean_url(_query_builder) - # Prepare headers - _headers = { - 'accept': 'application/json' - } - - # Prepare and execute request - _request = self.http_client.delete(_query_url, headers=_headers) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/controllers/routes_controller.py b/flowroutenumbersandmessaging/controllers/routes_controller.py index d275ea7..6559d7e 100644 --- a/flowroutenumbersandmessaging/controllers/routes_controller.py +++ b/flowroutenumbersandmessaging/controllers/routes_controller.py @@ -9,9 +9,6 @@ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration -from ..http.auth.basic_auth import BasicAuth -from ..exceptions.error_exception import ErrorException -from ..exceptions.api_exception import APIException import json @@ -56,23 +53,8 @@ class RoutesController(BaseController): _request = self.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(json.loads(body))) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('401 Unauthorized – ' - 'There was an issue with your API credentials.', _context) - elif _context.response.status_code == 403: - raise ErrorException('403 Forbidden – ' - 'The server understood the request but refuses to authorize it.', _context) - elif _context.response.status_code == 404: - raise ErrorException('404 The specified resource was not found', _context) - self.validate_response(_context) - - - # Return appropriate type - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def list_inbound_routes(self, limit=None, @@ -116,17 +98,8 @@ class RoutesController(BaseController): # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise APIException('Unauthorized', _context) - elif _context.response.status_code == 404: - raise APIException('Not Found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request) def update_primary_voice_route(self, number_id, body): """Does a PATCH request to /v2/numbers/{number_id}/relationships/primary_route. @@ -163,15 +136,8 @@ class RoutesController(BaseController): # Prepare and execute request _request = self.http_client.patch(_query_url, parameters=APIHelper.json_serialize(json.loads(body))) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) + return self.handle_request_and_response(_request) def update_failover_voice_route(self, number_id, @@ -203,39 +169,24 @@ class RoutesController(BaseController): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{number_id}/relationships/failover_route' - _query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { + _query_builder = APIHelper.append_url_with_template_parameters( + _query_builder, { 'number_id': number_id }) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.patch(_query_url, parameters=APIHelper.json_serialize(json.loads(body))) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) - elif _context.response.status_code == 404: - raise ErrorException('The specified resource was not found', _context) - self.validate_response(_context) + return self.handle_request_and_response(_request) def list_edge_strategies(self): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/routes/edge_strategies' _query_url = APIHelper.clean_url(_query_builder) - print("Query is : {}".format(_query_url)) + # Prepare and execute request _request = self.http_client.get(_query_url) - BasicAuth.apply(_request) - _context = self.execute_request(_request) - - # Endpoint and global error handling using HTTP status codes. - if _context.response.status_code == 401: - raise APIException('Unauthorized', _context) - elif _context.response.status_code == 404: - raise APIException('Not Found', _context) - self.validate_response(_context) - return APIHelper.json_deserialize(_context.response.raw_body) + return self.handle_request_and_response(_request)