Example #1
0
def list_postage():
    """
    Returns all available postage for the account.
    """
    results = post(api_url("postage", "list"), {}, api_key)
    postages = []

    for result in results['postages']:
        postage = get_postage(result)
        postages.append(postage)

    return postages
Example #2
0
def get_postage(filename):
    """
    Returns postage by filename.
    """
    result = post(api_url("postage", "get"), {"label_file_name": filename}, api_key)

    rate = Rate(result['rate']['rate'], result['rate']['carrier'], result['rate']['service'])
    postage = Postage(None, rate)

    postage.label_file_name = result['label_file_name']
    postage.tracking_code = result['tracking_code']
    postage.label_url = result['label_url']
    postage.label_file_type = result['label_file_name']

    return postage
Example #3
0
    def buy(self):
        params = {
            'parcel': self.shipment.package.as_json(),
            'to': self.shipment.to_address.as_json(),
            'from': self.shipment.from_address.as_json(),
            'service': self.rate.service,
            'carrier': 'USPS'
        }

        result = post(api_url("postage", "buy"), params, api_key)

        self.label_file_name = result['label_file_name']
        self.tracking_code = result['tracking_code']
        self.label_url = result['label_url']
        self.label_file_type = result['label_file_name']
Example #4
0
    def verify(self):
        params = {
            'address': self.as_json()
        }

        result = post(api_url("address", "verify"), params, api_key)

        if 'error' in result:
            raise InvalidAddress(result['error'])
        else:
            self.street1 = result['address'].get('street1', '')
            self.street2 = result['address'].get('street2', '')
            self.city = result['address'].get('city', '')
            self.state = result['address'].get('state', '')
            self.zip = result['address'].get('zip', '')

            return self
Example #5
0
    def rates(self):
        params = {
            'parcel': self.package.as_json(),
            'to': self.to_address.as_json(),
            'from': self.from_address.as_json()
        }

        results = post(api_url('postage', 'rates'), params, api_key)

        if 'error' in results:
            raise InvalidShipment(results['error'])
        else:
            rates = []

            for result in results['rates']:
                rates.append(Rate(result['rate'],
                                  result['carrier'],
                                  result['service']))
            return rates