def specific_api_error(self, rbody, rcode, resp, rheaders, error_data): util.log_info( 'Ping++ API error received', error_code=error_data.get('code'), error_type=error_data.get('type'), error_message=error_data.get('message'), error_param=error_data.get('param'), ) if rcode in [400, 404]: raise error.InvalidRequestError(error_data.get('message'), error_data.get('param'), error_data.get('code'), rbody, rcode, resp, rheaders) elif rcode == 401: raise error.AuthenticationError(error_data.get('message'), rbody, rcode, resp, rheaders) elif rcode == 402: raise error.ChannelError(error_data.get('message'), error_data.get('param'), error_data.get('code'), rbody, rcode, resp, rheaders) elif rcode == 403: raise error.RateLimitError(error_data.get('message'), rbody, rcode, resp, rheaders) else: raise error.APIError(error_data.get('message'), rbody, rcode, resp, rheaders)
def request_raw(self, method, url, params=None, supplied_headers=None): """ Mechanism for issuing an API call """ if self.api_key: my_api_key = self.api_key else: from pingpp import api_key my_api_key = api_key if my_api_key is None: raise error.AuthenticationError( 'No API key provided. (HINT: set your API key using ' '"pingpp.api_key = <API-KEY>"). You can generate API keys ' 'from the Ping++ web interface. ' 'See https://www.pingxx.com/api for details.') abs_url = '%s%s' % (self.api_base, url) request_uri = url if method == 'get' or method == 'delete': if params: encoded_params = urlencode(list(_api_encode(params or {}))) abs_url = _build_api_url(abs_url, encoded_params) request_uri = _build_api_url(url, encoded_params) post_data = None elif method == 'post' or method == 'put': post_data = util.json.dumps(params).encode("utf-8") else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Ping++ bindings. ' % (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 request_utc_timestamp = _get_utc_timestamp() headers['Pingplusplus-Request-Timestamp'] = request_utc_timestamp rsa_data = self.get_rsa_verify_data(post_data, request_uri, int(request_utc_timestamp)) util.log_debug('Signing data', data=rsa_data) privkey = self.get_private_key() if privkey is not None: headers['Pingplusplus-Signature'] = self.rsa_sign( privkey, rsa_data) rbody, rcode, rheaders = self.execute_request_with_retry( method, abs_url, headers, post_data) return rbody, rcode, rheaders, my_api_key
def request_raw(self, method, url, params=None): """ Mechanism for issuing an API call """ from pingpp import api_version from pingpp import accept_language my_accept_language = accept_language if self.api_key: my_api_key = self.api_key else: from pingpp import api_key my_api_key = api_key if my_api_key is None: raise error.AuthenticationError( 'No API key provided. (HINT: set your API key using ' '"pingpp.api_key = <API-KEY>"). You can generate API keys ' 'from the Ping++ web interface. See https://pingxx.com ' 'for details, or email [email protected] if you have any ' 'questions.') abs_url = '%s%s' % (pingpp.api_base, url) encoded_params = urllib.urlencode(list(_api_encode(params or {}))) if method == 'get' or method == 'delete': if params: abs_url = _build_api_url(abs_url, encoded_params) post_data = None elif method == 'post': post_data = json.dumps(params) else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Ping++ bindings. Please contact [email protected] for ' 'assistance.' % (method, )) ua = { 'bindings_version': version.VERSION, 'lang': 'python', 'publisher': 'pingpp', 'httplib': self._client.name, } for attr, func in [['lang_version', platform.python_version], ['platform', platform.platform], ['uname', lambda: ' '.join(platform.uname())]]: try: val = func() except Exception, e: val = "!! %s" % (e, ) ua[attr] = val
def handle_api_error(self, rbody, rcode, resp): try: err = resp['error'] except (KeyError, TypeError): raise error.APIError( "Invalid response object from API: %r (HTTP response code " "was %d)" % (rbody, rcode), rbody, rcode, resp) if rcode in [400, 404]: raise error.InvalidRequestError(err.get('message'), err.get('param'), rbody, rcode, resp) elif rcode == 401: raise error.AuthenticationError(err.get('message'), rbody, rcode, resp) elif rcode == 402: raise error.ChannelError(err.get('message'), err.get('param'), err.get('code'), rbody, rcode, resp) else: raise error.APIError(err.get('message'), rbody, rcode, resp)
def request_raw(self, method, url, params=None): """ Mechanism for issuing an API call """ from pingpp import api_version from pingpp import accept_language my_accept_language = accept_language if self.api_key: my_api_key = self.api_key else: from pingpp import api_key my_api_key = api_key if my_api_key is None: raise error.AuthenticationError( 'No API key provided. (HINT: set your API key using ' '"pingpp.api_key = <API-KEY>"). You can generate API keys ' 'from the Ping++ web interface. See https://pingxx.com ' 'for details.') abs_url = '%s%s' % (pingpp.api_base, url) encoded_params = urllib.urlencode(list(_api_encode(params or {}))) if method == 'get' or method == 'delete': if params: abs_url = _build_api_url(abs_url, encoded_params) post_data = None elif method == 'post': post_data = util.json.dumps(params).encode("utf-8") else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Ping++ bindings. ' % (method, )) ua = { 'bindings_version': version.VERSION, 'lang': 'python', 'publisher': 'pingpp', 'httplib': self._client.name, } for attr, func in [['lang_version', platform.python_version], ['platform', platform.platform], ['uname', lambda: ' '.join(platform.uname())]]: try: val = func() except Exception as e: val = "!! %s" % (e, ) ua[attr] = val headers = { 'X-Pingpp-Client-User-Agent': util.json.dumps(ua), 'User-Agent': 'Pingplusplus/v1 PythonBindings/%s' % (version.VERSION, ), 'Authorization': 'Bearer %s' % (my_api_key, ), 'Accept-Language': my_accept_language } if method == 'post': headers['Content-Type'] = 'application/json;charset=UTF-8' privkey = self.get_private_key() if privkey is not None: headers['Pingplusplus-Signature'] = self.rsa_sign( privkey, post_data) if api_version is not None: headers['Pingplusplus-Version'] = api_version rbody, rcode = self._client.request(method, abs_url, headers, post_data) util.logger.info( 'API request to %s returned (response code, response body) of ' '(%d, %r)', abs_url, rcode, rbody) return rbody, rcode, my_api_key