fedex.services.rate_service

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