forked from python-fedex-devs/python-fedex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
track_shipment.py
executable file
·93 lines (74 loc) · 3.97 KB
/
track_shipment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
"""
This example shows how to track shipments.
"""
import logging
import sys
from example_config import CONFIG_OBJ
from fedex.services.track_service import FedexTrackRequest
# Un-comment to see the response from Fedex printed in stdout.
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# NOTE: TRACKING IS VERY ERRATIC ON THE TEST SERVERS. YOU MAY NEED TO USE
# PRODUCTION KEYS/PASSWORDS/ACCOUNT #. THE TEST SERVERS OFTEN RETURN A NOT FOUND ERROR.
# WHEN TESTING IN PRODUCTION, GIVE SOME TIME FOR THE TRACKING TO PROPAGATE.
# We're using the FedexConfig object from example_config.py in this dir.
customer_transaction_id = "*** TrackService Request v10 using Python ***" # Optional transaction_id
track = FedexTrackRequest(CONFIG_OBJ, customer_transaction_id=customer_transaction_id)
# Track by Tracking Number
track.SelectionDetails.PackageIdentifier.Type = 'TRACKING_NUMBER_OR_DOORTAG'
track.SelectionDetails.PackageIdentifier.Value = '781820562774'
# FedEx operating company or delete
del track.SelectionDetails.OperatingCompany
# Can optionally set the TrackingNumberUniqueIdentifier
# del track.SelectionDetails.TrackingNumberUniqueIdentifier
# If you'd like to see some documentation on the ship service WSDL, un-comment
# this line. (Spammy).
# print(track.client)
# Un-comment this to see your complete, ready-to-send request as it stands
# before it is actually sent. This is useful for seeing what values you can
# change.
# print(track.SelectionDetails)
# print(track.ClientDetail)
# print(track.TransactionDetail)
# Fires off the request, sets the 'response' attribute on the object.
track.send_request()
# This will show the reply to your track request being sent. You can access the
# attributes through the response attribute on the request object. This is
# good to un-comment to see the variables returned by the FedEx reply.
print(track.response)
# This will convert the response to a python dict object. To
# make it easier to work with.
# from fedex.tools.conversion import basic_sobject_to_dict
# print(basic_sobject_to_dict(track.response))
# This will dump the response data dict to json.
# from fedex.tools.conversion import sobject_to_json
# print(basic_sobject_to_dict(track.response))
# Look through the matches (there should only be one for a tracking number
# query), and show a few details about each shipment.
print("== Results ==")
for match in track.response.CompletedTrackDetails[0].TrackDetails:
print("Tracking #: {}".format(match.TrackingNumber))
if hasattr(match, 'TrackingNumberUniqueIdentifier'):
print("Tracking # UniqueID: {}".format(match.TrackingNumberUniqueIdentifier))
if hasattr(match, 'StatusDetail.Description'):
print("Status Description: {}".format(match.StatusDetail.Description))
if hasattr(match, 'StatusDetail.AncillaryDetails'):
print("Status AncillaryDetails Reason: {}".format(match.StatusDetail.AncillaryDetails[-1].Reason))
print("Status AncillaryDetails Description: {}"
"".format(match.StatusDetail.AncillaryDetails[-1].ReasonDescription))
if hasattr(match, 'ServiceCommitMessage'):
print("Commit Message: {}".format(match.ServiceCommitMessage))
if hasattr(match, 'Notification'):
print("Notification Severity: {}".format(match.Notification.Severity))
print("Notification Code: {}".format(match.Notification.Code))
print("Notification Message: {}".format(match.Notification.Message))
print("")
event_details = []
if hasattr(match, 'Events'):
for j in range(len(match.Events)):
event_match = match.Events[j]
event_details.append({'created': event_match.Timestamp, 'type': event_match.EventType,
'description': event_match.EventDescription})
if hasattr(event_match, 'StatusExceptionDescription'):
event_details[j]['exception_description'] = event_match.StatusExceptionDescription
print("Event {}: {}".format(j + 1, event_details[j]))