fedex.services.ship_service

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

# Source Code for Module fedex.services.ship_service

      1  """
      2  Ship Service Module
      3  ===================
      4  This package contains the shipping methods defined by Fedex's
      5  ShipService WSDL file. Each is encapsulated in a class for easy access.
      6  For more details on each, refer to the respective class's documentation.
      7  """
      8  from datetime import datetime
      9  from .. base_service import FedexBaseService
     10
     11 -class FedexProcessShipmentRequest(FedexBaseService):
     12      """
     13      This class allows you to process (create) a new FedEx shipment. You will
     14      need to populate the data structures in self.RequestedShipment, then
     15      send the request. Label printing is supported and very configurable,
     16      returning an ASCII representation with the response as well.
     17      """
     18 -    def __init__(self, config_obj, *args, **kwargs):
     19          """
     20          The optional keyword args detailed on L{FedexBaseService}
     21          apply here as well.
     22
     23          @type config_obj: L{FedexConfig}
     24          @param config_obj: A valid FedexConfig object.
     25          """
     26          self._config_obj = config_obj
     27
     28          # Holds version info for the VersionId SOAP object.
     29          self._version_info = {'service_id': 'ship', 'major': '7',
     30                               'intermediate': '0', 'minor': '0'}
     31
     32          self.RequestedShipment = None
     33          """@ivar: Holds the RequestedShipment WSDL object."""
     34          # Call the parent FedexBaseService class for basic setup work.
     35          super(FedexProcessShipmentRequest, self).__init__(self._config_obj,
     36                                                           'ShipService_v7.wsdl',
     37                                                           *args, **kwargs)
     38
     39 -    def _prepare_wsdl_objects(self):
     40          """
     41          This is the data that will be used to create your shipment. Create
     42          the data structure and get it ready for the WSDL request.
     43          """
     44          # This is the primary data structure for processShipment requests.
     45          self.RequestedShipment = self.client.factory.create('RequestedShipment')
     46          self.RequestedShipment.ShipTimestamp = datetime.now()
     47
     48          TotalWeight = self.client.factory.create('Weight')
     49          # Start at nothing.
     50          TotalWeight.Value = 0.0
     51          # Default to pounds.
     52          TotalWeight.Units = 'LB'
     53          # This is the total weight of the entire shipment. Shipments may
     54          # contain more than one package.
     55          self.RequestedShipment.TotalWeight = TotalWeight
     56
     57          # This is the top level data structure for Shipper information.
     58          ShipperParty = self.client.factory.create('Party')
     59          ShipperParty.Address = self.client.factory.create('Address')
     60          ShipperParty.Contact = self.client.factory.create('Contact')
     61
     62          # Link the ShipperParty to our master data structure.
     63          self.RequestedShipment.Shipper = ShipperParty
     64
     65          # This is the top level data structure for Recipient information.
     66          RecipientParty = self.client.factory.create('Party')
     67          RecipientParty.Contact = self.client.factory.create('Contact')
     68          RecipientParty.Address = self.client.factory.create('Address')
     69
     70          # Link the RecipientParty object to our master data structure.
     71          self.RequestedShipment.Recipient = RecipientParty
     72
     73          Payor = self.client.factory.create('Payor')
     74          # Grab the account number from the FedexConfig object by default.
     75          Payor.AccountNumber = self._config_obj.account_number
     76          # Assume US.
     77          Payor.CountryCode = 'US'
     78
     79          ShippingChargesPayment = self.client.factory.create('Payment')
     80          ShippingChargesPayment.Payor = Payor
     81
     82          self.RequestedShipment.ShippingChargesPayment = ShippingChargesPayment
     83          self.RequestedShipment.LabelSpecification = self.client.factory.create('LabelSpecification')
     84          # ACCOUNT or LIST
     85          self.RequestedShipment.RateRequestTypes = ['ACCOUNT']
     86
     87          # Start with no packages, user must add them.
     88          self.RequestedShipment.PackageCount = 0
     89          self.RequestedShipment.RequestedPackageLineItems = []
     90
     91          # This is good to review if you'd like to see what the data structure
     92          # looks like.
     93          self.logger.debug(self.RequestedShipment)
     94
     95 -    def send_validation_request(self):
     96          """
     97          This is very similar to just sending the shipment via the typical
     98          send_request() function, but this doesn't create a shipment. It is
     99          used to make sure "good" values are given by the user or the
    100          application using the library.
    101          """
    102          self.send_request(send_function=self._assemble_and_send_validation_request)
    103
    104 -    def _assemble_and_send_validation_request(self):
    105          """
    106          Fires off the Fedex shipment validation request.
    107
    108          @warning: NEVER CALL THIS METHOD DIRECTLY. CALL
    109              send_validation_request(), WHICH RESIDES ON FedexBaseService
    110              AND IS INHERITED.
    111          """
    112          # Fire off the query.
    113          response = self.client.service.validateShipment(WebAuthenticationDetail=self.WebAuthenticationDetail,
    114                                          ClientDetail=self.ClientDetail,
    115                                          TransactionDetail=self.TransactionDetail,
    116                                          Version=self.VersionId,
    117                                          RequestedShipment=self.RequestedShipment)
    118          return response
    119
    120 -    def _assemble_and_send_request(self):
    121          """
    122          Fires off the Fedex request.
    123
    124          @warning: NEVER CALL THIS METHOD DIRECTLY. CALL send_request(),
    125              WHICH RESIDES ON FedexBaseService AND IS INHERITED.
    126          """
    127          # Fire off the query.
    128          response = self.client.service.processShipment(WebAuthenticationDetail=self.WebAuthenticationDetail,
    129                                          ClientDetail=self.ClientDetail,
    130                                          TransactionDetail=self.TransactionDetail,
    131                                          Version=self.VersionId,
    132                                          RequestedShipment=self.RequestedShipment)
    133          return response
    134
    135 -    def add_package(self, package_item):
    136          """
    137          Adds a package to the ship request.
    138
    139          @type package_item: WSDL object, type of RequestedPackageLineItem
    140              WSDL object.
    141          @keyword package_item: A RequestedPackageLineItem, created by
    142              calling create_wsdl_object_of_type('RequestedPackageLineItem') on
    143              this ShipmentRequest object. See examples/create_shipment.py for
    144              more details.
    145          """
    146          self.RequestedShipment.RequestedPackageLineItems.append(package_item)
    147          package_weight = package_item.Weight.Value
    148          self.RequestedShipment.TotalWeight.Value += package_weight
    149          self.RequestedShipment.PackageCount += 1
    150
    151 -class FedexDeleteShipmentRequest(FedexBaseService):
    152      """
    153      This class allows you to delete a shipment, given a tracking number.
    154      """
    155 -    def __init__(self, config_obj, *args, **kwargs):
    156          """
    157          Deletes a shipment via a tracking number.
    158          """
    159          self._config_obj = config_obj
    160
    161          # Holds version info for the VersionId SOAP object.
    162          self._version_info = {'service_id': 'ship', 'major': '7',
    163                               'intermediate': '0', 'minor': '0'}
    164          self.DeletionControlType = None
    165          """@ivar: Holds the DeletrionControlType WSDL object."""
    166          self.TrackingId = None
    167          """@ivar: Holds the TrackingId WSDL object."""
    168          # Call the parent FedexBaseService class for basic setup work.
    169          super(FedexDeleteShipmentRequest, self).__init__(self._config_obj,
    170                                                  'ShipService_v7.wsdl',
    171                                                  *args, **kwargs)
    172
    173 -    def _prepare_wsdl_objects(self):
    174          """
    175          Preps the WSDL data structures for the user.
    176          """
    177          self.DeletionControlType = self.client.factory.create('DeletionControlType')
    178          self.TrackingId = self.client.factory.create('TrackingId')
    179          self.TrackingId.TrackingIdType = self.client.factory.create('TrackingIdType')
    180
    181 -    def _assemble_and_send_request(self):
    182          """
    183          Fires off the Fedex request.
    184
    185          @warning: NEVER CALL THIS METHOD DIRECTLY. CALL send_request(), WHICH RESIDES
    186              ON FedexBaseService AND IS INHERITED.
    187          """
    188          client = self.client
    189          # Fire off the query.
    190          response = client.service.deleteShipment(WebAuthenticationDetail=self.WebAuthenticationDetail,
    191                                          ClientDetail=self.ClientDetail,
    192                                          TransactionDetail=self.TransactionDetail,
    193                                          Version=self.VersionId,
    194                                          ShipTimestamp = datetime.now(),
    195                                          TrackingId=self.TrackingId,
    196                                          DeletionControl=self.DeletionControlType)
    197
    198          return response
    199