usps-api
usps-api copied to clipboard
Support for tracking multiple packages
Thanks for an awesome package!
Since the USPS API can handle up to 35 tracking IDs per request, i'm wondering if there's a good way to pass a list with your existing package, or if it would be possible to add this feature.
I tried to generate the URL string using variations of the code below, but I lost the detailed dictionary that your single query code provides.
{'EventTime': ' 'EventDate': 'Event': 'EventCity': 'EventState': 'EventZIPCode': 'EventCountry': 'FirmName': 'Name': 'AuthorizedAgent': 'DeliveryAttributeCode': },
-----------------------------------what I tried --------------------------------------------------
def USPSRequest(trackingNumbers,userID):
TrackingNumbersFormatted = f'<TrackRequest USERID="{userID}">'
TrackingNumbersFormatted = str(TrackingNumbersFormatted)
for i in trackingNumbers:
TrackingNumbersFormatted += "<TrackID ID="
TrackingNumbersFormatted +='"'
TrackingNumbersFormatted += str(i).strip()
TrackingNumbersFormatted +='"></TrackID>'
URL = f'https://secure.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML={TrackingNumbersFormatted}</TrackRequest>'.replace('&','&').replace('<','<')
#return URL
xml_response = requests.get(URL).content
response = json.loads(json.dumps(xmltodict.parse(xml_response)))
if 'Error' in response:
raise USPSApiError(response['Error']['Description'])
return response
I'm fairly new to APIs & XML so I'm sure there's a much more elegant way to do this.
Thanks again, and have a great night!
I've added this capability to the CityStateLookup API in this PR: https://github.com/Brobin/usps-api/pull/13. Should be very easy to do the same thing for the tracking.