Update E911 Demo and controller code. Demo can be run multiple times as it cleans up after itself.

e911_addr
Chris Lacina 7 years ago
parent 839c891ae0
commit 92efa99360
  1. 4
      demo.py
  2. 109
      e911_demo.py
  3. 36
      flowroutenumbersandmessaging/controllers/base_controller.py
  4. 41
      flowroutenumbersandmessaging/controllers/e911s_controller.py

@ -180,7 +180,7 @@ request_body = '{ \
"attributes": { \
"to": "' + str(mobile_number) + '", \
"from": "' + str(number_id) + '", \
"body": "' + unicode(body, "utf-8") + '", \
"body": "' + str(body) + '", \
"is_mms": "false" \
} \
} \
@ -205,7 +205,7 @@ request_body_with_dlr = '{ \
"attributes": { \
"to": "' + str(mobile_number) + '", \
"from": "' + str(number_id) + '", \
"body": "' + unicode(body, "utf-8") + '", \
"body": "' + str(body) + '", \
"is_mms": "false", \
"dlr_callback": "http://httpbin.org/status/:code" \
} \

@ -8,7 +8,8 @@ basic_auth_user_name = os.environ.get('FR_ACCESS_KEY')
basic_auth_password = os.environ.get('FR_SECRET_KEY')
# Instantiate API client and create controllers for Numbers and E911s
client = FlowroutenumbersandmessagingClient(basic_auth_user_name, basic_auth_password)
client = FlowroutenumbersandmessagingClient(basic_auth_user_name,
basic_auth_password)
numbers_controller = client.numbers
e911s_controller = client.e911s
@ -19,19 +20,6 @@ offset = None
result = e911s_controller.list_e911s(limit, offset)
pprint.pprint(result)
print("--Validate an Address")
result = e911s_controller.validate_address(
label="Test Address",
first_name="Chris",
last_name="Smith",
street_name="3rd Ave",
street_number="1182",
city="Seattle",
state="WA",
country="USA",
zip="98101")
pprint.pprint(result)
e911_id = None
# If the user has any E911 records, pull one up
for e in result['data']:
@ -39,29 +27,55 @@ for e in result['data']:
break
if e911_id:
print("--Get Details for a specific E911 Record")
print("\n--Get Details for a specific E911 Record")
result = e911s_controller.get_e911(e911_id)
pprint.pprint(result)
print("--Create and Validate an Address")
result = e911s_controller.create_address(
label="E911 Test",
first_name="Chris",
last_name="Smith",
street_name="3rd Ave",
street_number="1218",
city="Seattle",
state="WA",
country="USA",
zip="98101")
pprint.pprint(result)
print("\n--Validate an Address")
try:
result = e911s_controller.validate_address(
label="Test Address",
first_name="Chris",
last_name="Smith",
street_name="3rd Ave",
street_number="1182",
city="Seattle",
state="WA",
country="US",
zipcode="98101")
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)
print("\n--Create and Validate an Address")
try:
result = e911s_controller.create_address(
label="E911 Test",
first_name="Chris",
last_name="Smith",
street_name="3rd Ave",
street_number="1218",
city="Seattle",
state="WA",
country="US",
zipcode="98101")
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)
# Pull the ID from the newly created record
record_id = result['data']['id']
if len(result):
record_id = result['data']['id']
print("--Update an E911 Address")
result = e911s_controller.update_address(record_id, last_name='Wiley')
pprint.pprint(result)
print("\n--Update an E911 Address")
try:
result = e911s_controller.update_address(record_id, last_name='Wiley')
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)
# Get our DIDs
did_list = numbers_controller.list_account_phone_numbers()
@ -73,19 +87,30 @@ e911_id = e911_list['data'][0]['id']
# Associate them
print("--Associate an E911 Record and a DID")
result = e911s_controller.associate(e911_id, did)
pprint.pprint(result)
try:
result = e911s_controller.associate(e911_id, did)
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)
print("--List all DIDs associated with an E911 Record")
print("\n--List all DIDs associated with an E911 Record")
result = e911s_controller.list_dids_for_e911(e911_id)
pprint.pprint(result)
# Diss-Associate them
print("--Un-associate the address")
result = e911s_controller.disconnect(e911_id, did)
pprint.pprint(result)
print("--Delete an E911 Address")
result = e911s_controller.delete_address(e911_id)
pprint.pprint(result)
# Dis-Associate them
try:
print("\n--Un-associate the address")
result = e911s_controller.disconnect(did)
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)
try:
print("\n--Delete an E911 Address")
result = e911s_controller.delete_address(e911_id)
pprint.pprint(result)
except Exception as e:
print(str(e))
print(e.context.response.raw_body)

@ -3,7 +3,8 @@
"""
flowroutenumbersandmessagingcontrollers.base_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 ..api_helper import APIHelper
@ -11,6 +12,7 @@ from ..http.http_context import HttpContext
from ..http.requests_client import RequestsClient
from ..exceptions.api_exception import APIException
class BaseController(object):
"""All controllers inherit from this base class.
@ -21,10 +23,8 @@ class BaseController(object):
the same HttpClient. A user can use his own custom HttpClient
as well.
http_call_back (HttpCallBack): An object which holds call back
methods to be called before and after the execution of an HttpRequest.
global_headers (dict): The global headers of the API which are sent with
every request.
methods to be called before and after the execution of an
HttpRequest.
"""
http_client = RequestsClient()
@ -36,12 +36,13 @@ class BaseController(object):
}
def __init__(self, client=None, call_back=None):
if client != None:
if client is not None:
self.http_client = client
if call_back != None:
if call_back is not None:
self.http_call_back = call_back
def validate_parameters(self, **kwargs):
@staticmethod
def validate_parameters(**kwargs):
"""Validates required parameters of an endpoint.
Args:
@ -50,7 +51,8 @@ class BaseController(object):
"""
for name, value in kwargs.items():
if value is None:
raise ValueError("Required parameter {} cannot be None.".format(name))
raise ValueError("Required parameter {} cannot be None.".
format(name))
def execute_request(self, request, binary=False):
"""Executes an HttpRequest.
@ -66,29 +68,33 @@ class BaseController(object):
"""
# Invoke the on before request HttpCallBack if specified
if self.http_call_back != None:
if self.http_call_back is not None:
self.http_call_back.on_before_request(request)
# Add global headers to request
request.headers = APIHelper.merge_dicts(self.global_headers, request.headers)
request.headers = APIHelper.merge_dicts(self.global_headers,
request.headers)
# Invoke the API call to fetch the response.
func = self.http_client.execute_as_binary if binary else self.http_client.execute_as_string
func = self.http_client.execute_as_binary if binary else \
self.http_client.execute_as_string
response = func(request)
context = HttpContext(request, response)
# Invoke the on after response HttpCallBack if specified
if self.http_call_back != None:
if self.http_call_back is not None:
self.http_call_back.on_after_response(context)
return context
def validate_response(self, context):
@staticmethod
def validate_response(context):
"""Validates an HTTP response by checking for global errors.
Args:
context (HttpContext): The HttpContext of the API call.
"""
if (context.response.status_code < 200) or (context.response.status_code > 208): #[200,208] = HTTP OK
if (context.response.status_code < 200) or \
(context.response.status_code > 208):
raise APIException('HTTP response not OK.', context)

@ -122,7 +122,7 @@ class E911sController(BaseController):
city,
state,
country,
zip):
zipcode):
"""Does a POST request to /v2/e911s/validate.
Returns a 204 No Content on success, or a 404 with error data
@ -136,7 +136,7 @@ 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 204 - No Content or a
@ -161,7 +161,7 @@ class E911sController(BaseController):
body['data']['attributes']['city'] = city
body['data']['attributes']['state'] = state
body['data']['attributes']['country'] = country
body['data']['attributes']['zip'] = zip
body['data']['attributes']['zip'] = zipcode
# Prepare query URL
_query_builder = Configuration.base_uri
@ -200,7 +200,7 @@ class E911sController(BaseController):
city,
state,
country,
zip):
zipcode):
"""Does a POST request to /v2/e911s.
Creates an address record that can then be associated with 1 or more DIDs
@ -238,7 +238,8 @@ class E911sController(BaseController):
body['data']['attributes']['city'] = city
body['data']['attributes']['state'] = state
body['data']['attributes']['country'] = country
body['data']['attributes']['zip'] = zip
body['data']['attributes']['zip'] = zipcode
print(body)
# Prepare query URL
_query_builder = Configuration.base_uri
@ -278,7 +279,7 @@ class E911sController(BaseController):
city=None,
state=None,
country=None,
zip=None):
zipcode=None):
"""Does a PATCH request to /v2/e911s/<e911_id>.
@ -294,7 +295,7 @@ class E911sController(BaseController):
city (string, optional):
state (2 character string, optional):
country (string USA or Canada, optional):
zip (string postal code, optional)
zipcode (string postal code, optional)
Returns:
mixed: Response from the API. A JSON object containing the new record information.
@ -329,9 +330,17 @@ class E911sController(BaseController):
record_data['data']['attributes']['state'] = state
if country is not None:
record_data['data']['attributes']['country'] = country
if zip is not None:
record_data['data']['attributes']['zip'] = str(zip)
record_data['data']['attributes']['zip_code'] = str(zip)
if zipcode is not None:
record_data['data']['attributes']['zip'] = str(zipcode)
record_data['data']['attributes']['zip_code'] = str(zipcode)
# Fix address_type if not used
if 'address_type' in record_data['data']['attributes'] and \
record_data['data']['attributes']['address_type'] == u'':
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
@ -344,7 +353,7 @@ 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))
@ -386,6 +395,7 @@ class E911sController(BaseController):
# Return appropriate type
_query_url = APIHelper.clean_url(_query_builder)
print("Calling {}".format(_query_url))
# Prepare headers
_headers = {
@ -438,6 +448,7 @@ 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)
@ -454,13 +465,12 @@ class E911sController(BaseController):
# Return appropriate type
return APIHelper.json_deserialize(_context.response.raw_body)
def disconnect(self, e911_id, did):
"""Does a DELETE request to /v2/numbers/<did>/relationships/e911s/<e911_id>.
def disconnect(self, did):
"""Does a DELETE request to /v2/numbers/<did>/relationships/e911s.
Un-Associates the specified e911 record with the specified did
Args:
e911_id (integer, required): the id of the e911 record to update
did (string, required): the phone number to associate with
Returns:
@ -476,10 +486,11 @@ 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)
# Return appropriate type
_query_url = APIHelper.clean_url(_query_builder)
print("Calling {}".format(_query_url))
# Prepare headers
_headers = {

Loading…
Cancel
Save