Skip to content

Commit 205c885

Browse files
committed
add country service example and test
1 parent 2e4763d commit 205c885

File tree

5 files changed

+1075
-0
lines changed

5 files changed

+1075
-0
lines changed

examples/country_postal_inquiry.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
"""
3+
ValidatePostalRequest classes are used to validate and receive additional
4+
information about postal codes.
5+
"""
6+
import logging
7+
from example_config import CONFIG_OBJ
8+
from fedex.services.country_service import FedexValidatePostalRequest
9+
10+
# Set this to the INFO level to see the response from Fedex printed in stdout.
11+
logging.basicConfig(level=logging.INFO)
12+
13+
# We're using the FedexConfig object from example_config.py in this dir.
14+
customer_transaction_id = "*** ValidatePostal Request v4 using Python ***" # Optional transaction_id
15+
inquiry = FedexValidatePostalRequest(CONFIG_OBJ, customer_transaction_id=customer_transaction_id)
16+
inquiry.Address.PostalCode = '29631'
17+
inquiry.Address.CountryCode = 'US'
18+
inquiry.Address.StreetLines = ['104 Knox Road']
19+
inquiry.Address.City = 'Clemson'
20+
inquiry.Address.StateOrProvinceCode = 'SC'
21+
22+
# If you'd like to see some documentation on the ship service WSDL, un-comment
23+
# this line. (Spammy).
24+
# print(inquiry.client)
25+
26+
# Un-comment this to see your complete, ready-to-send request as it stands
27+
# before it is actually sent. This is useful for seeing what values you can
28+
# change.
29+
# print(inquiry.CarrierCode)
30+
# print(inquiry.Address)
31+
# print(inquiry.ShipDateTime)
32+
# print(inquiry.CheckForMismatch)
33+
# print(inquiry.RoutingCode)
34+
35+
# Fires off the request, sets the 'response' attribute on the object.
36+
inquiry.send_request()
37+
38+
# See the response printed out.
39+
print(inquiry.response)
40+
41+
# Here is the overall end result of the query.
42+
print("HighestSeverity: {}".format(inquiry.response.HighestSeverity))
43+
print("")
44+
45+
print("State/Province: {}".format(inquiry.response.PostalDetail.StateOrProvinceCode))
46+
print("City First Initial: {}".format(inquiry.response.PostalDetail.CityFirstInitials))
47+
print("Clean Postal Code: {}".format(inquiry.response.PostalDetail.CleanedPostalCode))
48+
49+
for loc_description in inquiry.response.PostalDetail.LocationDescriptions:
50+
print("Location ID: {}".format(loc_description.LocationId))
51+
print("Location No.: {}".format(loc_description.LocationNumber))
52+
print("Country Code: {}".format(loc_description.CountryCode))
53+
print("Postal Code: {}".format(loc_description.PostalCode))
54+
print("Service Area: {}".format(loc_description.ServiceArea))
55+
print("Airport ID: {}".format(loc_description.AirportId))
56+
print("FedEx Europe First Origin: {}".format(loc_description.FedExEuropeFirstOrigin))
57+

fedex/services/country_service.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Service Availability and Commitment Module
3+
=================================
4+
This package contains the shipping methods defined by Fedex's
5+
ValidationAvailabilityAndCommitmentService WSDL file. Each is encapsulated in a class for
6+
easy access. For more details on each, refer to the respective class's
7+
documentation.
8+
"""
9+
10+
import datetime
11+
from ..base_service import FedexBaseService
12+
13+
14+
class FedexAvailabilityCommitmentRequest(FedexBaseService):
15+
"""
16+
This class allows you validate service availability
17+
"""
18+
19+
def __init__(self, config_obj, *args, **kwargs):
20+
"""
21+
@type config_obj: L{FedexConfig}
22+
@param config_obj: A valid FedexConfig object.
23+
"""
24+
25+
self._config_obj = config_obj
26+
# Holds version info for the VersionId SOAP object.
27+
self._version_info = {
28+
'service_id': 'vacs',
29+
'major': '4',
30+
'intermediate': '0',
31+
'minor': '0'
32+
}
33+
34+
"""ivar: Carrier Code Default to Fedex (FDXE), or can bbe FDXG."""
35+
self.CarrierCode = None
36+
37+
"""@ivar: Holds Addresses, Ship Date, Service and Packaging objects."""
38+
self.Origin = self.Destination = None
39+
self.ShipDate = None
40+
self.Service = None
41+
self.Packaging = None
42+
43+
"""@ivar: Holds the ValidationAvailabilityAndCommitmentService WSDL object."""
44+
# Call the parent FedexBaseService class for basic setup work.
45+
# Shortened the name of the wsdl, otherwise suds did not load it properly.
46+
# Suds throws the following error when using the long file name from FedEx:
47+
#
48+
# File "/Library/Python/2.7/site-packages/suds/wsdl.py", line 878, in resolve
49+
# raise Exception("binding '%s', not-found" % p.binding)
50+
# Exception: binding 'ns:ValidationAvailabilityAndCommitmentServiceSoapBinding', not-found
51+
52+
super(FedexAvailabilityCommitmentRequest, self).__init__(
53+
self._config_obj, 'AvailabilityAndCommitmentService_v4.wsdl', *args, **kwargs)
54+
55+
def _prepare_wsdl_objects(self):
56+
"""
57+
Create the data structure and get it ready for the WSDL request.
58+
"""
59+
self.CarrierCode = 'FDXE'
60+
self.Origin = self.Destination = self.client.factory.create('Address')
61+
self.ShipDate = datetime.date.today().isoformat()
62+
self.Service = None
63+
self.Packaging = 'YOUR_PACKAGING'
64+
65+
def _assemble_and_send_request(self):
66+
"""
67+
Fires off the Fedex request.
68+
69+
@warning: NEVER CALL THIS METHOD DIRECTLY. CALL send_request(),
70+
WHICH RESIDES ON FedexBaseService AND IS INHERITED.
71+
"""
72+
73+
# We get an exception like this when specifying an IntegratorId:
74+
# suds.TypeNotFound: Type not found: 'IntegratorId'
75+
# Setting it to None does not seem to appease it.
76+
del self.ClientDetail.IntegratorId
77+
self.logger.debug(self.WebAuthenticationDetail)
78+
self.logger.debug(self.ClientDetail)
79+
self.logger.debug(self.TransactionDetail)
80+
self.logger.debug(self.VersionId)
81+
# Fire off the query.
82+
return self.client.service.serviceAvailability(
83+
WebAuthenticationDetail=self.WebAuthenticationDetail,
84+
ClientDetail=self.ClientDetail,
85+
TransactionDetail=self.TransactionDetail,
86+
Version=self.VersionId,
87+
Origin=self.Origin,
88+
Destination=self.Destination,
89+
ShipDate=self.ShipDate,
90+
CarrierCode=self.CarrierCode,
91+
Service=self.Service,
92+
Packaging=self.Packaging)

0 commit comments

Comments
 (0)