def retrieve(cls, object_id, api_key=None): object_id = util.utf8(object_id) extn = urllib.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 update(cls, object_id, api_key=None, **params): object_id = util.utf8(object_id) extn = urllib.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)
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 = util.utf8(object_id) extn = urllib.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 instance_url(self): object_id = self.get('object_id') if not object_id: raise error.InvalidRequestError( 'Could not determine which URL to request: %s instance ' 'has invalid ID: %r' % (type(self).__name__, object_id), 'object_id') object_id = util.utf8(object_id) base = self.class_url() extn = urllib.quote_plus(object_id) return "%s/%s" % (base, extn)
def _api_encode(data): for key, value in data.iteritems(): key = util.utf8(key) if value is None: continue elif hasattr(value, 'shippo_id'): yield (key, value.shippo_id) elif isinstance(value, list) or isinstance(value, tuple): for subvalue in value: yield ("%s[]" % (key, ), util.utf8(subvalue)) elif isinstance(value, dict): subdict = dict(('%s[%s]' % (key, subkey), subvalue) for subkey, subvalue in value.iteritems()) for subkey, subvalue in _api_encode(subdict): yield (subkey, subvalue) elif isinstance(value, datetime.datetime): yield (key, _encode_datetime(value)) else: yield (key, util.utf8(value))
def _api_encode(data): for key, value in data.iteritems(): key = util.utf8(key) if value is None: continue elif hasattr(value, 'shippo_id'): yield (key, value.shippo_id) elif isinstance(value, list) or isinstance(value, tuple): for subvalue in value: yield ("%s[]" % (key,), util.utf8(subvalue)) elif isinstance(value, dict): subdict = dict(('%s[%s]' % (key, subkey), subvalue) for subkey, subvalue in value.iteritems()) for subkey, subvalue in _api_encode(subdict): yield (subkey, subvalue) elif isinstance(value, datetime.datetime): yield (key, _encode_datetime(value)) else: yield (key, util.utf8(value))
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.quote_plus(util.utf8(carrier_token)) tracking_number = urllib.quote_plus(util.utf8(tracking_number)) tn = urllib.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 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 = util.utf8(object_id) extn = urllib.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 = util.utf8(object_id) extn = urllib.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)