Brobin / usps-api

Python Wrapper for the USPS API :truck: :package:
MIT License
58 stars 27 forks source link

Support for tracking multiple packages #11

Open NetRxn opened 4 years ago

NetRxn commented 4 years ago

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!

Robert-Muil commented 3 years ago

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.