def specific_api_error(self, rbody, rcode, resp, rheaders, error_data): util.log_info( 'Stripe 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'), ) # Rate limits were previously coded as 400's with code 'rate_limit' if rcode == 429 or (rcode == 400 and error_data.get('code') == 'rate_limit'): return error.RateLimitError(error_data.get('message'), rbody, rcode, resp, rheaders) elif rcode in [400, 404]: return error.InvalidRequestError(error_data.get('message'), error_data.get('param'), rbody, rcode, resp, rheaders) elif rcode == 401: return error.AuthenticationError(error_data.get('message'), rbody, rcode, resp, rheaders) elif rcode == 402: return error.CardError(error_data.get('message'), error_data.get('param'), error_data.get('code'), rbody, rcode, resp, rheaders) elif rcode == 403: return error.PermissionError(error_data.get('message'), rbody, rcode, resp, rheaders) else: return error.APIError(error_data.get('message'), rbody, rcode, resp, rheaders)
def handle_api_error(self, rbody, rcode, resp, rheaders): 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) # Rate limits were previously coded as 400's with code 'rate_limit' if rcode == 429 or (rcode == 400 and err.get('code') == 'rate_limit'): raise error.RateLimitError( err.get('message'), rbody, rcode, resp, rheaders) elif rcode in [400, 404]: raise error.InvalidRequestError( err.get('message'), err.get('param'), rbody, rcode, resp, rheaders) elif rcode == 401: raise error.AuthenticationError( err.get('message'), rbody, rcode, resp, rheaders) elif rcode == 402: raise error.CardError(err.get('message'), err.get('param'), err.get('code'), rbody, rcode, resp, rheaders) elif rcode == 403: raise error.PermissionError( err.get('message'), rbody, rcode, resp, rheaders) else: raise error.APIError(err.get('message'), rbody, rcode, resp, rheaders)
def _set_client_id(params): if 'client_id' in params: return from stripe import client_id if client_id: params['client_id'] = client_id return raise error.AuthenticationError( 'No client_id provided. (HINT: set your client_id using ' '"stripe.client_id = <CLIENT-ID>"). You can find your client_ids ' 'in your Stripe dashboard at ' 'https://dashboard.stripe.com/account/applications/settings, ' 'after registering your account as a platform. See ' 'https://stripe.com/docs/connect/standalone-accounts for details, ' 'or email [email protected] if you have any questions.')
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.CardError(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 stripe import api_version if self.api_key: my_api_key = self.api_key else: from stripe 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 ' '"stripe.api_key = <API-KEY>"). You can generate API keys ' 'from the Stripe web interface. See https://stripe.com/api ' 'for details, or email [email protected] if you have any ' 'questions.') abs_url = '%s%s' % (stripe.api_base, url) encoded_params = urllib.parse.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 = encoded_params else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Stripe bindings. Please contact [email protected] for ' 'assistance.' % (method,)) ua = { 'bindings_version': version.VERSION, 'lang': 'python', 'publisher': 'stripe', '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-Stripe-Client-User-Agent': util.json.dumps(ua), 'User-Agent': 'Stripe/v1 PythonBindings/%s' % (version.VERSION,), 'Authorization': 'Bearer %s' % (my_api_key,) } if api_version is not None: headers['Stripe-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
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 stripe 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 ' '"stripe.api_key = <API-KEY>"). You can generate API keys ' 'from the Stripe web interface. See https://stripe.com/api ' 'for details, or email [email protected] if you have any ' 'questions.') abs_url = '%s%s' % (self.api_base, url) encoded_params = urllib.parse.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': if supplied_headers is not None and \ supplied_headers.get("Content-Type") == \ "multipart/form-data": generator = MultipartDataGenerator() generator.add_params(params or {}) post_data = generator.get_post_data() supplied_headers["Content-Type"] = \ "multipart/form-data; boundary=%s" % (generator.boundary,) else: post_data = encoded_params else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Stripe 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 list(supplied_headers.items()): headers[key] = value util.log_info('Request to Stripe api', method=method, path=abs_url) util.log_debug('Post details', post_data=post_data, api_version=self.api_version) rbody, rcode, rheaders = self._client.request(method, abs_url, headers, post_data) util.log_info('Stripe API response', path=abs_url, response_code=rcode) util.log_debug('API response body', body=rbody) if 'Request-Id' in rheaders: util.log_debug('Dashboard link for request', link=util.dashboard_link(rheaders['Request-Id'])) return rbody, rcode, rheaders, my_api_key
def request_raw(self, method, url, params=None, supplied_headers=None): """ Mechanism for issuing an API call """ from stripe import api_version if self.api_key: my_api_key = self.api_key else: from stripe 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 ' '"stripe.api_key = <API-KEY>"). You can generate API keys ' 'from the Stripe web interface. See https://stripe.com/api ' 'for details, or email [email protected] if you have any ' 'questions.') abs_url = '%s%s' % (self.api_base, url) encoded_params = urllib.parse.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': if supplied_headers is not None and \ supplied_headers.get("Content-Type") == \ "multipart/form-data": generator = MultipartDataGenerator() generator.add_params(params or {}) post_data = generator.get_post_data() supplied_headers["Content-Type"] = \ "multipart/form-data; boundary=%s" % (generator.boundary,) else: post_data = encoded_params else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Stripe bindings. Please contact [email protected] for ' 'assistance.' % (method,)) ua = { 'bindings_version': version.VERSION, 'lang': 'python', 'publisher': 'stripe', '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-Stripe-Client-User-Agent': util.json.dumps(ua), 'User-Agent': 'Stripe/v1 PythonBindings/%s' % (version.VERSION,), 'Authorization': 'Bearer %s' % (my_api_key,) } if self.stripe_account: headers['Stripe-Account'] = self.stripe_account if method == 'post': headers['Content-Type'] = 'application/x-www-form-urlencoded' if api_version is not None: headers['Stripe-Version'] = api_version if supplied_headers is not None: for key, value in list(supplied_headers.items()): headers[key] = value util.log_info('Request to Stripe api', method=method, path=abs_url) util.log_debug( 'Post details', post_data=post_data, api_version=api_version) rbody, rcode, rheaders = self._client.request( method, abs_url, headers, post_data) util.log_info( 'Stripe API response', path=abs_url, response_code=rcode) util.log_debug('API response body', body=rbody) if 'Request-Id' in rheaders: util.log_debug('Dashboard link for request', link=util.dashboard_link(rheaders['Request-Id'])) return rbody, rcode, rheaders, my_api_key