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