Skip to content

Support for tracking multiple packages #11

@NetRxn

Description

@NetRxn

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('&amp;','&').replace('&lt;','<')
#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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions