|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This example shows how to use the FedEx RateRequest service. |
| 4 | +The variables populated below represents the minimum required values. |
| 5 | +You will need to fill all of these, or risk seeing a SchemaValidationError |
| 6 | +exception thrown by suds. |
| 7 | +
|
| 8 | +TIP: Near the bottom of the module, see how to check the if the destination |
| 9 | + is Out of Delivery Area (ODA). |
| 10 | +""" |
| 11 | +import logging |
| 12 | +from example_config import CONFIG_OBJ |
| 13 | +from fedex.services.rate_service import FedexRateServiceRequest |
| 14 | + |
| 15 | +# Set this to the INFO level to see the response from Fedex printed in stdout. |
| 16 | +logging.basicConfig(level=logging.INFO) |
| 17 | + |
| 18 | + |
| 19 | +# This is the object that will be handling our tracking request. |
| 20 | +# We're using the FedexConfig object from example_config.py in this dir. |
| 21 | +customer_transaction_id = "*** RateService Request v18 using Python ***" # Optional transaction_id |
| 22 | +rate_request = FedexRateServiceRequest(CONFIG_OBJ, customer_transaction_id=customer_transaction_id) |
| 23 | + |
| 24 | +# If you wish to have transit data returned with your request you |
| 25 | +# need to uncomment the following |
| 26 | +# rate_request.ReturnTransitAndCommit = True |
| 27 | + |
| 28 | +# This is very generalized, top-level information. |
| 29 | +# REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER or STATION |
| 30 | +rate_request.RequestedShipment.DropoffType = 'REGULAR_PICKUP' |
| 31 | + |
| 32 | +# See page 355 in WS_ShipService.pdf for a full list. Here are the common ones: |
| 33 | +# STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, FEDEX_EXPRESS_SAVER |
| 34 | +# To receive rates for multiple ServiceTypes set to None. |
| 35 | +rate_request.RequestedShipment.ServiceType = 'FEDEX_GROUND' |
| 36 | + |
| 37 | +# What kind of package this will be shipped in. |
| 38 | +# FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING |
| 39 | +rate_request.RequestedShipment.PackagingType = 'YOUR_PACKAGING' |
| 40 | + |
| 41 | +# Shipper's address |
| 42 | +rate_request.RequestedShipment.Shipper.Address.PostalCode = '29631' |
| 43 | +rate_request.RequestedShipment.Shipper.Address.CountryCode = 'US' |
| 44 | +rate_request.RequestedShipment.Shipper.Address.Residential = False |
| 45 | + |
| 46 | +# Recipient address |
| 47 | +rate_request.RequestedShipment.Recipient.Address.PostalCode = '27577' |
| 48 | +rate_request.RequestedShipment.Recipient.Address.CountryCode = 'US' |
| 49 | +# This is needed to ensure an accurate rate quote with the response. |
| 50 | +#rate_request.RequestedShipment.Recipient.Address.Residential = True |
| 51 | +#include estimated duties and taxes in rate quote, can be ALL or NONE |
| 52 | +rate_request.RequestedShipment.EdtRequestType = 'NONE' |
| 53 | + |
| 54 | +# Who pays for the rate_request? |
| 55 | +# RECIPIENT, SENDER or THIRD_PARTY |
| 56 | +rate_request.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER' |
| 57 | + |
| 58 | +package1_weight = rate_request.create_wsdl_object_of_type('Weight') |
| 59 | +# Weight, in LB. |
| 60 | +package1_weight.Value = 1.0 |
| 61 | +package1_weight.Units = "LB" |
| 62 | + |
| 63 | +package1 = rate_request.create_wsdl_object_of_type('RequestedPackageLineItem') |
| 64 | +package1.Weight = package1_weight |
| 65 | +#can be other values this is probably the most common |
| 66 | +package1.PhysicalPackaging = 'BOX' |
| 67 | +# Required, but according to FedEx docs: |
| 68 | +# "Used only with PACKAGE_GROUPS, as a count of packages within a |
| 69 | +# group of identical packages". In practice you can use this to get rates |
| 70 | +# for a shipment with multiple packages of an identical package size/weight |
| 71 | +# on rate request without creating multiple RequestedPackageLineItem elements. |
| 72 | +# You can OPTIONALLY specify a package group: |
| 73 | +# package1.GroupNumber = 0 # default is 0 |
| 74 | +# The result will be found in RatedPackageDetail, with specified GroupNumber. |
| 75 | +package1.GroupPackageCount = 1 |
| 76 | +# Un-comment this to see the other variables you may set on a package. |
| 77 | +#print(package1) |
| 78 | + |
| 79 | +# This adds the RequestedPackageLineItem WSDL object to the rate_request. It |
| 80 | +# increments the package count and total weight of the rate_request for you. |
| 81 | +rate_request.add_package(package1) |
| 82 | + |
| 83 | +# If you'd like to see some documentation on the ship service WSDL, un-comment |
| 84 | +# this line. (Spammy). |
| 85 | +#print(rate_request.client) |
| 86 | + |
| 87 | +# Un-comment this to see your complete, ready-to-send request as it stands |
| 88 | +# before it is actually sent. This is useful for seeing what values you can |
| 89 | +# change. |
| 90 | +#print(rate_request.RequestedShipment) |
| 91 | + |
| 92 | +# Fires off the request, sets the 'response' attribute on the object. |
| 93 | +rate_request.send_request() |
| 94 | + |
| 95 | +# This will show the reply to your rate_request being sent. You can access the |
| 96 | +# attributes through the response attribute on the request object. This is |
| 97 | +# good to un-comment to see the variables returned by the FedEx reply. |
| 98 | +#print(rate_request.response) |
| 99 | + |
| 100 | +# Here is the overall end result of the query. |
| 101 | +print("HighestSeverity:", rate_request.response.HighestSeverity) |
| 102 | + |
| 103 | +# RateReplyDetails can contain rates for multiple ServiceTypes if ServiceType was set to None |
| 104 | +for service in rate_request.response.RateReplyDetails: |
| 105 | + for detail in service.RatedShipmentDetails: |
| 106 | + for surcharge in detail.ShipmentRateDetail.Surcharges: |
| 107 | + if surcharge.SurchargeType == 'OUT_OF_DELIVERY_AREA': |
| 108 | + print("%s: ODA rate_request charge %s" % (service.ServiceType, surcharge.Amount.Amount)) |
| 109 | + |
| 110 | + for rate_detail in service.RatedShipmentDetails: |
| 111 | + print("%s: Net FedEx Charge %s %s" % (service.ServiceType, |
| 112 | + rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Currency, |
| 113 | + rate_detail.ShipmentRateDetail.TotalNetFedExCharge.Amount)) |
| 114 | + |
0 commit comments