88 lines
2.4 KiB
Python

import json
import requests
from django.conf import settings
from . import ShippingContainer
api_key = "Gj0MZRxvpJz2YbWaroKAjstYGPIHf738"
customer_id = "12375190"
headers = {'Authorization': 'RSIS ' + api_key, 'Content-Type': 'application/json'}
base_url = "https://xpsshipper.com/restapi/v1/customers/" + customer_id
def get_container(weight, name):
containers = {
"box_a": {
"type_code": "usps_custom_package",
"dim_unit": "in",
"pieces": {
"weight": weight,
"length": "10.125",
"width": "7.125",
"height": "5",
"insuranceAmount": None,
"declaredValue": None
}
},
"box_b": {
"type_code": "usps_custom_package",
"dim_unit": "in",
"pieces": {
"weight": weight,
"length": "13",
"width": "11",
"height": "6",
"insuranceAmount": None,
"declaredValue": None
}
},
"large_flat_rate_box": {
"type_code": "usps_large_flat_rate_box",
"dim_unit": None,
"pieces": {
"weight": weight,
"length": None,
"width": None,
"height": None,
"insuranceAmount": None,
"declaredValue": None
}
}
}
return containers[name]
def get_quote(destination_zip, weight, container):
data = get_data(destination_zip, weight, container)
resp = request("/quote", data)
return resp["totalAmount"]
def request(url, data):
r = requests.post(base_url + url, headers=headers, data=json.dumps(data))
return r.json()
def get_data(destination_zip, weight, container):
container = get_container(weight, container)
data = {
"carrierCode": "usps",
"serviceCode": "usps_priority",
"packageTypeCode": container["type_code"],
"signatureOptionCode": "NO_SIGNATURE_REQUIRED",
"sender": {
"country": "US",
"zip": "98368"
},
"receiver": {
"country": "US",
"zip": destination_zip
},
"residential": True,
"weightUnit": "lb",
"dimUnit": container["dim_unit"],
"currency": "USD",
"customsCurrency": "USD",
"pieces": [ container["pieces"] ]
}
return data