Package fedex :: Package services :: Module rate_service
[hide private]
[frames] | no frames]

Source Code for Module fedex.services.rate_service

  1  """ 
  2  Rate Service Module 
  3   
  4  This package contains classes to request pre-ship rating information and to 
  5  determine estimated or courtesy billing quotes. Time in Transit can be 
  6  returned with the rates if it is specified in the request. 
  7  """ 
  8   
  9  import datetime 
 10  from ..base_service import FedexBaseService 
 11   
 12   
13 -class FedexRateServiceRequest(FedexBaseService):
14 """ 15 This class allows you to get the shipping charges for a particular address. 16 You will need to populate the data structures in self.RequestedShipment, 17 then send the request. 18 """ 19
20 - def __init__(self, config_obj, *args, **kwargs):
21 """ 22 The optional keyword args detailed on L{FedexBaseService} 23 apply here as well. 24 25 @type config_obj: L{FedexConfig} 26 @param config_obj: A valid FedexConfig object. 27 """ 28 29 self._config_obj = config_obj 30 31 # Holds version info for the VersionId SOAP object. 32 self._version_info = {'service_id': 'crs', 'major': '18', 33 'intermediate': '0', 'minor': '0'} 34 35 self.RequestedShipment = None 36 """@ivar: Holds the RequestedShipment WSDL object including the shipper, recipient and shipt time.""" 37 # Call the parent FedexBaseService class for basic setup work. 38 super(FedexRateServiceRequest, self).__init__( 39 self._config_obj, 'RateService_v18.wsdl', *args, **kwargs) 40 self.ClientDetail.Region = config_obj.express_region_code 41 """@ivar: Holds the express region code from the config object."""
42
43 - def _prepare_wsdl_objects(self):
44 """ 45 This is the data that will be used to create your shipment. Create 46 the data structure and get it ready for the WSDL request. 47 """ 48 49 # Default behavior is to not request transit information 50 self.ReturnTransitAndCommit = False 51 52 # This is the primary data structure for processShipment requests. 53 self.RequestedShipment = self.client.factory.create('RequestedShipment') 54 self.RequestedShipment.ShipTimestamp = datetime.datetime.now() 55 56 # Defaults for TotalWeight wsdl object. 57 total_weight = self.client.factory.create('Weight') 58 # Start at nothing. 59 total_weight.Value = 0.0 60 # Default to pounds. 61 total_weight.Units = 'LB' 62 # This is the total weight of the entire shipment. Shipments may 63 # contain more than one package. 64 self.RequestedShipment.TotalWeight = total_weight 65 66 # This is the top level data structure for Shipper information. 67 shipper = self.client.factory.create('Party') 68 shipper.Address = self.client.factory.create('Address') 69 shipper.Contact = self.client.factory.create('Contact') 70 71 # Link the ShipperParty to our master data structure. 72 self.RequestedShipment.Shipper = shipper 73 74 # This is the top level data structure for Recipient information. 75 recipient_party = self.client.factory.create('Party') 76 recipient_party.Contact = self.client.factory.create('Contact') 77 recipient_party.Address = self.client.factory.create('Address') 78 # Link the RecipientParty object to our master data structure. 79 self.RequestedShipment.Recipient = recipient_party 80 81 # Make sender responsible for payment by default. 82 self.RequestedShipment.ShippingChargesPayment = self.create_wsdl_object_of_type('Payment') 83 self.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER' 84 85 # Start with no packages, user must add them. 86 self.RequestedShipment.PackageCount = 0 87 self.RequestedShipment.RequestedPackageLineItems = [] 88 89 # This is good to review if you'd like to see what the data structure 90 # looks like. 91 self.logger.debug(self.RequestedShipment)
92
94 """ 95 Fires off the Fedex request. 96 97 @warning: NEVER CALL THIS METHOD DIRECTLY. CALL send_request(), 98 WHICH RESIDES ON FedexBaseService AND IS INHERITED. 99 """ 100 101 # Fire off the query. 102 return self.client.service.getRates( 103 WebAuthenticationDetail=self.WebAuthenticationDetail, 104 ClientDetail=self.ClientDetail, 105 TransactionDetail=self.TransactionDetail, 106 Version=self.VersionId, 107 RequestedShipment=self.RequestedShipment, 108 ReturnTransitAndCommit=self.ReturnTransitAndCommit)
109
110 - def add_package(self, package_item):
111 """ 112 Adds a package to the ship request. 113 114 @type package_item: WSDL object, type of RequestedPackageLineItem 115 WSDL object. 116 @keyword package_item: A RequestedPackageLineItem, created by 117 calling create_wsdl_object_of_type('RequestedPackageLineItem') on 118 this ShipmentRequest object. See examples/create_shipment.py for 119 more details. 120 """ 121 122 self.RequestedShipment.RequestedPackageLineItems.append(package_item) 123 package_weight = package_item.Weight.Value 124 self.RequestedShipment.TotalWeight.Value += package_weight 125 self.RequestedShipment.PackageCount += 1
126