Skip to content

Commit 77bac4d

Browse files
committed
refactor variable names, add warn logging
* function local variable names should not be camel cased, fixed to lower case with underscores. * added a warning logger when request returns with NOTE warning from fedex.
1 parent 8a01f54 commit 77bac4d

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

fedex/base_service.py

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, **kwargs):
2323
self.kwargs = kwargs
2424

2525
def marshalled(self, context):
26+
# Removes the WSDL objects that do not have a value before sending.
2627
context.envelope = context.envelope.prune()
2728

2829
def sending(self, context):
@@ -106,9 +107,14 @@ def __init__(self, config_obj, wsdl_name, *args, **kwargs):
106107

107108
self.logger = logging.getLogger('fedex')
108109
"""@ivar: Python logger instance with name 'fedex'."""
110+
109111
self.config_obj = config_obj
110112
"""@ivar: The FedexConfig object to pull auth info from."""
111113

114+
if not self._version_info:
115+
self._version_info = {}
116+
"""#ivar: Set in each service class. Holds version info for the VersionId SOAP object."""
117+
112118
# If the config object is set to use the test server, point
113119
# suds at the test server WSDL directory.
114120
if config_obj.use_test_server:
@@ -149,48 +155,48 @@ def __set_web_authentication_detail(self):
149155
"""
150156

151157
# Start of the authentication stuff.
152-
WebAuthenticationCredential = self.client.factory.create('WebAuthenticationCredential')
153-
WebAuthenticationCredential.Key = self.config_obj.key
154-
WebAuthenticationCredential.Password = self.config_obj.password
158+
web_authentication_credential = self.client.factory.create('WebAuthenticationCredential')
159+
web_authentication_credential.Key = self.config_obj.key
160+
web_authentication_credential.Password = self.config_obj.password
155161

156162
# Encapsulates the auth credentials.
157-
WebAuthenticationDetail = self.client.factory.create('WebAuthenticationDetail')
158-
WebAuthenticationDetail.UserCredential = WebAuthenticationCredential
163+
web_authentication_detail = self.client.factory.create('WebAuthenticationDetail')
164+
web_authentication_detail.UserCredential = web_authentication_credential
159165

160166
# Set Default ParentCredential
161-
if hasattr(WebAuthenticationDetail, 'ParentCredential'):
162-
WebAuthenticationDetail.ParentCredential = WebAuthenticationCredential
167+
if hasattr(web_authentication_detail, 'ParentCredential'):
168+
web_authentication_detail.ParentCredential = web_authentication_credential
163169

164-
self.WebAuthenticationDetail = WebAuthenticationDetail
170+
self.WebAuthenticationDetail = web_authentication_detail
165171

166172
def __set_client_detail(self, *args, **kwargs):
167173
"""
168174
Sets up the ClientDetail node, which is required for all shipping
169175
related requests.
170176
"""
171177

172-
ClientDetail = self.client.factory.create('ClientDetail')
173-
ClientDetail.AccountNumber = self.config_obj.account_number
174-
ClientDetail.MeterNumber = self.config_obj.meter_number
175-
ClientDetail.IntegratorId = self.config_obj.integrator_id
176-
if hasattr(ClientDetail, 'Region'):
177-
ClientDetail.Region = self.config_obj.express_region_code
178+
client_detail = self.client.factory.create('ClientDetail')
179+
client_detail.AccountNumber = self.config_obj.account_number
180+
client_detail.MeterNumber = self.config_obj.meter_number
181+
client_detail.IntegratorId = self.config_obj.integrator_id
182+
if hasattr(client_detail, 'Region'):
183+
client_detail.Region = self.config_obj.express_region_code
178184

179185
client_language_code = kwargs.get('client_language_code', None)
180186
client_locale_code = kwargs.get('client_locale_code', None)
181187

182-
if hasattr(ClientDetail, 'Localization') and (client_language_code or client_locale_code):
183-
Localization = self.client.factory.create('Localization')
188+
if hasattr(client_detail, 'Localization') and (client_language_code or client_locale_code):
189+
localization = self.client.factory.create('Localization')
184190

185191
if client_language_code:
186-
Localization.LanguageCode = client_language_code
192+
localization.LanguageCode = client_language_code
187193

188194
if client_locale_code:
189-
Localization.LocaleCode = client_locale_code
195+
localization.LocaleCode = client_locale_code
190196

191-
ClientDetail.Localization = Localization
197+
client_detail.Localization = localization
192198

193-
self.ClientDetail = ClientDetail
199+
self.ClientDetail = client_detail
194200

195201
def __set_transaction_detail(self, *args, **kwargs):
196202
"""
@@ -199,23 +205,23 @@ def __set_transaction_detail(self, *args, **kwargs):
199205

200206
customer_transaction_id = kwargs.get('customer_transaction_id', None)
201207
if customer_transaction_id:
202-
TransactionDetail = self.client.factory.create('TransactionDetail')
203-
TransactionDetail.CustomerTransactionId = customer_transaction_id
204-
self.logger.debug(TransactionDetail)
205-
self.TransactionDetail = TransactionDetail
208+
transaction_detail = self.client.factory.create('TransactionDetail')
209+
transaction_detail.CustomerTransactionId = customer_transaction_id
210+
self.logger.debug(transaction_detail)
211+
self.TransactionDetail = transaction_detail
206212

207213
def __set_version_id(self):
208214
"""
209215
Pulles the versioning info for the request from the child request.
210216
"""
211217

212-
VersionId = self.client.factory.create('VersionId')
213-
VersionId.ServiceId = self._version_info['service_id']
214-
VersionId.Major = self._version_info['major']
215-
VersionId.Intermediate = self._version_info['intermediate']
216-
VersionId.Minor = self._version_info['minor']
217-
self.logger.debug(VersionId)
218-
self.VersionId = VersionId
218+
version_id = self.client.factory.create('VersionId')
219+
version_id.ServiceId = self._version_info['service_id']
220+
version_id.Major = self._version_info['major']
221+
version_id.Intermediate = self._version_info['intermediate']
222+
version_id.Minor = self._version_info['minor']
223+
self.logger.debug(version_id)
224+
self.VersionId = version_id
219225

220226
def _prepare_wsdl_objects(self):
221227
"""
@@ -251,17 +257,34 @@ def _check_response_for_request_errors(self):
251257
raise FedexError(notification.Code,
252258
notification.Message)
253259

260+
def _check_response_for_request_warnings(self):
261+
"""
262+
Override this in a service module to check for errors that are
263+
specific to that module. For example, changing state/province based
264+
on postal code in a Rate Service request.
265+
"""
266+
267+
if self.response.HighestSeverity == "NOTE":
268+
for notification in self.response.Notifications:
269+
if notification.Severity == "NOTE":
270+
self.logger.warning(FedexFailure(notification.Code,
271+
notification.Message))
272+
254273
def create_wsdl_object_of_type(self, type_name):
255274
"""
256275
Creates and returns a WSDL object of the specified type.
276+
:param type_name: specifies the object's type name from WSDL.
257277
"""
258278

259279
return self.client.factory.create(type_name)
260280

261281
def _assemble_and_send_request(self):
262282
"""
263-
This method should be over-ridden on each sub-class. It assembles all required objects
283+
This method should be over-ridden on each sub-class.
284+
It assembles all required objects
264285
into the specific request object and calls send_request.
286+
Objects that are not set will be pruned before sending
287+
via GeneralSudsPlugin marshalled function.
265288
"""
266289

267290
pass
@@ -295,10 +318,15 @@ def send_request(self, send_function=None):
295318
# Check the response for general Fedex errors/failures that aren't
296319
# specific to any given WSDL/request.
297320
self.__check_response_for_fedex_error()
321+
298322
# Check the response for errors specific to the particular request.
299-
# This is handled by an overridden method on the child object.
323+
# This method can be overridden by a method on the child class object.
300324
self._check_response_for_request_errors()
301325

326+
# Check the response for errors specific to the particular request.
327+
# This method can be overridden by a method on the child class object.
328+
self._check_response_for_request_warnings()
329+
302330
# Debug output.
303331
self.logger.debug("== FEDEX QUERY RESULT ==")
304332
self.logger.debug(self.response)

0 commit comments

Comments
 (0)