Skip to content

Commit f7bed81

Browse files
committed
added ship service v17, added ship test
1 parent a8dd4cf commit f7bed81

File tree

4 files changed

+212
-61
lines changed

4 files changed

+212
-61
lines changed

examples/create_freight_shipment.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212
from example_config import CONFIG_OBJ
1313
from fedex.services.ship_service import FedexProcessShipmentRequest
1414

15+
# What kind of file do you want this example to generate?
16+
# Valid choices for this example are PDF, PNG
17+
GENERATE_IMAGE_TYPE = 'PDF'
18+
19+
1520
# Set this to the INFO level to see the response from Fedex printed in stdout.
1621
#logging.basicConfig(filename="suds.log", level=logging.DEBUG)
1722
logging.basicConfig(level=logging.INFO)
23+
24+
# NOTE: A VALID 'freight_account_number' REQUIRED IN YOUR 'CONFIB_OBJ' FOR THIS SERVICE TO WORK.
25+
# OTHERWISE YOU WILL GET FEDEX FREIGHT OR ASSOCIATED ADDRESS IS REQUIRED, ERROR 3619.
26+
1827
# This is the object that will be handling our tracking request.
1928
# We're using the FedexConfig object from example_config.py in this dir.
2029
shipment = FedexProcessShipmentRequest(CONFIG_OBJ)
@@ -52,18 +61,21 @@
5261
# This is needed to ensure an accurate rate quote with the response.
5362
shipment.RequestedShipment.Recipient.Address.Residential = False
5463
shipment.RequestedShipment.FreightShipmentDetail.TotalHandlingUnits = 1
55-
shipment.RequestedShipment.ShippingChargesPayment.Payor.ResponsibleParty.AccountNumber = CONFIG_OBJ.freight_account_number
56-
57-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Contact.PersonName = 'Sender Name'
58-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Contact.CompanyName = 'Some Company'
59-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Contact.PhoneNumber = '9012638716'
60-
61-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.StreetLines = ['2000 Freight LTL Testing']
62-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.City = 'Harrison'
63-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.StateOrProvinceCode = 'AR'
64-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.PostalCode = '72601'
65-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.CountryCode = 'US'
66-
shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress.Address.Residential = False
64+
shipment.RequestedShipment.ShippingChargesPayment.Payor.ResponsibleParty.AccountNumber = \
65+
CONFIG_OBJ.freight_account_number
66+
67+
billing_contact_address = shipment.RequestedShipment.FreightShipmentDetail.FedExFreightBillingContactAndAddress
68+
69+
billing_contact_address.Contact.PersonName = 'Sender Name'
70+
billing_contact_address.Contact.CompanyName = 'Some Company'
71+
billing_contact_address.Contact.PhoneNumber = '9012638716'
72+
73+
billing_contact_address.Address.StreetLines = ['2000 Freight LTL Testing']
74+
billing_contact_address.Address.City = 'Harrison'
75+
billing_contact_address.Address.StateOrProvinceCode = 'AR'
76+
billing_contact_address.Address.PostalCode = '72601'
77+
billing_contact_address.Address.CountryCode = 'US'
78+
billing_contact_address.Address.Residential = False
6779
spec = shipment.create_wsdl_object_of_type('ShippingDocumentSpecification')
6880

6981
spec.ShippingDocumentTypes = [spec.CertificateOfOrigin]
@@ -93,6 +105,11 @@
93105
shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
94106
shipment.RequestedShipment.EdtRequestType = 'NONE'
95107

108+
# Delete the flags we don't want.
109+
# Can be SHIPPING_LABEL_FIRST, SHIPPING_LABEL_LAST or delete
110+
if hasattr(shipment.RequestedShipment.LabelSpecification, 'LabelOrder'):
111+
del shipment.RequestedShipment.LabelSpecification.LabelOrder # Delete, not using.
112+
96113
package1_weight = shipment.create_wsdl_object_of_type('Weight')
97114
package1_weight.Value = 500.0
98115
package1_weight.Units = "LB"
@@ -131,13 +148,17 @@
131148
# attributes through the response attribute on the request object. This is
132149
# good to un-comment to see the variables returned by the Fedex reply.
133150
print shipment.response
151+
#print shipment.client.last_received()
134152
# Here is the overall end result of the query.
135153
# print "HighestSeverity:", shipment.response.HighestSeverity
136154
# # Getting the tracking number from the new shipment.
137155
# print "Tracking #:", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].TrackingIds[0].TrackingNumber
138156
# # Net shipping costs.
139157
# print "Net Shipping Cost (US$):", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].PackageRating.PackageRateDetails[0].NetCharge.Amount
140158

159+
# See the request printed out.
160+
#print shipment.client.last_sent()
161+
141162
# # Get the label image in ASCII format from the reply. Note the list indices
142163
# we're using. You'll need to adjust or iterate through these if your shipment
143164
# has multiple packages.
@@ -148,12 +169,14 @@
148169
label_binary_data = binascii.a2b_base64(ascii_label_data)
149170

150171
"""
151-
This is an example of how to dump a label to a PNG file.
172+
This is an example of how to dump a label to a local file.
152173
"""
153174
# This will be the file we write the label out to.
154-
pdf_file = open('example_shipment_label.pdf', 'wb')
155-
pdf_file.write(label_binary_data)
156-
pdf_file.close()
175+
out_path = 'example_freight_shipment_label.%s' % GENERATE_IMAGE_TYPE.lower()
176+
print "Writing to file", out_path
177+
out_file = open(out_path, 'wb')
178+
out_file.write(label_binary_data)
179+
out_file.close()
157180

158181
"""
159182
This is an example of how to print the label to a serial printer. This will not
@@ -175,4 +198,4 @@
175198
#label_printer = serial.Serial(0)
176199
#print "SELECTED SERIAL PORT: "+ label_printer.portstr
177200
#label_printer.write(label_binary_data)
178-
#label_printer.close()
201+
#label_printer.close()

examples/create_shipment.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
"""
1010
import logging
1111
import binascii
12+
import datetime
13+
1214
from example_config import CONFIG_OBJ
1315
from fedex.services.ship_service import FedexProcessShipmentRequest
1416

17+
# What kind of file do you want this example to generate?
18+
# Valid choices for this example are PDF, PNG
19+
GENERATE_IMAGE_TYPE = 'PDF'
20+
21+
1522
# Set this to the INFO level to see the response from Fedex printed in stdout.
1623
logging.basicConfig(level=logging.INFO)
1724

1825
# This is the object that will be handling our tracking request.
1926
# We're using the FedexConfig object from example_config.py in this dir.
20-
shipment = FedexProcessShipmentRequest(CONFIG_OBJ)
27+
customer_transaction_id = "*** ShipService Request v17 using Python ***" # Optional transaction_id
28+
shipment = FedexProcessShipmentRequest(CONFIG_OBJ, customer_transaction_id=customer_transaction_id)
2129

2230
# This is very generalized, top-level information.
2331
# REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER or STATION
24-
shipment.RequestedShipment.DropoffType = 'REGULAR_PICKUP'
32+
shipment.RequestedShipment.DropoffType = 'BUSINESS_SERVICE_CENTER'
2533

2634
# See page 355 in WS_ShipService.pdf for a full list. Here are the common ones:
2735
# STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, FEDEX_EXPRESS_SAVER
2836
shipment.RequestedShipment.ServiceType = 'PRIORITY_OVERNIGHT'
2937

3038
# What kind of package this will be shipped in.
31-
# FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING
39+
# FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, FEDEX_ENVELOPE
3240
shipment.RequestedShipment.PackagingType = 'FEDEX_PAK'
3341

3442
# Shipper contact info.
@@ -55,11 +63,13 @@
5563
shipment.RequestedShipment.Recipient.Address.StateOrProvinceCode = 'VA'
5664
shipment.RequestedShipment.Recipient.Address.PostalCode = '20171'
5765
shipment.RequestedShipment.Recipient.Address.CountryCode = 'US'
58-
# This is needed to ensure an accurate rate quote with the response.
66+
# This is needed to ensure an accurate rate quote with the response. Use AddressValidation to get ResidentialStatus
5967
shipment.RequestedShipment.Recipient.Address.Residential = True
6068
shipment.RequestedShipment.EdtRequestType = 'NONE'
6169

70+
# Senders account information
6271
shipment.RequestedShipment.ShippingChargesPayment.Payor.ResponsibleParty.AccountNumber = CONFIG_OBJ.account_number
72+
6373
# Who pays for the shipment?
6474
# RECIPIENT, SENDER or THIRD_PARTY
6575
shipment.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER'
@@ -70,25 +80,36 @@
7080

7181
# Specifies which format the label file will be sent to you in.
7282
# DPL, EPL2, PDF, PNG, ZPLII
73-
shipment.RequestedShipment.LabelSpecification.ImageType = 'PNG'
83+
shipment.RequestedShipment.LabelSpecification.ImageType = GENERATE_IMAGE_TYPE
7484

7585
# To use doctab stocks, you must change ImageType above to one of the
7686
# label printer formats (ZPLII, EPL2, DPL).
7787
# See documentation for paper types, there quite a few.
78-
shipment.RequestedShipment.LabelSpecification.LabelStockType = 'PAPER_4X6'
88+
shipment.RequestedShipment.LabelSpecification.LabelStockType = 'PAPER_7X4.75'
7989

8090
# This indicates if the top or bottom of the label comes out of the
8191
# printer first.
8292
# BOTTOM_EDGE_OF_TEXT_FIRST or TOP_EDGE_OF_TEXT_FIRST
83-
shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
93+
# Timestamp in YYYY-MM-DDThh:mm:ss format, e.g. 2002-05-30T09:00:00
94+
shipment.RequestedShipment.ShipTimestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
8495

96+
# BOTTOM_EDGE_OF_TEXT_FIRST, TOP_EDGE_OF_TEXT_FIRST
97+
shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'TOP_EDGE_OF_TEXT_FIRST'
98+
99+
# Delete the flags we don't want.
100+
# Can be SHIPPING_LABEL_FIRST, SHIPPING_LABEL_LAST or delete
101+
if hasattr(shipment.RequestedShipment.LabelSpecification, 'LabelOrder'):
102+
del shipment.RequestedShipment.LabelSpecification.LabelOrder # Delete, not using.
103+
104+
# Create Weight, in pounds.
85105
package1_weight = shipment.create_wsdl_object_of_type('Weight')
86-
# Weight, in pounds.
87106
package1_weight.Value = 1.0
88107
package1_weight.Units = "LB"
89108

109+
# Create PackageLineItem
90110
package1 = shipment.create_wsdl_object_of_type('RequestedPackageLineItem')
91-
package1.PhysicalPackaging = 'BOX'
111+
# BAG, BARREL, BASKET, BOX, BUCKET, BUNDLE, CARTON, CASE, CONTAINER, ENVELOPE etc..
112+
package1.PhysicalPackaging = 'ENVELOPE'
92113
package1.Weight = package1_weight
93114
# Un-comment this to see the other variables you may set on a package.
94115
#print package1
@@ -99,17 +120,19 @@
99120

100121
# If you'd like to see some documentation on the ship service WSDL, un-comment
101122
# this line. (Spammy).
102-
#print shipment.client
123+
# print shipment.client
103124

104125
# Un-comment this to see your complete, ready-to-send request as it stands
105126
# before it is actually sent. This is useful for seeing what values you can
106127
# change.
107128
#print shipment.RequestedShipment
129+
#print shipment.ClientDetail
130+
#print shipment.TransactionDetail
108131

109132
# If you want to make sure that all of your entered details are valid, you
110133
# can call this and parse it just like you would via send_request(). If
111134
# shipment.response.HighestSeverity == "SUCCESS", your shipment is valid.
112-
#shipment.send_validation_request()
135+
#print shipment.send_validation_request()
113136

114137
# Fires off the request, sets the 'response' attribute on the object.
115138
shipment.send_request()
@@ -118,28 +141,42 @@
118141
# attributes through the response attribute on the request object. This is
119142
# good to un-comment to see the variables returned by the Fedex reply.
120143
print shipment.response
144+
#print shipment.client.last_received()
145+
146+
# See the request printed out.
147+
#print shipment.client.last_sent()
121148

122149
# Here is the overall end result of the query.
123150
print "HighestSeverity:", shipment.response.HighestSeverity
151+
124152
# Getting the tracking number from the new shipment.
125153
print "Tracking #:", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].TrackingIds[0].TrackingNumber
126-
# Net shipping costs.
127-
print "Net Shipping Cost (US$):", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].PackageRating.PackageRateDetails[0].NetCharge.Amount
154+
155+
# Net shipping costs. Only show if available. Sometimes sandbox will not include this in the response.
156+
CompletedPackageDetails = shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0]
157+
if hasattr(CompletedPackageDetails, 'PackageRating'):
158+
print "Net Shipping Cost (US$):", CompletedPackageDetails.PackageRating.PackageRateDetails[0].NetCharge.Amount
159+
else:
160+
print 'WARNING: Unable to get rate.'
128161

129162
# Get the label image in ASCII format from the reply. Note the list indices
130163
# we're using. You'll need to adjust or iterate through these if your shipment
131164
# has multiple packages.
165+
132166
ascii_label_data = shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].Label.Parts[0].Image
167+
133168
# Convert the ASCII data to binary.
134169
label_binary_data = binascii.a2b_base64(ascii_label_data)
135170

136171
"""
137-
This is an example of how to dump a label to a PNG file.
172+
This is an example of how to dump a label to a local file.
138173
"""
139174
# This will be the file we write the label out to.
140-
png_file = open('example_shipment_label.png', 'wb')
141-
png_file.write(label_binary_data)
142-
png_file.close()
175+
out_path = 'example_shipment_label.%s' % GENERATE_IMAGE_TYPE.lower()
176+
print "Writing to file", out_path
177+
out_file = open(out_path, 'wb')
178+
out_file.write(label_binary_data)
179+
out_file.close()
143180

144181
"""
145182
This is an example of how to print the label to a serial printer. This will not
@@ -161,4 +198,5 @@
161198
#label_printer = serial.Serial(0)
162199
#print "SELECTED SERIAL PORT: "+ label_printer.portstr
163200
#label_printer.write(label_binary_data)
164-
#label_printer.close()
201+
#label_printer.close()
202+

0 commit comments

Comments
 (0)