示例#1
0
    def handle_api_error(self, rbody, rcode, resp):

        if rcode in [400, 404]:
            raise error.InvalidRequestError(rbody, rcode, resp)
        elif rcode == 401:
            raise error.AuthenticationError(rbody, rcode, resp)
        else:
            raise error.APIError(rbody, rcode, resp)
示例#2
0
    def request_raw(self, method, url, params=None):
        """
        Mechanism for issuing an API call
        """
        from shippo import api_version

        if self.api_key:
            my_api_key = self.api_key
        else:
            from shippo 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 '
                '"shippo.api_key = <API-KEY>"). You can generate API keys '
                'from the Shippo web interface.  See https://goshippo.com/api '
                'for details, or email [email protected] if you have any '
                'questions.')

        token_type = 'ShippoToken'
        if my_api_key.startswith('oauth.'):
            token_type = 'Bearer'

        abs_url = '%s%s' % (shippo.api_base, url)

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

        ua = {
            'bindings_version': VERSION,
            'lang': 'python',
            'publisher': 'shippo',
            '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
示例#3
0
    def request_raw(self, method, url, params=None):
        """
        Mechanism for issuing an API call
        """

        if self.api_key:
            my_api_key = self.api_key
        else:
            my_api_key = config.api_key

        if my_api_key is None:
            raise error.AuthenticationError(
                'No API key provided. (HINT: set your API key using '
                '"shippo.config.api_key = shippo_test_d90f00698a0a8def0495fddb4212bb08051469d3"). You can generate API keys '
                'from the Shippo web interface.  See https://goshippo.com/api '
                'for details, or email [email protected] if you have any '
                'questions.')

        token_type = 'ShippoToken'
        if my_api_key.startswith('oauth.'):
            token_type = 'Bearer'

        abs_url = '%s%s' % (config.api_base, url)

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

        shippo_user_agent = APIRequestor.get_shippo_user_agent_header(config)

        headers = {
            'Content-Type':
            'application/json',
            'X-Shippo-Client-User-Agent':
            shippo_user_agent,
            'User-Agent':
            '%s/%s ShippoPythonSDK/%s' % (
                config.app_name,
                config.app_version,
                config.sdk_version,
            ),
            'Authorization':
            '%s %s' % (
                token_type,
                my_api_key,
            ),
            'Shippo-API-Version':
            config.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
示例#4
0
    def request_raw(self, method, url, params=None):
        """
        Mechanism for issuing an API call
        """
        from shippo.config import api_version

        if self.api_key:
            my_api_key = self.api_key
        else:
            from shippo.config 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 '
                '"shippo.config.api_key = shippo_test_d90f00698a0a8def0495fddb4212bb08051469d3"). You can generate API keys '
                'from the Shippo web interface.  See https://goshippo.com/api '
                'for details, or email [email protected] if you have any '
                'questions.')

        token_type = 'ShippoToken'
        if my_api_key.startswith('oauth.'):
            token_type = 'Bearer'

        abs_url = '%s%s' % (shippo.config.api_base, url)

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

        ua = {
            'bindings_version': VERSION,
            'lang': 'python',
            'publisher': 'shippo',
            '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 = {
            'Content-Type': 'application/json',
            'X-Shippo-Client-User-Agent': util.json.dumps(ua),
            'User-Agent': 'Shippo/v1 PythonBindings/%s' % (VERSION, ),
            'Authorization': '%s %s' % (
                token_type,
                my_api_key,
            )
        }

        if api_version is not None:
            headers['Shippo-API-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