def _handle_request_error(self, e, url): if isinstance(e, urlfetch.InvalidURLError): msg = ("The Gpayments library attempted to fetch an " "invalid URL (%r). This is likely due to a bug " "in the Gpayments Python bindings. Please let us know " "at [email protected]." % (url, )) elif isinstance(e, urlfetch.DownloadError): msg = "There was a problem retrieving data from Gpayments." elif isinstance(e, urlfetch.ResponseTooLargeError): msg = ("There was a problem receiving all of your data from " "Gpayments. This is likely due to a bug in Gpayments. " "Please let us know at [email protected].") else: msg = ("Unexpected error communicating with Gpayments. If this " "problem persists, let us know at [email protected].") msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")" raise error.APIConnectionError(msg)
def _handle_request_error(self, e): if isinstance(e, requests.exceptions.RequestException): msg = ("Unexpected error communicating with Gpayments. " "If this problem persists, let us know at " "[email protected].") err = "%s: %s" % (type(e).__name__, str(e)) else: msg = ("Unexpected error communicating with Gpayments. " "It looks like there's probably a configuration " "issue locally. If this problem persists, let us " "know at [email protected].") err = "A %s was raised" % (type(e).__name__, ) if str(e): err += " with error message %s" % (str(e), ) else: err += " with no error message" msg = textwrap.fill(msg) + "\n\n(Network error: %s)" % (err, ) raise error.APIConnectionError(msg)
def _handle_request_error(self, e): if e.args[0] in [ pycurl.E_COULDNT_CONNECT, pycurl.E_COULDNT_RESOLVE_HOST, pycurl.E_OPERATION_TIMEOUTED ]: msg = ("Could not connect to Gpayments. Please check your " "internet connection and try again. If this problem " "persists, let us know at " "[email protected].") elif e.args[0] in [pycurl.E_SSL_CACERT, pycurl.E_SSL_PEER_CERTIFICATE]: msg = ( "Could not verify Gpayments's SSL certificate. Please make " "sure that your network is not intercepting certificates. " "If this problem persists, let us know at " "[email protected].") else: msg = ("Unexpected error communicating with Gpayments. If this " "problem persists, let us know at [email protected].") msg = textwrap.fill(msg) + "\n\n(Network error: " + e.args[1] + ")" raise error.APIConnectionError(msg)
def _deprecated_request(self, impl, method, url, headers, params): warnings.warn( 'The *_request functions of APIRequestor are deprecated and ' 'will be removed in version 2.0. Please use the client classes ' ' in `gpayments.http_client` instead', DeprecationWarning, stacklevel=2) method = method.lower() if method == 'get' or method == 'delete': if params: url = self.build_url(url, params) post_data = None elif method == 'post': post_data = self.encode(params) else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Gpayments bindings. Please contact [email protected] for ' 'assistance.' % (method, )) client = impl(verify_ssl_certs=self._client._verify_ssl_certs) return client.request(method, url, headers, post_data)
def _handle_request_error(self, e): msg = ("Unexpected error communicating with Gpayments. " "If this problem persists, let us know at [email protected].") msg = textwrap.fill(msg) + "\n\n(Network error: " + str(e) + ")" raise error.APIConnectionError(msg)
def request_raw(self, method, url, params=None, supplied_headers=None): """ Mechanism for issuing an API call """ if self.access_token: my_access_token = self.access_token else: from gpayments import access_token my_access_token = access_token from gpayments import client_id, client_secret if my_access_token is None and \ (client_id is None or client_secret is None): raise error.AuthenticationError( 'No Client id or client secret provided. (HINT: set your client id using ' '"gpayments.client_id = <CLIENT_ID>" gpayments.client_secret = <CLIENT_SECRET>). ' 'You can obtain your client_id and client_secret from GPayments web interface.' 'See http://docs.payments.4geeks.io ' 'for details, or email [email protected] if you have any ' 'questions.') abs_url = '%s%s' % (self.api_base, url) encoded_params = 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' or method == 'put' or method == 'patch': 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 if method == 'put' or method == 'patch': post_data = params if my_access_token is None: post_data = util.json.dumps(params) else: raise error.APIConnectionError( 'Unrecognized HTTP method %r. This may indicate a bug in the ' 'Gpayments bindings. Please contact [email protected] for ' 'assistance.' % (method, )) headers = self.request_headers(my_access_token, method) if my_access_token is None: headers.pop('Authorization', None) if supplied_headers is not None: for key, value in six.iteritems(supplied_headers): headers[key] = value util.log_info('Request to Gpayments api', method=method, path=abs_url) util.log_debug('Post details', post_data=encoded_params, api_version=self.api_version) rbody, rcode, rheaders = self._client.request(method, abs_url, headers, post_data) util.log_info('Gpayments API response', path=abs_url, response_code=rcode) util.log_debug('API response body', body=rbody) if len(rbody) == 0: rbody = '{}' my_response = util.json.loads(rbody) if my_access_token is None and 'access_token' in my_response: gpayments.access_token = my_response['access_token'] my_access_token = gpayments.access_token return rbody, rcode, rheaders, my_access_token