Skip to content

Commit b8680b2

Browse files
committed
improved output for location service example, updated doc
1 parent c2106ff commit b8680b2

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

examples/location_request.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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
1415

1516
# Set this to the INFO level to see the response from Fedex printed in stdout.
1617
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
@@ -26,6 +27,7 @@
2627
location_request.MultipleMatchesAction = 'RETURN_ALL'
2728

2829
# Set constraints, see SearchLocationConstraints definition.
30+
# For LocationTypesToInclude, see FedExLocationType definition.
2931
location_request.Constraints.LocationTypesToInclude = ['FEDEX_SELF_SERVICE_LOCATION',
3032
'FEDEX_AUTHORIZED_SHIP_CENTER']
3133

@@ -49,5 +51,54 @@
4951
# good to un-comment to see the variables returned by the FedEx reply.
5052
# print(location_request.response)
5153

54+
# This will convert the response to a python dict object. To
55+
# make it easier to work with.
56+
print(sobject_to_dict(location_request.response))
57+
5258
# Here is the overall end result of the query.
53-
print("HighestSeverity:", location_request.response.HighestSeverity)
59+
print("HighestSeverity: {}".format(location_request.response.HighestSeverity))
60+
print("TotalResultsAvailable: {}".format(location_request.response.TotalResultsAvailable))
61+
print("ResultsReturned: {}".format(location_request.response.ResultsReturned))
62+
63+
result = location_request.response.AddressToLocationRelationships[0]
64+
print("MatchedAddress: {}, {} Residential: {}".format(result.MatchedAddress.PostalCode,
65+
result.MatchedAddress.CountryCode,
66+
result.MatchedAddress.Residential))
67+
print("MatchedAddressGeographicCoordinates: {}".format(result.MatchedAddressGeographicCoordinates.strip("/")))
68+
69+
# Locations sorted by closest found to furthest.
70+
locations = result.DistanceAndLocationDetails
71+
for location in locations:
72+
print("Distance: {}{}".format(location.Distance.Value, location.Distance.Units))
73+
74+
location_detail = location.LocationDetail
75+
print("LocationID: {}".format(location_detail.LocationId))
76+
print("StoreNumber: {}".format(location_detail.StoreNumber))
77+
78+
if hasattr(location_detail, 'LocationContactAndAddress'):
79+
contact_and_address = location_detail.LocationContactAndAddress
80+
contact_and_address = sobject_to_dict(contact_and_address)
81+
print("LocationContactAndAddress Dict: {}".format(contact_and_address))
82+
83+
print("GeographicCoordinates {}".format(getattr(location_detail, 'GeographicCoordinates')))
84+
print("LocationType {}".format(getattr(location_detail, 'LocationType')))
85+
86+
if hasattr(location_detail, 'Attributes'):
87+
for attribute in location_detail.Attributes:
88+
print "Attribute: {}".format(attribute)
89+
90+
print("MapUrl {}".format(getattr(location_detail, 'MapUrl')))
91+
92+
if hasattr(location_detail, 'NormalHours'):
93+
for open_time in location_detail.NormalHours:
94+
print("NormalHours Dict: {}".format(sobject_to_dict(open_time)))
95+
96+
if hasattr(location_detail, 'HoursForEffectiveDate'):
97+
for effective_open_time in location_detail.HoursForEffectiveDate:
98+
print("HoursForEffectiveDate Dict: {}".format(sobject_to_dict(effective_open_time)))
99+
100+
if hasattr(location_detail, 'CarrierDetails'):
101+
for carrier_detail in location_detail.CarrierDetails:
102+
print("CarrierDetails Dict: {}".format(sobject_to_dict(carrier_detail)))
103+
104+
print("")

fedex/services/location_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
class FedexSearchLocationRequest(FedexBaseService):
1414
"""
15-
This class allows you validate service availability
15+
This class allows you to figure out a FedEx location closest
16+
to a specified location, based on location type. The response includes
17+
location details like operating times, directions and a map link.
1618
"""
1719

1820
def __init__(self, config_obj, *args, **kwargs):

fedex/tools/__init__.py

Whitespace-only changes.

fedex/tools/response_tools.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Response output conversion tools to help parse suds
2+
response object output.
3+
"""
4+
from suds.sudsobject import asdict
5+
from suds.sax.text import Text
6+
7+
8+
def object_to_dict(obj):
9+
""" Converts a suds object to a dictionary.
10+
:param o: object
11+
:return: dictionary
12+
"""
13+
out = {}
14+
for k, v in asdict(obj).items():
15+
k = k.lower()
16+
if hasattr(v, '__keylist__'):
17+
out[k] = object_to_dict(v)
18+
elif isinstance(v, list):
19+
out[k] = []
20+
for item in v:
21+
if hasattr(item, '__keylist__'):
22+
out[k].append(object_to_dict(item))
23+
else:
24+
out[k].append(
25+
item.title() if isinstance(item, Text) else item)
26+
else:
27+
out[k] = v.title() if isinstance(v, Text) else v
28+
return out
29+
#
30+
# import datetime
31+
#
32+
# def object_to_dict(obj):
33+
# """ Converts an object to a dictionary.
34+
# :param o: object
35+
# :return: dictionary
36+
# """
37+
# if isinstance(obj, (str, unicode, bool, int, long, float, datetime.datetime, datetime.date, datetime.time)):
38+
# return obj
39+
# data_dict = {}
40+
# try:
41+
# all_keys = obj.__dict__.keys() # vars(obj).keys()
42+
# except AttributeError:
43+
# return obj
44+
# fields = [k for k in all_keys if not k.startswith('_')]
45+
# for field in fields:
46+
# val = getattr(obj, field)
47+
# if isinstance(val, (list, tuple)):
48+
# data_dict[field] = []
49+
# for item in val:
50+
# data_dict[field].append(object_to_dict(item))
51+
# else:
52+
# data_dict[field] = object_to_dict(val)
53+
# return data_dict

0 commit comments

Comments
 (0)