From 5dcc0f4f39078f0bce48b3cdbc055a7ef83b8f31 Mon Sep 17 00:00:00 2001 From: Chris Lacina Date: Fri, 7 May 2021 14:41:04 -0700 Subject: [PATCH 1/2] Fixed invalid code Ran 'Black' on code for formatting Removed unneeded dependancies Simplified code structure --- demo_with_comments.py | 261 +++++++++--------- flowroutenumbersandmessaging/configuration.py | 5 +- .../controllers/base_controller.py | 43 +-- .../controllers/messages_controller.py | 2 +- .../controllers/routes_controller.py | 6 +- .../flowroutenumbersandmessaging_client.py | 9 +- 6 files changed, 171 insertions(+), 155 deletions(-) diff --git a/demo_with_comments.py b/demo_with_comments.py index 1fc165d..6b18f38 100755 --- a/demo_with_comments.py +++ b/demo_with_comments.py @@ -5,26 +5,21 @@ import json import random import string -import configuration -import credentials -from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import FlowroutenumbersandmessagingClient +from flowroutenumbersandmessaging.configuration import * +from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import ( + FlowroutenumbersandmessagingClient, +) print("Number/Route Management v2 & Messaging v2.1 Demo") -''' -# 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') -mobile_number = "YOUR_MOBILE_NUMBER" -''' -#Using files credentials.py and configuration.py for user-dependent variables. -default_fr_creds = credentials.FLOWROUTE_API_KEY -default_config = configuration.FLOWROUTE_DEFAULT_DIDS -basic_auth_user_name = default_fr_creds["USERNAME"] -basic_auth_password = default_fr_creds["PASSWORD"] -test_from_number = default_config["FROM"] -test_to_number = default_config["TO"] +# Use configuration.py for user-dependent variables. +basic_auth_user_name = Configuration().basic_auth_user_name +basic_auth_password = Configuration().basic_auth_password + +# If testing SMS functionality, fill in the following variables +test_from_number = None +test_to_number = None # Instantiate API client and create controllers for Numbers, Messages, and Routes. @@ -34,7 +29,7 @@ messages_controller = client.messages -#Use the numbers controller to list available area codes currently in inventory. +# Use the numbers controller to list available area codes currently in inventory. print("--List Available Area Codes") max_setup_cost = 3.25 limit = 3 @@ -43,178 +38,198 @@ pprint.pprint(result) -#Use the numbers controller to list numbers in the specified area code that are currently in inventory. +# Use the numbers controller to list numbers in the specified area code that are currently in inventory. print("--List Available Exchange Codes") limit = 3 offset = None max_setup_cost = None areacode = 206 -result = numbers_controller.list_available_exchange_codes(limit, offset, max_setup_cost, areacode) +result = numbers_controller.list_available_exchange_codes( + limit, offset, max_setup_cost, areacode +) pprint.pprint(result) -#Search for vanity and/or local numbers in numbers currently in inventory. +# Search for vanity and/or local numbers in numbers currently in inventory. print("--Search for Purchasable Phone Numbers") starts_with = 1 contains = 0 -ends_with = 007 +ends_with = "007" limit = 3 offset = None rate_center = None state = None print("Searching for vanity numbers with the specified patterns") -result = numbers_controller.search_for_purchasable_phone_numbers(starts_with, contains, ends_with, limit, offset, rate_center, state) +result = numbers_controller.search_for_purchasable_phone_numbers( + starts_with, contains, ends_with, limit, offset, rate_center, state +) pprint.pprint(result) -#Use the numbers controller to purchase a phone number from inventory. +# Use the numbers controller to purchase a phone number from inventory. print("--Purchase a Phone Number") -''' +""" Uncomment the result line below to allow the demo script to purchase the first number returned by the query above. If you are not running the number search above, please specify a purchasable_number before running the purchase a number request. -''' -purchasable_number = result['data'][0]['id'] -if purchasable_number == None : - print("Please assign purchasable_number to an available number you wish to purchase") -else : - print("Uncomment the result line below to allow the demo to purchase a number") - #result = numbers_controller.purchase_a_phone_number(purchasable_number) +""" +purchasable_number = result["data"][0]["id"] +if purchasable_number is None: + print( + "Please assign purchasable_number to an available number you wish to purchase" + ) +else: + print("Uncomment the result line below to allow the demo to purchase a number") + # result = numbers_controller.purchase_a_phone_number(purchasable_number) -#Use the numbers controller to list phone numbers currently on your account +# Use the numbers controller to list phone numbers currently on your account print("--List Account Phone Numbers") starts_with = 1 ends_with = None contains = None limit = 5 offset = None -result = numbers_controller.list_account_phone_numbers(starts_with, ends_with, contains, limit, offset) +result = numbers_controller.list_account_phone_numbers( + starts_with, ends_with, contains, limit, offset +) pprint.pprint(result) -#Use the numbers controller to list the details for a number on your account. +# Use the numbers controller to list the details for a number on your account. print("--List Phone Number Details") -#You can only get details for a number currently on your account. -number_id = result['data'][0]['id'] -if number_id == None : - print ("Please assign number_id to a number to get details for") -else : - result = numbers_controller.list_phone_number_details(number_id) - pprint.pprint(result) +# You can only get details for a number currently on your account. +number_id = result["data"][0]["id"] +if number_id is None: + print("Please assign number_id to a number to get details for") +else: + result = numbers_controller.list_phone_number_details(number_id) + pprint.pprint(result) -#Use the numbers controller to create an inbound route. Each inbound route created has a unique id, even if the route is identical. +# Use the numbers controller to create an inbound route. +# Each inbound route created has a unique id, even if the route is identical. print("---Create an Inbound Route") -# Function to generate six-character random string. If you attempt to create a route already on your account you will receive a 403 error. + + def id_generator(size=6, chars=string.ascii_lowercase + string.digits): - return ''.join(random.choice(chars) for _ in range(size)) -new_route = id_generator() + '.sonsofodin.com' + # Function to generate six-character random string. + # If you attempt to create a route already on your account you will receive a 403 error. + return "".join(random.choice(chars) for _ in range(size)) + + +new_route = id_generator() + ".sonsofodin.com" alias = id_generator() for i in range(10): alias += str(i) -#request_body is what a JSON body looks like! -request_body = '{ \ - "data": { \ - "type": "route", \ - "attributes": { \ - "route_type": "host", \ - "value": "' + new_route +'", \ - "alias": "' + alias + '" \ - } \ - } \ -}' +# request_body is what a JSON body looks like! +request_body = { + "data": { + "type": "route", + "attributes": { + "route_type": "host", + "value": new_route, + "alias": alias, + } + } +} + result = routes_controller.create_an_inbound_route(request_body) pprint.pprint(result) -#Use the numbers controller to list inbound routes currently on your account. -print ("---List Inbound Routes") +# Use the numbers controller to list inbound routes currently on your account. +print("---List Inbound Routes") limit = 3 result = routes_controller.list_inbound_routes(limit) pprint.pprint(result) -#assigns your primary_inbound_route_id variable to be the [0] first result from the querey above. -#The first result will always be sip-reg with a route id of 0. -primary_inbound_route_id = result['data'][0]['id'] -if primary_inbound_route_id == None : - print("Please assign a primary_inbound_route_id") - -#assigns the failover_route_id variable to be the [1] second result from the querey above. -failover_route_id = result['data'][1]['id'] -if failover_route_id == None : - print("Please assign a failover_route_id") - - -#Use the routes controller to assign a primary route to a number currently on your account. -#create the primary route JSON request: -request_body = '{ \ - "data": { \ - "type": "route", \ - "id": "' + str(primary_inbound_route_id) +'" \ - } \ -}' -#update the route! +# assigns your primary_inbound_route_id variable to be the [0] first result from the querey above. +# The first result will always be sip-reg with a route id of 0. +primary_inbound_route_id = result["data"][0]["id"] +if primary_inbound_route_id is None: + print("Please assign a primary_inbound_route_id") + +# assigns the failover_route_id variable to be the [1] second result from the querey above. +failover_route_id = result["data"][1]["id"] +if failover_route_id is None: + print("Please assign a failover_route_id") + + +# Use the routes controller to assign a primary route to a number currently on your account. +# create the primary route JSON request: +request_body = { + "data": { + "type": "route", + "id": str(primary_inbound_route_id), + } +} + +# update the route! print("---Update Primary Voice Route") result = routes_controller.update_primary_voice_route(number_id, request_body) if result is None: print("204: No Content") else: - print (result) - - -#Use the routes controller to assign a failover route to a number currently on your account. -#create the failover route JSON request: -request_body = '{ \ - "data": { \ - "type": "route", \ - "id": "' + str(failover_route_id) +'" \ - } \ -}' -#update the route! + print(result) + + +# Use the routes controller to assign a failover route to a number currently on your account. +# create the failover route JSON request: +request_body = { + "data": { + "type": "route", + "id": str(failover_route_id), + } +} + +# update the route! print("---Update Failover Voice Route") result = routes_controller.update_failover_voice_route(number_id, request_body) if result is None: print("204: No Content") else: - print (result) - - -#Use the messaging controller to send a MMS message using a number currently on your account. -#Create the JSON request: -request_body = '{ \ - "data": { \ - "type": "message", \ - "attributes": { \ - "to": "' + str(test_to_number) + '", \ - "from": "' + str(test_from_number) + '", \ - "body": "greetings hooman!!", \ - "is_mms": "true", \ - "media_urls": ["http://s3.amazonaws.com/barkpost-assets/50+GIFs/37.gif"] \ - } \ - } \ -}' -#send the message! -print("---Send A Message") -print("Please uncomment the following two lines to send an MMS message") -#result = messages_controller.send_a_message(request_body) -#pprint.pprint(result) - - -#Use the messages controller to look up the MDRs for a set of messages. + print(result) + + +if test_to_number and test_from_number: + # Use the messaging controller to send a MMS message using a number currently on your account. + # Create the JSON request: + request_body = { + "data": { + "type": "message", + "attributes": { + "to": str(test_to_number), + "from": str(test_from_number), + "body": "greetings hooman!!", + "is_mms": "true", + "media_urls": ["http://s3.amazonaws.com/barkpost-assets/50+GIFs/37.gif"] + } + } + } + + # send the message! + print("---Send A Message") + result = messages_controller.send_a_message(request_body) + pprint.pprint(result) + + +# Use the messages controller to look up the MDRs for a set of messages. print("---Look Up A Set Of Messages") -start_date = "2017-12-01" -end_date = "2018-01-08" +start_date = "2018-12-01" +end_date = "2021-01-08" limit = 2 result = messages_controller.look_up_a_set_of_messages(start_date, end_date, limit) pprint.pprint(result) -#Use the messages controller to look up the report for an MDR. -print ("---Look Up A Message Detail Record") -message_id = result['data'][0]['id'] -if message_id == None : - print("You need to have sent a message within the specified date range to get its MDR.") +# Use the messages controller to look up the report for an MDR. +print("---Look Up A Message Detail Record") +message_id = result["data"][0]["id"] +if message_id is None: + print( + "You need to have sent a message within the specified date range to get its MDR." + ) result = messages_controller.look_up_a_message_detail_record(message_id) pprint.pprint(result) diff --git a/flowroutenumbersandmessaging/configuration.py b/flowroutenumbersandmessaging/configuration.py index 7158de6..0558ddf 100644 --- a/flowroutenumbersandmessaging/configuration.py +++ b/flowroutenumbersandmessaging/configuration.py @@ -5,7 +5,6 @@ This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ) """ -from .api_helper import APIHelper class Configuration(object): @@ -26,9 +25,9 @@ class Configuration(object): # The username to use with basic authentication # TODO: Set an appropriate value - basic_auth_user_name = "YOUR API KEY" + basic_auth_user_name = '22476860' # "YOUR API KEY" # The password to use with basic authentication # TODO: Set an appropriate value - basic_auth_password = "YOUR SECRET KEY" + basic_auth_password = 'bbK4ek0MxXvoyQ7ahWSDEOr8TcIdlfBx' # "YOUR SECRET KEY" diff --git a/flowroutenumbersandmessaging/controllers/base_controller.py b/flowroutenumbersandmessaging/controllers/base_controller.py index 7ab2ea3..e416e10 100644 --- a/flowroutenumbersandmessaging/controllers/base_controller.py +++ b/flowroutenumbersandmessaging/controllers/base_controller.py @@ -33,9 +33,7 @@ class BaseController(object): http_call_back = None - global_headers = { - 'user-agent': 'Flowroute SDK v3.0' - } + global_headers = {"user-agent": "Flowroute SDK v3.0"} def __init__(self, client=None, call_back=None): if client is not None: @@ -53,8 +51,7 @@ def validate_parameters(**kwargs): """ 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. @@ -74,12 +71,14 @@ def execute_request(self, request, binary=False): 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) @@ -97,9 +96,8 @@ def validate_response(context): context (HttpContext): The HttpContext of the API call. """ - if (context.response.status_code < 200) or \ - (context.response.status_code > 208): - raise APIException('HTTP response not OK.', context) + 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): @@ -108,17 +106,22 @@ def handle_request_and_response(self, 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) + 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) + 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) + 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) + 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/messages_controller.py b/flowroutenumbersandmessaging/controllers/messages_controller.py index d8fe3c3..f6233ad 100644 --- a/flowroutenumbersandmessaging/controllers/messages_controller.py +++ b/flowroutenumbersandmessaging/controllers/messages_controller.py @@ -159,7 +159,7 @@ def send_a_message(self, body): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, - parameters=APIHelper.json_serialize(json.loads(body))) + parameters=APIHelper.json_serialize(body)) return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/controllers/routes_controller.py b/flowroutenumbersandmessaging/controllers/routes_controller.py index 6559d7e..387592c 100644 --- a/flowroutenumbersandmessaging/controllers/routes_controller.py +++ b/flowroutenumbersandmessaging/controllers/routes_controller.py @@ -52,7 +52,7 @@ def create_an_inbound_route(self, body): # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, - parameters=APIHelper.json_serialize(json.loads(body))) + parameters=APIHelper.json_serialize(body)) return self.handle_request_and_response(_request) @@ -135,7 +135,7 @@ def update_primary_voice_route(self, number_id, body): # Prepare and execute request _request = self.http_client.patch(_query_url, - parameters=APIHelper.json_serialize(json.loads(body))) + parameters=APIHelper.json_serialize(body)) return self.handle_request_and_response(_request) @@ -176,7 +176,7 @@ def update_failover_voice_route(self, _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))) + _request = self.http_client.patch(_query_url, parameters=APIHelper.json_serialize(body)) return self.handle_request_and_response(_request) diff --git a/flowroutenumbersandmessaging/flowroutenumbersandmessaging_client.py b/flowroutenumbersandmessaging/flowroutenumbersandmessaging_client.py index 9b00ac8..2209e30 100644 --- a/flowroutenumbersandmessaging/flowroutenumbersandmessaging_client.py +++ b/flowroutenumbersandmessaging/flowroutenumbersandmessaging_client.py @@ -15,6 +15,7 @@ from .controllers.porting_controller import PortingController from .controllers.cdrs_controller import CDRsController + class FlowroutenumbersandmessagingClient(object): config = Configuration @@ -47,10 +48,8 @@ def porting(self): def cdrs(self): return CDRsController() - def __init__(self, - basic_auth_user_name = None, - basic_auth_password = None): - if basic_auth_user_name != None: + def __init__(self, basic_auth_user_name=None, basic_auth_password=None): + if basic_auth_user_name is not None: Configuration.basic_auth_user_name = basic_auth_user_name - if basic_auth_password != None: + if basic_auth_password is not None: Configuration.basic_auth_password = basic_auth_password From cba96c8685c19d40bf86bb6a5927b0e127e03154 Mon Sep 17 00:00:00 2001 From: Chris Lacina Date: Fri, 7 May 2021 14:56:07 -0700 Subject: [PATCH 2/2] Update configuration.py --- flowroutenumbersandmessaging/configuration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flowroutenumbersandmessaging/configuration.py b/flowroutenumbersandmessaging/configuration.py index 0558ddf..0ccb25a 100644 --- a/flowroutenumbersandmessaging/configuration.py +++ b/flowroutenumbersandmessaging/configuration.py @@ -25,9 +25,9 @@ class Configuration(object): # The username to use with basic authentication # TODO: Set an appropriate value - basic_auth_user_name = '22476860' # "YOUR API KEY" + basic_auth_user_name = "YOUR API KEY" # The password to use with basic authentication # TODO: Set an appropriate value - basic_auth_password = 'bbK4ek0MxXvoyQ7ahWSDEOr8TcIdlfBx' # "YOUR SECRET KEY" + basic_auth_password = "YOUR SECRET KEY"