# -*- coding: utf-8 -*- """ flowroutenumbersandmessaging.controllers.cnams_controller This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ). """ from .base_controller import BaseController from ..api_helper import APIHelper from ..configuration import Configuration from ..http.auth.basic_auth import BasicAuth from ..exceptions.error_exception import ErrorException from .numbers_controller import NumbersController class CNAMsController(BaseController): """A Controller to access Endpoints in the flowroutenumbersandmessaging API.""" def list_cnams(self, limit=None, offset=None, is_approved=None): """Does a GET request to /v2/cnams. Returns a list of all cnams owned by the user. Args: limit (int, optional): Limits the number of items to retrieve. A maximum of 200 items can be retrieved. offset (int, optional): Offsets the list of phone numbers by your specified value. For example, if you have 4 phone numbers and you entered 1 as your offset value, then only 3 of your phone numbers will be displayed in the response. is_approved if set to true or false, will only show matching records Returns: mixed: Response from the API. A JSON object of E911 Records that satisfy your search criteria. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/cnams' _query_parameters = { 'limit': limit, 'offset': offset } if is_approved is not None: _query_parameters['is_approved'] = is_approved _query_builder = APIHelper.append_url_with_query_parameters(_query_builder, _query_parameters, Configuration.array_serialization) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def get_cnam(self, cnam_id): """Does a GET request to /v2/cnams/. Returns a record detail for the CNAM Record Id specified Args: Returns: mixed: Response from the API. A JSON object of of an E911 record that satisfy your search criteria. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/cnams/{}'.format(cnam_id) _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def search_cnams(self, limit=10, offset=0, starts_with=None, contains=None, ends_with=None): """Does a GET request to /v2/cnams?. Searches for CNAM Records that match the criteria Args: limit (int, optional): Limits the number of items to retrieve. A maximum of 200 items can be retrieved. offset (int, optional): Offsets the list of cnams by your specified value. For example, if you have 4 cnam records and you entered 1 as your offset value, then only 3 of your cnams will be displayed in the response. Returns: mixed: Response from the API. A JSON object of of an E911 record that satisfy your search criteria. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/cnams' _query_parameters = { 'limit': limit, 'offset': offset } if starts_with is not None: _query_parameters['starts_with'] = starts_with if contains is not None: _query_parameters['contains'] = contains if ends_with is not None: _query_parameters['ends_with'] = ends_with _query_url = APIHelper.clean_url(_query_builder) # Prepare and execute request _request = self.http_client.get(_query_url) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def create_cnam_record(self, value): """Does a POST request to /v2/cnams. Searches for CNAM Records that match the criteria Args: value (string, required): The text string for the new CNAM record Returns: mixed: Response from the API. A JSON object of of a CNAM record with the new data Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ body = { "value": value } # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/cnams' _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.post(_query_url, headers=_headers, parameters=body) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def associate_cnam(self, cnam_id, phone_number): # first, verify the number belongs to the user did = NumbersController().list_account_phone_numbers(contains=phone_number) if did is None: error_string = "Error, this phone number does not belong to you." return error_string did = did['data'][0]['id'] # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{}/relationships/cnam/{}'.format(did, cnam_id) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.patch(_query_url, headers=_headers) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def unassociate_cnam(self, phone_number): # first, verify the number belongs to the user did = NumbersController().list_account_phone_numbers(contains=phone_number) if did is None: error_string = "Error, this phone number does not belong to you." return error_string did = did['data'][0]['id'] # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/numbers/{}/relationships/cnam'.format(did) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body) def remove_cnam(self, cnam_id): # Prepare query URL _query_builder = Configuration.base_uri _query_builder += '/v2/cnams/{}'.format(cnam_id) _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.delete(_query_url, headers=_headers) BasicAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 401: raise ErrorException('Unauthorized – There was an issue with your API credentials.', _context) elif _context.response.status_code == 404: raise ErrorException('The specified resource was not found', _context) self.validate_response(_context) return APIHelper.json_deserialize(_context.response.raw_body)