Skip to content

Commit 73b8d13

Browse files
committed
added json and dict response output
1 parent d3ad0af commit 73b8d13

13 files changed

+238
-82
lines changed

examples/address_validation.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class can handle up to 100 addresses for validation.
1212
# NOTE: TO USE ADDRESS VALIDATION SERVICES, YOU NEED TO REQUEST FEDEX TO ENABLE THIS SERVICE FOR YOUR ACCOUNT.
1313
# BY DEFAULT, THE SERVICE IS DISABLED AND YOU WILL RECEIVE AUTHENTICATION FAILED, 1000 RESPONSE.
1414

15-
# Set this to the INFO level to see the response from Fedex printed in stdout.
15+
# Un-comment to see the response from Fedex printed in stdout.
1616
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1717

1818
# This is the object that will be handling our avs request.
@@ -64,7 +64,16 @@ class can handle up to 100 addresses for validation.
6464
avs_request.send_request()
6565

6666
# good to un-comment to see the variables returned by the Fedex reply.
67-
print(avs_request.response)
67+
# print(avs_request.response)
68+
69+
# This will convert the response to a python dict object. To
70+
# make it easier to work with.
71+
# from fedex.tools.response_tools import basic_sobject_to_dict
72+
# print(basic_sobject_to_dict(avs_request.response))
73+
74+
# This will dump the response data dict to json.
75+
# from fedex.tools.response_tools import sobject_to_json
76+
# print(sobject_to_json(avs_request.response))
6877

6978
# Overall end result of the query
7079
for i in range(len(avs_request.response.AddressResults)):

examples/create_freight_shipment.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
# Valid choices for this example are PDF, PNG
2020
GENERATE_IMAGE_TYPE = 'PDF'
2121

22-
# Set this to the INFO level to see the response from Fedex printed in stdout.
23-
# logging.basicConfig(filename="suds.log", level=logging.DEBUG)
22+
# Un-comment to see the response from Fedex printed in stdout.
2423
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
2524

2625
# NOTE: A VALID 'freight_account_number' REQUIRED IN YOUR 'CONFIB_OBJ' FOR THIS SERVICE TO WORK.
@@ -147,7 +146,18 @@
147146
# This will show the reply to your shipment being sent. You can access the
148147
# attributes through the response attribute on the request object. This is
149148
# good to un-comment to see the variables returned by the Fedex reply.
150-
print(shipment.response)
149+
# print(shipment.response)
150+
151+
# This will convert the response to a python dict object. To
152+
# make it easier to work with. Also see basic_sobject_to_dict, it's faster but lacks options.
153+
# from fedex.tools.response_tools import sobject_to_dict
154+
# response_dict = sobject_to_dict(shipment.response)
155+
# response_dict['CompletedShipmentDetail']['ShipmentDocuments'][0]['Parts'][0]['Image'] = ''
156+
# print(response_dict) # Image is empty string for display purposes.
157+
158+
# This will dump the response data dict to json.
159+
# from fedex.tools.response_tools import sobject_to_json
160+
# print(sobject_to_json(shipment.response))
151161

152162
# Here is the overall end result of the query.
153163
print("HighestSeverity: {}".format(shipment.response.HighestSeverity))

examples/create_shipment.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Valid choices for this example are PDF, PNG
2020
GENERATE_IMAGE_TYPE = 'PDF'
2121

22-
# Set this to the INFO level to see the response from Fedex printed in stdout.
22+
# Un-comment to see the response from Fedex printed in stdout.
2323
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
2424

2525
# This is the object that will be handling our shipment request.
@@ -147,7 +147,18 @@
147147
# This will show the reply to your shipment being sent. You can access the
148148
# attributes through the response attribute on the request object. This is
149149
# good to un-comment to see the variables returned by the Fedex reply.
150-
print(shipment.response)
150+
# print(shipment.response)
151+
152+
# This will convert the response to a python dict object. To
153+
# make it easier to work with. Also see basic_sobject_to_dict, it's faster but lacks options.
154+
# from fedex.tools.response_tools import sobject_to_dict
155+
# response_dict = sobject_to_dict(shipment.response)
156+
# response_dict['CompletedShipmentDetail']['CompletedPackageDetails'][0]['Label']['Parts'][0]['Image'] = ''
157+
# print(response_dict) # Image is empty string for display purposes.
158+
159+
# This will dump the response data dict to json.
160+
# from fedex.tools.response_tools import sobject_to_json
161+
# print(sobject_to_json(shipment.response))
151162

152163
# Here is the overall end result of the query.
153164
print("HighestSeverity: {}".format(shipment.response.HighestSeverity))

examples/delete_shipment.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
from example_config import CONFIG_OBJ
99
from fedex.services.ship_service import FedexDeleteShipmentRequest
10+
from fedex.base_service import FedexError
1011

11-
# Set this to the INFO level to see the response from Fedex printed in stdout.
12+
# Un-comment to see the response from Fedex printed in stdout.
1213
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1314

1415
# This is the object that will be handling our request.
@@ -21,15 +22,33 @@
2122
del_request.DeletionControlType = "DELETE_ALL_PACKAGES"
2223

2324
# The tracking number of the shipment to delete.
24-
del_request.TrackingId.TrackingNumber = '794798682968'
25+
del_request.TrackingId.TrackingNumber = '794798682968' # '111111111111' will also not delete
2526

2627
# What kind of shipment the tracking number used.
2728
# Docs say this isn't required, but the WSDL won't validate without it.
2829
# EXPRESS, GROUND, or USPS
2930
del_request.TrackingId.TrackingIdType = 'EXPRESS'
3031

3132
# Fires off the request, sets the 'response' attribute on the object.
32-
del_request.send_request()
33+
try:
34+
del_request.send_request()
35+
except FedexError as e:
36+
if 'Unable to retrieve record' in str(e):
37+
print "WARNING: Unable to delete the shipment with the provided tracking number."
38+
else:
39+
print(e)
3340

3441
# See the response printed out.
35-
print(del_request.response)
42+
# print(del_request.response)
43+
44+
# This will convert the response to a python dict object. To
45+
# make it easier to work with.
46+
# from fedex.tools.response_tools import basic_sobject_to_dict
47+
# print(basic_sobject_to_dict(del_request.response))
48+
49+
# This will dump the response data dict to json.
50+
# from fedex.tools.response_tools import sobject_to_json
51+
# print(sobject_to_json(del_request.response))
52+
53+
# Here is the overall end result of the query.
54+
print("HighestSeverity: {}".format(del_request.response.HighestSeverity))

examples/freight_rate_request.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from example_config import CONFIG_OBJ
1515
from fedex.services.rate_service import FedexRateServiceRequest
1616

17-
# Set this to the INFO level to see the response from Fedex printed in stdout.
17+
# Un-comment to see the response from Fedex printed in stdout.
1818
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1919

2020
# This is the object that will be handling our request.
@@ -114,17 +114,26 @@
114114
# This will show the reply to your rate_request being sent. You can access the
115115
# attributes through the response attribute on the request object. This is
116116
# good to un-comment to see the variables returned by the FedEx reply.
117-
print(rate_request.response)
117+
# print(rate_request.response)
118+
119+
# This will convert the response to a python dict object. To
120+
# make it easier to work with.
121+
# from fedex.tools.response_tools import basic_sobject_to_dict
122+
# print(basic_sobject_to_dict(rate_request.response))
123+
124+
# This will dump the response data dict to json.
125+
# from fedex.tools.response_tools import sobject_to_json
126+
# print(sobject_to_json(rate_request.response))
118127

119128
# Here is the overall end result of the query.
120-
print("HighestSeverity:", rate_request.response.HighestSeverity)
129+
print("HighestSeverity: {}".format(rate_request.response.HighestSeverity))
121130

122131
# RateReplyDetails can contain rates for multiple ServiceTypes if ServiceType was set to None
123132
for service in rate_request.response.RateReplyDetails:
124133
for detail in service.RatedShipmentDetails:
125134
for surcharge in detail.ShipmentRateDetail.Surcharges:
126135
if surcharge.SurchargeType == 'OUT_OF_DELIVERY_AREA':
127-
print("%s: ODA rate_request charge {}".format(service.ServiceType, surcharge.Amount.Amount))
136+
print("{}: ODA rate_request charge {}".format(service.ServiceType, surcharge.Amount.Amount))
128137

129138
for rate_detail in service.RatedShipmentDetails:
130139
print("{}: Net FedEx Charge {} {}".format(service.ServiceType,

examples/location_request.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
from example_config import CONFIG_OBJ
1313
from fedex.services.location_service import FedexSearchLocationRequest
14-
from fedex.tools.response_tools import sobject_to_dict
14+
from fedex.tools.conversion import sobject_to_dict
1515

16-
# Set this to the INFO level to see the response from Fedex printed in stdout.
16+
# Un-comment to see the response from Fedex printed in stdout.
1717
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1818

1919
# This is the object that will be handling our request.
@@ -53,7 +53,12 @@
5353

5454
# This will convert the response to a python dict object. To
5555
# make it easier to work with.
56-
print(sobject_to_dict(location_request.response))
56+
# from fedex.tools.response_tools import basic_sobject_to_dict
57+
# print(basic_sobject_to_dict(location_request.response))
58+
59+
# This will dump the response data dict to json.
60+
# from fedex.tools.response_tools import sobject_to_json
61+
# print(sobject_to_json(location_request.response))
5762

5863
# Here is the overall end result of the query.
5964
print("HighestSeverity: {}".format(location_request.response.HighestSeverity))
@@ -85,7 +90,7 @@
8590

8691
if hasattr(location_detail, 'Attributes'):
8792
for attribute in location_detail.Attributes:
88-
print "Attribute: {}".format(attribute)
93+
print("Attribute: {}".format(attribute))
8994

9095
print("MapUrl {}".format(getattr(location_detail, 'MapUrl')))
9196

examples/postal_inquiry.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
from example_config import CONFIG_OBJ
1010
from fedex.services.package_movement import PostalCodeInquiryRequest
11+
from fedex.tools.conversion import sobject_to_dict
1112

12-
# Set this to the INFO level to see the response from Fedex printed in stdout.
13+
# Un-comment to see the response from Fedex printed in stdout.
1314
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1415

1516
# We're using the FedexConfig object from example_config.py in this dir.
@@ -32,4 +33,18 @@
3233
inquiry.send_request()
3334

3435
# See the response printed out.
35-
print(inquiry.response)
36+
# print(inquiry.response)
37+
38+
# This will convert the response to a python dict object. To
39+
# make it easier to work with.
40+
# from fedex.tools.conversion import basic_sobject_to_dict
41+
# print(basic_sobject_to_dict(inquiry.response))
42+
43+
# This will dump the response data dict to json.
44+
# from fedex.tools.conversion import sobject_to_json
45+
# print(sobject_to_json(inquiry.response))
46+
47+
# Here is the overall end result of the query.
48+
print("HighestSeverity: {}".format(inquiry.response.HighestSeverity))
49+
print("ExpressFreightContractorDeliveryArea: {}".format(sobject_to_dict(inquiry.response.ExpressDescription)))
50+
print("ExpressDescription: {}".format(sobject_to_dict(inquiry.response.ExpressFreightDescription)))

examples/rate_request.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
from example_config import CONFIG_OBJ
1515
from fedex.services.rate_service import FedexRateServiceRequest
16+
from fedex.tools.conversion import sobject_to_dict
1617

17-
# Set this to the INFO level to see the response from Fedex printed in stdout.
18+
# Un-comment to see the response from Fedex printed in stdout.
1819
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1920

2021
# This is the object that will be handling our request.
@@ -98,17 +99,36 @@
9899
# good to un-comment to see the variables returned by the FedEx reply.
99100
# print(rate_request.response)
100101

102+
# This will convert the response to a python dict object. To
103+
# make it easier to work with.
104+
# from fedex.tools.conversion import basic_sobject_to_dict
105+
# print(basic_sobject_to_dict(rate_request.response))
106+
107+
# This will dump the response data dict to json.
108+
# from fedex.tools.conversion import sobject_to_json
109+
# print(sobject_to_json(rate_request.response))
110+
101111
# Here is the overall end result of the query.
102-
print("HighestSeverity:", rate_request.response.HighestSeverity)
112+
print("HighestSeverity: {}".format(rate_request.response.HighestSeverity))
103113

104114
# RateReplyDetails can contain rates for multiple ServiceTypes if ServiceType was set to None
105115
for service in rate_request.response.RateReplyDetails:
106116
for detail in service.RatedShipmentDetails:
107117
for surcharge in detail.ShipmentRateDetail.Surcharges:
108118
if surcharge.SurchargeType == 'OUT_OF_DELIVERY_AREA':
109-
print("%s: ODA rate_request charge %s" % (service.ServiceType, surcharge.Amount.Amount))
119+
print("{}: ODA rate_request charge {}".format(service.ServiceType, surcharge.Amount.Amount))
110120

111121
for rate_detail in service.RatedShipmentDetails:
112-
print("%s: Net FedEx Charge %s %s" % (service.ServiceType,
113-
rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Currency,
114-
rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Amount))
122+
print("{}: Net FedEx Charge {} {}".format(service.ServiceType,
123+
rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Currency,
124+
rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Amount))
125+
126+
# Not sure if 'NOTE' checking should be put in base class.
127+
# For now can check notifications manually.
128+
# if notification.Severity == 'NOTE':
129+
# self.logger.warning(FedexFailure(notification.Code,
130+
# notification.Message))
131+
if rate_request.response.HighestSeverity == 'NOTE':
132+
for notification in rate_request.response.Notifications:
133+
if notification.Severity == 'NOTE':
134+
print(sobject_to_dict(notification))

examples/service_availability_request.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from example_config import CONFIG_OBJ
1414
from fedex.services.availability_commitment_service import FedexAvailabilityCommitmentRequest
1515

16-
# Set this to the INFO level to see the response from Fedex printed in stdout.
16+
# Un-comment to see the response from Fedex printed in stdout.
1717
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1818

1919
# This is the object that will be handling our service availability request.
@@ -56,7 +56,16 @@
5656
# This will show the reply to your avc_request being sent. You can access the
5757
# attributes through the response attribute on the request object. This is
5858
# good to un-comment to see the variables returned by the FedEx reply.
59-
print(avc_request.response)
59+
# print(avc_request.response)
60+
61+
# This will convert the response to a python dict object. To
62+
# make it easier to work with.
63+
# from fedex.tools.conversion import basic_sobject_to_dict
64+
# print(basic_sobject_to_dict(avc_request.response))
65+
66+
# This will dump the response data dict to json.
67+
# from fedex.tools.conversion import sobject_to_json
68+
# print(basic_sobject_to_dict(avc_request.response))
6069

6170
# Here is the overall end result of the query.
6271
print("HighestSeverity: {}".format(avc_request.response.HighestSeverity))

examples/track_shipment.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from example_config import CONFIG_OBJ
99
from fedex.services.track_service import FedexTrackRequest
1010

11-
# Set this to the INFO level to see the response from Fedex printed in stdout.
11+
# Un-comment to see the response from Fedex printed in stdout.
1212
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1313

1414
# NOTE: TRACKING IS VERY ERRATIC ON THE TEST SERVERS. YOU MAY NEED TO USE
@@ -50,6 +50,15 @@
5050
# good to un-comment to see the variables returned by the FedEx reply.
5151
print(track.response)
5252

53+
# This will convert the response to a python dict object. To
54+
# make it easier to work with.
55+
# from fedex.tools.conversion import basic_sobject_to_dict
56+
# print(basic_sobject_to_dict(track.response))
57+
58+
# This will dump the response data dict to json.
59+
# from fedex.tools.conversion import sobject_to_json
60+
# print(basic_sobject_to_dict(track.response))
61+
5362
# Look through the matches (there should only be one for a tracking number
5463
# query), and show a few details about each shipment.
5564
print("== Results ==")

0 commit comments

Comments
 (0)