def create(cls, api_key=None, **params):
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url()
     if 'asynchronous' in params:
         params['async'] = params['asynchronous']
     response, api_key = requestor.request('post', url, params)
     return convert_to_shippo_object(response, api_key)
 def update(cls, object_id, api_key=None, **params):
     object_id = object_id
     extn = urllib.parse.quote_plus(object_id)
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url() + extn
     response, api_key = requestor.request('put', url, params)
     return convert_to_shippo_object(response, api_key)
Exemple #3
0
 def remove(cls, object_id, api_key=None, **params):
     object_id = object_id
     extn = urllib.parse.quote_plus(object_id)
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url() + extn
     response, api_key = requestor.request('delete', url, params)
     return "Deleted the webhook"
    def retrieve(cls, object_id, api_key=None, **params):
        """
        Retrieve a batch, customized to allow the addition of url parameters

        Arguments:
            object_id (str) -- the batch object id
            **params
                page (int as str) -- pagination
                object_results (str) -- valid options are:
                                            "creation_failed"
                                            "creation_succeeded"
                                            "purchase_succeeded"
                                            "purchase_failed"

        Keyword Arguments:
            api_key (str) -- an api key, if not specified here it will default to the key
                             set in your environment var or by shippo.api_key = "..."

        Returns:
            (ShippoObject) -- The server response
        """
        object_id = object_id
        extn = urllib.parse.quote_plus(object_id)
        glue = '?'
        for key in params:
            extn += glue + key + '=' + str(params[key])
            if glue == '?':
                glue = '&'
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url() + extn
        response, api_key = requestor.request('get', url)
        return convert_to_shippo_object(response, api_key)
    def get_rates(cls, object_id, asynchronous=False, api_key=None, currency=None, **params):
        """
            Given a valid shipment object_id, all possible rates are calculated and returned.
        """
        if 'sync' in params:
            warnings.warn('The `sync` parameter is deprecated. '
                            'Use `asynchronous` while creating a shipment instead.', DeprecationWarning)
            # will be removed in the next major version
            if params.get('sync') is not None:
                asynchronous = not params['sync']

        if 'async' in params:
            warnings.warn('The `async` parameter is deprecated. '
                            'Use `asynchronous` while creating a shipment instead.', DeprecationWarning)
            # will be removed in the next major version
            if params.get('async') is not None:
                asynchronous = params['async']

        if not asynchronous:
            timeout = time.time() + rates_req_timeout
            while cls.retrieve(object_id, api_key=api_key).status in ("QUEUED", "WAITING") and time.time() < timeout:
                continue

        shipment_id = urllib.quote_plus(object_id)
        url = cls.class_url() + shipment_id + '/rates/'
        if currency:
            url = url + '' + urllib.quote_plus(currency)
        requestor = api_requestor.APIRequestor(api_key)
        response, api_key = requestor.request('get', url)
        return convert_to_shippo_object(response, api_key)
 def retrieve(cls, object_id, api_key=None):
     object_id = object_id
     extn = urllib.parse.quote_plus(object_id)
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url() + extn
     response, api_key = requestor.request('get', url)
     return convert_to_shippo_object(response, api_key)
    def request(self, method, url, params=None):
        if params is None:
            params = self._retrieve_params

        requestor = api_requestor.APIRequestor(self.api_key)
        response, api_key = requestor.request(method, url, params)

        return convert_to_shippo_object(response, api_key)
 def all(cls, api_key=None, size=None, page=None, **params):
     """
     To retrieve a list of all the objects in a class. The size of page and
         the page number can be specified respectively cls.all(<size>,<page>)
         **NOTE: To specify a page number, the page size must also be provided
     """
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url()
     if size:
         url = url + '?results=' + urllib.parse.quote_plus(str(size))
     if page:
         url = url + '&page=' + urllib.parse.quote_plus(str(page))
     response, api_key = requestor.request('get', url, params)
     return convert_to_shippo_object(response, api_key)
    def create(cls, api_key=None, **params):
        """
            Creates a new transaction object, given a valid rate ID.
            Takes the parameters as a dictionary instead of key word arguments.
        """
        url = cls.class_url()
        requestor = api_requestor.APIRequestor(api_key)
        response, content = requestor.request('post', url, params)
        transaction = convert_to_shippo_object(response, content)
        if 'sync' in params and params['sync']:
            timeout = time.time() + transaction_req_timeout
            while transaction.object_status in ("QUEUED", "WAITING") and time.time() < timeout:
                transaction = cls.retrieve(transaction.object_id)
                continue

        return transaction
    def get_rates(cls, object_id, api_key=None, currency=None, **params):
        """
            Given a valid shipment object_id, all possible rates are calculated and returned.
        """
        if 'sync' in params and params['sync']:
            timeout = time.time() + rates_req_timeout
            while cls.retrieve(object_id).object_status in ("QUEUED", "WAITING") and time.time() < timeout:
                continue

        shipment_id = urllib.quote_plus(object_id)
        url = cls.class_url() + shipment_id + '/rates/'
        if currency:
            url = url + '' + urllib.quote_plus(currency)
        requestor = api_requestor.APIRequestor(api_key)
        response, content = requestor.request('get', url)
        return convert_to_shippo_object(response, content)
Exemple #11
0
    def test_oauth_token_auth(self):
        mock_client = Mock()
        mock_client.name = 'mock_client'
        mock_client.request.return_value = ('{"status": "ok"}', 200)

        requestor = api_requestor.APIRequestor(key='oauth.mocktoken.mocksig',
                                               client=mock_client)
        requestor.request('GET', '/v1/echo')

        args, kwargs = mock_client.request.call_args
        method, url, headers, data = args

        self.assertDictContainsSubset(
            {'Authorization': 'Bearer oauth.mocktoken.mocksig'}, headers,
            "Expect correct token type to used for authorization with oauth token"
        )
Exemple #12
0
    def test_shippo_token_auth(self):
        config.app_name = 'TestApp'
        config.app_version = '1.1.1'

        mock_client = Mock()
        mock_client.name = 'mock_client'
        mock_client.request.return_value = ('{"status": "ok"}', 200)

        requestor = api_requestor.APIRequestor(key='shippo_test_mocktoken',
                                               client=mock_client)
        requestor.request('GET', '/v1/echo')

        args, kwargs = mock_client.request.call_args
        method, url, headers, data = args

        self.assertDictContainsSubset(
            {'Authorization': 'ShippoToken shippo_test_mocktoken'}, headers,
            "Expect correct token type to used for authorization with shippo token"
        )
    def purchase(cls, object_id, api_key=None):
        """
        Purchase batch of shipments

        Arguments:
            object_id (str) -- the batch object id

        Keyword Arguments:
            api_key (str) -- an api key, if not specified here it will default to the key
                             set in your environment var or by shippo.api_key = "..."

        Returns:
            (ShippoObject) -- The server response
        """
        object_id = object_id
        extn = urllib.parse.quote_plus(object_id)
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url() + extn + '/purchase'
        response, api_key = requestor.request('post', url)
        return convert_to_shippo_object(response, api_key)
    def remove(cls, object_id, shipments_to_remove, api_key=None):
        """
        Remove shipments from a batch

        Arguments:
            object_id (str) -- the batch object id
            shipments_to_remove (list of str) -- list of shipments to remove, must be in the format
                [<shipment 1 object id>, <shipment 2 object id>, ...]

        Keyword Arguments:
            api_key (str) -- an api key, if not specified here it will default to the key
                             set in your environment var or by shippo.api_key = "..."

        Returns:
            (ShippoObject) -- The server response
        """
        object_id = object_id
        extn = urllib.parse.quote_plus(object_id)
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url() + extn + '/remove_shipments'
        response, api_key = requestor.request('post', url, shipments_to_remove)
        return convert_to_shippo_object(response, api_key)
    def add(cls, object_id, shipments_to_add, api_key=None):
        """
        Add shipments to a batch

        Arguments:
            object_id (str) -- the batch object id
            shipments_to_add (list of dict) -- list of shipments to add, must be in the format
                [{'shipment': <shipment 1 object id>}, {'shipment': <shipment 2 object id>}, ...]

        Keyword Arguments:
            api_key (str) -- an api key, if not specified here it will default to the key
                             set in your environment var or by shippo.api_key = "..."

        Returns:
            (ShippoObject) -- The server response
        """
        object_id = util.utf8(object_id)
        extn = urllib.quote_plus(object_id)
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url() + extn + '/add_shipments'
        response, api_key = requestor.request('post', url, shipments_to_add)
        return convert_to_shippo_object(response, api_key)
Exemple #16
0
class Shipment(CreateableAPIResource, ListableAPIResource,
               FetchableAPIResource):
    """
        The heart of the Shippo API, a Shipment is made up of "to" and "from" Addresses
        and the Parcel to be shipped. Shipments can be created, retrieved and listed.
    """
    @classmethod
    def get_rates(cls,
                  object_id,
                  async=False,
                  api_key=None,
                  currency=None,
                  **params):
        """
            Given a valid shipment object_id, all possible rates are calculated and returned.
        """
        if 'sync' in params:
            warnings.warn(
                'The `sync` parameter is deprecated. '
                'Use `async` while creating a shipment instead.',
                DeprecationWarning)
            # will be removed in the next major version
            if params.get('sync') is not None:
                async = not params['sync']

        if not async:
            timeout = time.time() + rates_req_timeout
            while cls.retrieve(object_id, api_key=api_key).object_status in (
                    "QUEUED", "WAITING") and time.time() < timeout:
                continue

        shipment_id = urllib.quote_plus(object_id)
        url = cls.class_url() + shipment_id + '/rates/'
        if currency:
            url = url + '' + urllib.quote_plus(currency)
        requestor = api_requestor.APIRequestor(api_key)
        response, api_key = requestor.request('get', url)
        return convert_to_shippo_object(response, api_key)
    def get_status(cls, carrier_token, tracking_number, api_key=None):
        """
        A custom get method for tracking based on carrier and tracking number
        Written because the endpoint for tracking is different from our standard endpoint

        Arguments:
            carrier_token (str) -- name of the carrier of the shipment to track
                                    see https://goshippo.com/docs/reference#carriers
            tracking_number (str) -- tracking number to track

        Keyword Arguments:
            api_key (str) -- an api key, if not specified here it will default to the key
                             set in your environment var or by shippo.api_key = "..."

        Returns:
            (ShippoObject) -- The server response
        """
        carrier_token = urllib.parse.quote_plus(carrier_token)
        tracking_number = urllib.parse.quote_plus(tracking_number)
        tn = urllib.parse.quote_plus(tracking_number)
        requestor = api_requestor.APIRequestor(api_key)
        url = cls.class_url() + carrier_token + '/' + tracking_number
        response, api_key = requestor.request('get', url)
        return convert_to_shippo_object(response, api_key)
 def validate(cls, object_id, api_key=None):
     extn = urllib.parse.quote_plus(object_id)
     url = cls.class_url() + extn + '/validate'
     requestor = api_requestor.APIRequestor(api_key)
     response, api_key = requestor.request('get', url)
     return convert_to_shippo_object(response, api_key)
 def create(cls, api_key=None, **params):
     requestor = api_requestor.APIRequestor(api_key)
     url = cls.class_url()
     response, api_key = requestor.request('post', url, params)
     return convert_to_shippo_object(response, api_key)