Merge pull request #5 from flowroute/code_cleanup

Code cleanup
e911_addr
Chris Lacina 7 years ago committed by GitHub
commit 9d174d2cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      cnam_demo.py
  2. 24
      flowroutenumbersandmessaging/controllers/base_controller.py
  3. 108
      flowroutenumbersandmessaging/controllers/cnams_controller.py
  4. 212
      flowroutenumbersandmessaging/controllers/e911s_controller.py
  5. 128
      flowroutenumbersandmessaging/controllers/messages_controller.py
  6. 163
      flowroutenumbersandmessaging/controllers/numbers_controller.py
  7. 105
      flowroutenumbersandmessaging/controllers/porting_controller.py
  8. 65
      flowroutenumbersandmessaging/controllers/routes_controller.py

@ -1,8 +1,15 @@
#!/usr/bin/env python #!/usr/bin/env python
import pprint import pprint
import os import os
import random
import string
from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import FlowroutenumbersandmessagingClient 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 # 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_user_name = os.environ.get('FR_ACCESS_KEY')
basic_auth_password = os.environ.get('FR_SECRET_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) client = FlowroutenumbersandmessagingClient(basic_auth_user_name, basic_auth_password)
numbers_controller = client.numbers numbers_controller = client.numbers
cnams_controller = client.cnams cnams_controller = client.cnams
cnam_id = None
print("--List CNAM Records") print("--List CNAM Records")
limit = 10 limit = 10
@ -20,32 +26,42 @@ offset = None
result = cnams_controller.list_cnams(limit, offset) result = cnams_controller.list_cnams(limit, offset)
pprint.pprint(result) pprint.pprint(result)
print("\n--List Approved CNAM Records") print("\n--List Approved CNAM Records")
result = cnams_controller.list_cnams(limit, offset, is_approved=True) result = cnams_controller.list_cnams(limit, offset, is_approved=True)
pprint.pprint(result) pprint.pprint(result)
if len(result['data']):
cnam_id = result['data'][0]['id'] cnam_id = result['data'][0]['id']
print("\n--List CNAM Detail") print("\n--List CNAM Detail")
cnam_id = result['data'][0]['id']
result = cnams_controller.get_cnam(cnam_id) result = cnams_controller.get_cnam(cnam_id)
pprint.pprint(result) pprint.pprint(result)
if len(result['data']):
cnam_id = result['data'][0]['id']
print("\n--Search for CNAM Record") print("\n--Search for CNAM Record")
result = cnams_controller.search_cnams(contains='CHRIS') result = cnams_controller.search_cnams(contains='CHRIS')
pprint.pprint(result) pprint.pprint(result)
# print("\n--Create a CNAM Record") print("\n--Create a CNAM Record")
# result = cnams_controller.create_cnam_record('CJL') cnam_value = 'FR ' + random_generator()
# pprint.pprint(result) result = cnams_controller.create_cnam_record(cnam_value)
# cnam_id = result['data']['id'] 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") print("\n--Associate a CNAM Record to a DID")
result = cnams_controller.associate_cnam(cnam_id, '12066417659') our_numbers = numbers_controller.list_account_phone_numbers()
did_id = our_numbers['data'][0]['id']
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) pprint.pprint(result)
print("\n--Unassociate a CNAM Record from a DID") print("\n--Unassociate a CNAM Record from a DID")
result = cnams_controller.unassociate_cnam('12066417659') result = cnams_controller.unassociate_cnam(did_id)
pprint.pprint(result) pprint.pprint(result)
print("\n--Remove a CNAM Record from your account") print("\n--Remove a CNAM Record from your account")

@ -10,7 +10,9 @@
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..http.http_context import HttpContext from ..http.http_context import HttpContext
from ..http.requests_client import RequestsClient from ..http.requests_client import RequestsClient
from ..exceptions.error_exception import ErrorException
from ..exceptions.api_exception import APIException from ..exceptions.api_exception import APIException
from ..http.auth.basic_auth import BasicAuth
class BaseController(object): class BaseController(object):
@ -98,3 +100,25 @@ class BaseController(object):
if (context.response.status_code < 200) or \ if (context.response.status_code < 200) or \
(context.response.status_code > 208): (context.response.status_code > 208):
raise APIException('HTTP response not OK.', context) 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)

@ -9,10 +9,9 @@
from .base_controller import BaseController from .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration from ..configuration import Configuration
from ..http.auth.basic_auth import BasicAuth
from ..exceptions.error_exception import ErrorException
from .numbers_controller import NumbersController from .numbers_controller import NumbersController
class CNAMsController(BaseController): class CNAMsController(BaseController):
"""A Controller to access Endpoints in the """A Controller to access Endpoints in the
@ -57,23 +56,16 @@ class CNAMsController(BaseController):
if is_approved is not None: if is_approved is not None:
_query_parameters['is_approved'] = is_approved _query_parameters['is_approved'] = is_approved
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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): def get_cnam(self, cnam_id):
"""Does a GET request to /v2/cnams/<cnam_id>. """Does a GET request to /v2/cnams/<cnam_id>.
@ -101,19 +93,11 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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?. """Does a GET request to /v2/cnams?.
Searches for CNAM Records that match the criteria 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 specified value. For example, if you have 4 cnam records and
you entered 1 as your offset value, then only 3 of your cnams you entered 1 as your offset value, then only 3 of your cnams
will be displayed in the response. 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: 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. that satisfy your search criteria.
Raises: Raises:
@ -155,17 +145,8 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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): def create_cnam_record(self, value):
"""Does a POST request to /v2/cnams. """Does a POST request to /v2/cnams.
@ -194,6 +175,7 @@ class CNAMsController(BaseController):
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/cnams' _query_builder += '/v2/cnams'
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare headers # Prepare headers
_headers = { _headers = {
'accept': 'application/json' 'accept': 'application/json'
@ -202,21 +184,13 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers, _request = self.http_client.post(_query_url, headers=_headers,
parameters=body) parameters=body)
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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): def associate_cnam(self, cnam_id, phone_number):
# first, verify the number belongs to the user # 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: if did is None:
error_string = "Error, this phone number does not belong to you." error_string = "Error, this phone number does not belong to you."
@ -226,8 +200,10 @@ class CNAMsController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _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) _query_url = APIHelper.clean_url(_query_builder)
# Prepare headers # Prepare headers
_headers = { _headers = {
'accept': 'application/json' 'accept': 'application/json'
@ -235,21 +211,13 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.patch(_query_url, headers=_headers) _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): def unassociate_cnam(self, phone_number):
# first, verify the number belongs to the user # 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: if did is None:
error_string = "Error, this phone number does not belong to you." error_string = "Error, this phone number does not belong to you."
@ -268,17 +236,8 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.delete(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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): def remove_cnam(self, cnam_id):
# Prepare query URL # Prepare query URL
@ -292,14 +251,5 @@ class CNAMsController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.delete(_query_url, headers=_headers) _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)

@ -3,14 +3,12 @@
""" """
flowroutenumbersandmessaging.controllers.e911s_controller 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 .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration from ..configuration import Configuration
from ..http.auth.basic_auth import BasicAuth
from ..exceptions.error_exception import ErrorException
class E911sController(BaseController): class E911sController(BaseController):
@ -57,23 +55,15 @@ class E911sController(BaseController):
'offset': offset, 'offset': offset,
'state': state 'state': state
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder, _query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _request = self.http_client.get(_query_url)
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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 get_e911(self, e911_id): def get_e911(self, e911_id):
"""Does a GET request to /v2/e911s/<e911_id>. """Does a GET request to /v2/e911s/<e911_id>.
@ -101,17 +91,8 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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, def validate_address(self,
label, label,
@ -149,19 +130,22 @@ class E911sController(BaseController):
the request. the request.
""" """
body = dict() body = {
body['data'] = dict() 'data': {
body['data']['type'] = 'e911' 'type': 'e911',
body['data']['attributes'] = {} 'attributes': {
body['data']['attributes']['label'] = label 'label': label,
body['data']['attributes']['first_name'] = first_name 'first_name': first_name,
body['data']['attributes']['last_name'] = last_name 'last_name': last_name,
body['data']['attributes']['street_name'] = street_name 'street_name': street_name,
body['data']['attributes']['street_number'] = street_number 'street_number': street_number,
body['data']['attributes']['city'] = city 'city': city,
body['data']['attributes']['state'] = state 'state': state,
body['data']['attributes']['country'] = country 'country': country,
body['data']['attributes']['zip'] = zipcode 'zip': zipcode
}
}
}
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
@ -177,19 +161,10 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers, _request = self.http_client.post(_query_url, headers=_headers,
parameters=APIHelper.json_serialize(body)) parameters=APIHelper.json_serialize(
BasicAuth.apply(_request) body))
_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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def create_address(self, def create_address(self,
label, label,
@ -203,7 +178,8 @@ class E911sController(BaseController):
zipcode): zipcode):
"""Does a POST request to /v2/e911s. """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: Args:
label (string): the alias or friendly name of your entry label (string): the alias or friendly name of your entry
@ -214,10 +190,11 @@ class E911sController(BaseController):
city (string): city (string):
state (2 character string): state (2 character string):
country (string USA or Canada): country (string USA or Canada):
zip (string postal code) zipcode (string postal code)
Returns: 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: Raises:
APIException: When an error occurs while fetching the data from APIException: When an error occurs while fetching the data from
@ -226,20 +203,22 @@ class E911sController(BaseController):
the request. the request.
""" """
body = dict() body = {
body['data'] = dict() 'data': {
body['data']['type'] = 'e911' 'type': 'e911',
body['data']['attributes'] = {} 'attributes': {
body['data']['attributes']['label'] = label 'label': label,
body['data']['attributes']['first_name'] = first_name 'first_name': first_name,
body['data']['attributes']['last_name'] = last_name 'last_name': last_name,
body['data']['attributes']['street_name'] = street_name 'street_name': street_name,
body['data']['attributes']['street_number'] = street_number 'street_number': street_number,
body['data']['attributes']['city'] = city 'city': city,
body['data']['attributes']['state'] = state 'state': state,
body['data']['attributes']['country'] = country 'country': country,
body['data']['attributes']['zip'] = zipcode 'zip': zipcode
print(body) }
}
}
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
@ -255,19 +234,10 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers, _request = self.http_client.post(_query_url, headers=_headers,
parameters=APIHelper.json_serialize(body)) parameters=APIHelper.json_serialize(
BasicAuth.apply(_request) body))
_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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def update_address(self, def update_address(self,
e911_id, e911_id,
@ -298,7 +268,8 @@ class E911sController(BaseController):
zipcode (string postal code, optional) zipcode (string postal code, optional)
Returns: 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: Raises:
APIException: When an error occurs while fetching the data from APIException: When an error occurs while fetching the data from
@ -310,9 +281,7 @@ class E911sController(BaseController):
cur_record = self.get_e911(e911_id) cur_record = self.get_e911(e911_id)
record_data = cur_record record_data = cur_record
record_data['data']['attributes']['zip_code'] = str(record_data['data']['attributes']['zip']) # Only update the fields specified
record_data['data']['attributes']['house_number'] = str(record_data['data']['attributes']['street_number'])
if label is not None: if label is not None:
record_data['data']['attributes']['label'] = label record_data['data']['attributes']['label'] = label
if first_name is not None: if first_name is not None:
@ -322,8 +291,8 @@ class E911sController(BaseController):
if street_name is not None: if street_name is not None:
record_data['data']['attributes']['street_name'] = street_name record_data['data']['attributes']['street_name'] = street_name
if street_number is not None: if street_number is not None:
record_data['data']['attributes']['street_number'] = str(street_number) record_data['data']['attributes']['street_number'] = \
record_data['data']['attributes']['house_number'] = str(street_number) str(street_number)
if city is not None: if city is not None:
record_data['data']['attributes']['city'] = city record_data['data']['attributes']['city'] = city
if state is not None: 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', None)
record_data['data']['attributes'].pop('address_type_number', None) record_data['data']['attributes'].pop('address_type_number', None)
print(record_data)
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/e911s/{}'.format(e911_id) _query_builder += '/v2/e911s/{}'.format(e911_id)
@ -353,25 +320,16 @@ class E911sController(BaseController):
_headers = { _headers = {
'accept': 'application/json' 'accept': 'application/json'
} }
print("Calling {}".format(_query_url))
# Prepare and execute request # Prepare and execute request
_request = self.http_client.patch(_query_url, headers=_headers, _request = self.http_client.patch(_query_url, headers=_headers,
parameters=APIHelper.json_serialize(record_data)) parameters=APIHelper.json_serialize(
BasicAuth.apply(_request) record_data))
_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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def delete_address(self, e911_id): def delete_address(self, e911_id):
"""Does a DELETE request to /v2/e911s/<e911_id>. """Performs a DELETE request to /v2/e911s/<e911_id>.
Removes the existing address record and any associations it may have Removes the existing address record and any associations it may have
@ -395,7 +353,6 @@ class E911sController(BaseController):
# Return appropriate type # Return appropriate type
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
print("Calling {}".format(_query_url))
# Prepare headers # Prepare headers
_headers = { _headers = {
@ -404,18 +361,8 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.delete(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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)
def associate(self, e911_id, did): def associate(self, e911_id, did):
"""Does a PATCH request to /v2/numbers/<did>/relationships/e911s/<e911_id>. """Does a PATCH request to /v2/numbers/<did>/relationships/e911s/<e911_id>.
@ -439,7 +386,8 @@ class E911sController(BaseController):
""" """
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _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 # Return appropriate type
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
@ -448,22 +396,11 @@ class E911sController(BaseController):
_headers = { _headers = {
'accept': 'application/json' 'accept': 'application/json'
} }
print("Calling {}".format(_query_url))
# Prepare and execute request # Prepare and execute request
_request = self.http_client.patch(_query_url, headers=_headers) _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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def disconnect(self, did): def disconnect(self, did):
"""Does a DELETE request to /v2/numbers/<did>/relationships/e911s. """Does a DELETE request to /v2/numbers/<did>/relationships/e911s.
@ -490,7 +427,6 @@ class E911sController(BaseController):
# Return appropriate type # Return appropriate type
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
print("Calling {}".format(_query_url))
# Prepare headers # Prepare headers
_headers = { _headers = {
@ -499,18 +435,8 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.delete(_query_url, headers=_headers) _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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def list_dids_for_e911(self, e911_id): def list_dids_for_e911(self, e911_id):
"""Does a GET request to /v2/e911s/<e911_id>/relationships/numbers """Does a GET request to /v2/e911s/<e911_id>/relationships/numbers
@ -537,14 +463,6 @@ class E911sController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _request = self.http_client.get(_query_url)
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)

@ -9,15 +9,14 @@
from .base_controller import BaseController from .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration from ..configuration import Configuration
from ..http.auth.basic_auth import BasicAuth
from ..models.mdr_2 import MDR2 from ..models.mdr_2 import MDR2
from ..exceptions.error_exception import ErrorException
import json import json
class MessagesController(BaseController): 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, def look_up_a_set_of_messages(self,
start_date, start_date,
@ -68,8 +67,10 @@ class MessagesController(BaseController):
'limit': limit, 'limit': limit,
'offset': offset 'offset': offset
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare headers # Prepare headers
@ -79,18 +80,8 @@ class MessagesController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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)
def look_up_a_message_detail_record(self, def look_up_a_message_detail_record(self,
id): id):
@ -118,7 +109,8 @@ class MessagesController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2.1/messages/{id}' _query_builder += '/v2.1/messages/{id}'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
@ -130,18 +122,8 @@ class MessagesController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url, headers=_headers) _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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def send_a_message(self, body): def send_a_message(self, body):
"""Does a POST request to /v2.1/messages. """Does a POST request to /v2.1/messages.
@ -175,23 +157,11 @@ class MessagesController(BaseController):
} }
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(json.loads(body))) _request = self.http_client.post(_query_url,
BasicAuth.apply(_request) headers=_headers,
_context = self.execute_request(_request) parameters=APIHelper.json_serialize(json.loads(body)))
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)
def set_account_level_sms_callback(self, url): def set_account_level_sms_callback(self, url):
"""Does a PUT request to /v2.1/messages/sms_callback. """Does a PUT request to /v2.1/messages/sms_callback.
@ -234,22 +204,8 @@ class MessagesController(BaseController):
_request = self.http_client.put(_query_url, _request = self.http_client.put(_query_url,
headers=_headers, headers=_headers,
parameters=APIHelper.json_serialize(body)) parameters=APIHelper.json_serialize(body))
BasicAuth.apply(_request)
_context = self.execute_request(_request) return self.handle_request_and_response(_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)
def set_account_level_mms_callback(self, url): def set_account_level_mms_callback(self, url):
"""Does a PUT request to /v2.1/messages/mms_callback. """Does a PUT request to /v2.1/messages/mms_callback.
@ -293,29 +249,8 @@ class MessagesController(BaseController):
headers=_headers, headers=_headers,
parameters=APIHelper.json_serialize( parameters=APIHelper.json_serialize(
body)) body))
BasicAuth.apply(_request)
_context = self.execute_request(_request) return self.handle_request_and_response(_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)
def set_account_level_dlr_callback(self, url): def set_account_level_dlr_callback(self, url):
"""Does a PUT request to /v2.1/messages/dlr_callback. """Does a PUT request to /v2.1/messages/dlr_callback.
@ -359,26 +294,5 @@ class MessagesController(BaseController):
headers=_headers, headers=_headers,
parameters=APIHelper.json_serialize( parameters=APIHelper.json_serialize(
body)) body))
BasicAuth.apply(_request)
_context = self.execute_request(_request) return self.handle_request_and_response(_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)

@ -9,15 +9,13 @@
from .base_controller import BaseController from .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration from ..configuration import Configuration
from ..http.auth.basic_auth import BasicAuth
from ..models.number_26 import Number26 from ..models.number_26 import Number26
from ..exceptions.error_exception import ErrorException
from ..exceptions.api_exception import APIException
class NumbersController(BaseController): 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, def list_available_exchange_codes(self,
limit=None, limit=None,
@ -42,7 +40,8 @@ class NumbersController(BaseController):
area code. area code.
Returns: Returns:
mixed: Response from the API. A JSON object of Central Office (exchange) codes containing mixed: Response from the API. A JSON object of Central Office
(exchange) codes containing
purchasable phone numbers that satisfy your search criteria. purchasable phone numbers that satisfy your search criteria.
Raises: Raises:
@ -62,23 +61,16 @@ class NumbersController(BaseController):
'max_setup_cost': max_setup_cost, 'max_setup_cost': max_setup_cost,
'areacode': areacode 'areacode': areacode
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _request = self.http_client.get(_query_url)
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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 list_available_area_codes(self, def list_available_area_codes(self,
limit=None, limit=None,
@ -119,25 +111,16 @@ class NumbersController(BaseController):
'offset': offset, 'offset': offset,
'max_setup_cost': max_setup_cost 'max_setup_cost': max_setup_cost
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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, def search_for_purchasable_phone_numbers(self,
starts_with=None, starts_with=None,
@ -194,8 +177,10 @@ class NumbersController(BaseController):
'rate_center': rate_center, 'rate_center': rate_center,
'state': state 'state': state
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare headers # Prepare headers
@ -205,18 +190,8 @@ class NumbersController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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)
def list_account_phone_numbers(self, def list_account_phone_numbers(self,
starts_with=None, starts_with=None,
@ -267,8 +242,10 @@ class NumbersController(BaseController):
'limit': limit, 'limit': limit,
'offset': offset 'offset': offset
} }
_query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_builder = APIHelper.append_url_with_query_parameters(
_query_parameters, Configuration.array_serialization) _query_builder,
_query_parameters,
Configuration.array_serialization)
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare headers # Prepare headers
@ -278,21 +255,10 @@ class NumbersController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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 def purchase_a_phone_number(self, id):
return APIHelper.json_deserialize(_context.response.raw_body)
def purchase_a_phone_number(self,
id):
"""Does a POST request to /v2/numbers/{id}. """Does a POST request to /v2/numbers/{id}.
Lets you purchase a phone number from available Flowroute inventory. Lets you purchase a phone number from available Flowroute inventory.
@ -315,7 +281,8 @@ class NumbersController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{id}' _query_builder += '/v2/numbers/{id}'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
# Return appropriate type # Return appropriate type
@ -328,21 +295,10 @@ class NumbersController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers) _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 self.handle_request_and_response(_request)
return APIHelper.json_deserialize(_context.response.raw_body)
def list_phone_number_details(self, def list_phone_number_details(self, id):
id):
"""Does a GET request to /v2/numbers/{id}. """Does a GET request to /v2/numbers/{id}.
Lists all of the information associated with any of the phone numbers Lists all of the information associated with any of the phone numbers
@ -368,7 +324,8 @@ class NumbersController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{id}' _query_builder += '/v2/numbers/{id}'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
@ -380,18 +337,8 @@ class NumbersController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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)
def release_a_did(self, id): def release_a_did(self, id):
"""Does a DELETE request to /v2/numbers/{id}. """Does a DELETE request to /v2/numbers/{id}.
@ -410,13 +357,13 @@ class NumbersController(BaseController):
the remote API. This exception includes the HTTP Response the remote API. This exception includes the HTTP Response
code, an error message, and the HTTP body that was received in code, an error message, and the HTTP body that was received in
the request. the request.
""" """
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{id}' _query_builder += '/v2/numbers/{id}'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
@ -430,18 +377,8 @@ class NumbersController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.delete(_query_url, headers=_headers) _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. return self.handle_request_and_response(_request)
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)
def set_did_alias(self, id, alias): def set_did_alias(self, id, alias):
"""Does a PATCH request to /v2/numbers/{id}. """Does a PATCH request to /v2/numbers/{id}.
@ -467,7 +404,8 @@ class NumbersController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{id}' _query_builder += '/v2/numbers/{id}'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
# Return appropriate type # Return appropriate type
@ -487,18 +425,8 @@ class NumbersController(BaseController):
_request = self.http_client.patch(_query_url, headers=_headers, _request = self.http_client.patch(_query_url, headers=_headers,
parameters=APIHelper.json_serialize( parameters=APIHelper.json_serialize(
body)) body))
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)
def set_did_callback(self, id, url): def set_did_callback(self, id, url):
"""Does a POST request to /v2/numbers/{id}/relationships/dlr_callback. """Does a POST request to /v2/numbers/{id}/relationships/dlr_callback.
@ -524,7 +452,8 @@ class NumbersController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{id}/relationships/dlr_callback' _query_builder += '/v2/numbers/{id}/relationships/dlr_callback'
_query_builder = APIHelper.append_url_with_template_parameters(_query_builder, { _query_builder = APIHelper.append_url_with_template_parameters(
_query_builder, {
'id': id 'id': id
}) })
# Return appropriate type # Return appropriate type
@ -547,15 +476,5 @@ class NumbersController(BaseController):
_request = self.http_client.post(_query_url, headers=_headers, _request = self.http_client.post(_query_url, headers=_headers,
parameters=APIHelper.json_serialize( parameters=APIHelper.json_serialize(
body)) body))
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)

@ -9,9 +9,7 @@
from .base_controller import BaseController from .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration 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): class PortingController(BaseController):
@ -52,104 +50,5 @@ class PortingController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.post(_query_url, headers=_headers, _request = self.http_client.post(_query_url, headers=_headers,
parameters=body) 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)

@ -9,9 +9,6 @@
from .base_controller import BaseController from .base_controller import BaseController
from ..api_helper import APIHelper from ..api_helper import APIHelper
from ..configuration import Configuration 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 import json
@ -56,23 +53,8 @@ class RoutesController(BaseController):
_request = self.http_client.post(_query_url, _request = self.http_client.post(_query_url,
headers=_headers, headers=_headers,
parameters=APIHelper.json_serialize(json.loads(body))) parameters=APIHelper.json_serialize(json.loads(body)))
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)
def list_inbound_routes(self, def list_inbound_routes(self,
limit=None, limit=None,
@ -116,17 +98,8 @@ class RoutesController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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): def update_primary_voice_route(self, number_id, body):
"""Does a PATCH request to /v2/numbers/{number_id}/relationships/primary_route. """Does a PATCH request to /v2/numbers/{number_id}/relationships/primary_route.
@ -163,15 +136,8 @@ class RoutesController(BaseController):
# Prepare and execute request # Prepare and execute request
_request = self.http_client.patch(_query_url, _request = self.http_client.patch(_query_url,
parameters=APIHelper.json_serialize(json.loads(body))) parameters=APIHelper.json_serialize(json.loads(body)))
BasicAuth.apply(_request)
_context = self.execute_request(_request)
# Endpoint and global error handling using HTTP status codes. return self.handle_request_and_response(_request)
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)
def update_failover_voice_route(self, def update_failover_voice_route(self,
number_id, number_id,
@ -203,39 +169,24 @@ class RoutesController(BaseController):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/numbers/{number_id}/relationships/failover_route' _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 'number_id': number_id
}) })
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
# Prepare and execute request # Prepare and execute request
_request = self.http_client.patch(_query_url, parameters=APIHelper.json_serialize(json.loads(body))) _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. return self.handle_request_and_response(_request)
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)
def list_edge_strategies(self): def list_edge_strategies(self):
# Prepare query URL # Prepare query URL
_query_builder = Configuration.base_uri _query_builder = Configuration.base_uri
_query_builder += '/v2/routes/edge_strategies' _query_builder += '/v2/routes/edge_strategies'
_query_url = APIHelper.clean_url(_query_builder) _query_url = APIHelper.clean_url(_query_builder)
print("Query is : {}".format(_query_url))
# Prepare and execute request # Prepare and execute request
_request = self.http_client.get(_query_url) _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)

Loading…
Cancel
Save