def request_with_retries(self, method, url, headers, post_data=None): num_retries = 0 while True: try: num_retries += 1 response = self.request(method, url, headers, post_data) connection_error = None except error.APIConnectionError as e: connection_error = e response = None if self._should_retry(response, connection_error, num_retries): if connection_error: util.log_info( "Encountered a retryable error %s" % connection_error.user_message ) sleep_time = self._sleep_time_seconds(num_retries) util.log_info( ( "Initiating retry %i for request %s %s after " "sleeping %.2f seconds." % (num_retries, method, url, sleep_time) ) ) time.sleep(sleep_time) else: if response is not None: return response else: raise connection_error # pylint: disable=raising-bad-type
def prepare_raw_request(self, method, url, params=None, supplied_headers=None): if self.api_key: my_api_key = self.api_key else: from telnyx import api_key my_api_key = api_key if supplied_headers is None: supplied_headers = {} if my_api_key is None: raise error.AuthenticationError( "No API key provided. (HINT: set your API key using " '"telnyx.api_key = <API-KEY>"). You can generate API keys ' "from the Telnyx web interface. See https://developers.telnyx.com/docs/v2/development/authentication " "for details, or email [email protected] if you have any " "questions.") abs_url = "%s%s" % (self.api_base, url) if method == "get" or method == "head" or method == "delete": encoded_params = self.build_query_params(params) if params: abs_url = self.build_api_url(abs_url, encoded_params) post_data = None elif method == "post" or method == "patch": if (supplied_headers.get( "Content-Type", "application/json") == "application/json"): encoded_params = json.dumps(params or {}) post_data = encoded_params elif supplied_headers.get("Content-Type", None) == "multipart/form-data": generator = MultipartDataGenerator() generator.add_params(params or {}) post_data = generator.get_post_data() encoded_params = params supplied_headers[ "Content-Type"] = "multipart/form-data; boundary=%s" % ( generator.boundary, ) else: raise error.APIConnectionError( "Unrecognized HTTP method %r. This may indicate a bug in the " "Telnyx bindings. Please contact [email protected] for " "assistance." % (method, )) headers = self.request_headers(my_api_key, method) if supplied_headers is not None: for key, value in six.iteritems(supplied_headers): headers[key] = value util.log_info("Request to Telnyx api", method=method, path=abs_url) util.log_debug("Post details", post_data=encoded_params) return abs_url, headers, post_data, my_api_key
def request_raw(self, method, url, params=None, supplied_headers=None): """ Mechanism for issuing an API call """ abs_url, headers, post_data, my_api_key = self.prepare_raw_request( method, url, params=params, supplied_headers=supplied_headers) rbody, rcode, rheaders = self._client.request_with_retries( method, abs_url, headers, post_data) util.log_info("Telnyx API response", path=abs_url, response_code=rcode) util.log_debug("API response body", body=rbody) return rbody, rcode, rheaders, my_api_key
def specific_api_error(self, rbody, rcode, resp, rheaders, error_list): for err in error_list: util.log_info( "Telnyx API error received", error_code=err.get("code"), error_detail=err.get("detail"), error_source=err.get("source"), error_title=err.get("title"), ) if rcode == 400: return error.InvalidRequestError(error_list, rbody, rcode, resp, rheaders) elif rcode == 401: return error.AuthenticationError(error_list, rbody, rcode, resp, rheaders) elif rcode == 403: return error.PermissionError(error_list, rbody, rcode, resp, rheaders) elif rcode == 404: return error.ResourceNotFoundError(error_list, rbody, rcode, resp, rheaders) elif rcode == 405: return error.MethodNotSupportedError(error_list, rbody, rcode, resp, rheaders) elif rcode == 408: return error.TimeoutError(error_list, rbody, rcode, resp, rheaders) elif rcode == 415: return error.UnsupportedMediaTypeError(error_list, rbody, rcode, resp, rheaders) elif rcode == 422: return error.InvalidParametersError(error_list, rbody, rcode, resp, rheaders) elif rcode == 429: return error.RateLimitError(error_list, rbody, rcode, resp, rheaders) elif rcode == 500: return error.APIError(error_list, rbody, rcode, resp, rheaders) elif rcode == 503: return error.ServiceUnavailableError(error_list, rbody, rcode, resp, rheaders) else: return error.APIError(error_list, rbody, rcode, resp, rheaders)