Esempio n. 1
0
    def handle_api_error(rbody, rcode, resp):
        err = resp

        if rcode in [400, 404]:
            msg = err.get('description') + ", error code: " + str(
                resp['error_code'])
            raise error.InvalidRequestError(msg, err.get('request_id'), rbody,
                                            rcode, resp)
        elif rcode == 401:
            raise error.AuthenticationError(err.get('description'), rbody,
                                            rcode, resp)
        elif rcode == 402:
            raise error.CardError(err.get('description'),
                                  err.get('request_id'), err.get('error_code'),
                                  rbody, rcode, resp)
        else:
            raise error.APIError(
                "{0}, error code: {1}".format(err.get('description'),
                                              resp['error_code']), rbody,
                rcode, resp)
Esempio n. 2
0
    def request_raw(self, method, url, params=None):
        """
        Mechanism for issuing an API call
        """
        from openpay import api_version

        if self.api_key:
            my_api_key = self.api_key
        else:
            from openpay 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 '
                '"openpay.api_key = <API-KEY>"). You can generate API keys '
                'from the Openpay Dashboard.  See http://docs.openpay.mx '
                'for details, or email [email protected] if you have any '
                'questions.')

        abs_url = "{0}{1}".format(openpay.get_api_base(), url)

        if method == 'get' or method == 'delete':
            if params:
                abs_url = _build_api_url(abs_url, params)
            post_data = None
        elif method == 'post' or method == 'put':
            post_data = json.dumps(params)
        else:
            raise error.APIConnectionError(
                'Unrecognized HTTP method %r.  This may indicate a bug in the '
                'Openpay bindings.  Please contact [email protected] for '
                'assistance.' % (method, ))

        ua = {
            'bindings_version': version.VERSION,
            'lang': 'python',
            'publisher': 'openpay',
            '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-Openpay-Client-User-Agent': json.dumps(ua),
            'User-Agent': 'Openpay/v1 PythonBindings/%s' % (version.VERSION, ),
            'content-type': 'application/json',
        }

        if api_version is not None:
            headers['Openpay-Version'] = api_version

        rbody, rcode = self._client.request(method,
                                            abs_url,
                                            headers,
                                            post_data,
                                            user=my_api_key)

        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