@@ -83,7 +83,7 @@ def __init__(self, user_id, secret, storage_type="FILE", token_file_path="", mem
8383 self .__token = f .readline ()
8484
8585 else :
86- logger .error ("Can't find file '{}' to read security token." .format (filepath ))
86+ logger .warning ("Can't find file '{}' to read security token." .format (filepath ))
8787 logger .debug ("Got: '{}'" .format (self .__token , ))
8888 if not self .__token and not self .__get_token ():
8989 raise Exception ("Could not connect to API. Please, check your ID and SECRET" )
@@ -173,38 +173,40 @@ def __send_request(self, path, method="GET", params=None, use_token=True, use_js
173173 def __handle_result (self , data ):
174174 """ Process request results
175175
176- @param data:
176+ @param data: a Response object from the Python Requests package
177177 @return: dictionary with response message and/or http code
178178 """
179- if 'status_code' not in data :
180- if data .status_code == 200 :
181- logger .debug ("Hanle result: {}" .format (data .json (), ))
182- return data .json ()
183- elif data .status_code == 404 :
184- response = {
185- 'is_error' : True ,
186- 'http_code' : data .status_code ,
187- 'message' : "Sorry, the page you are looking for {} could not be found." .format (data .url , )
188- }
189- elif data .status_code == 500 :
190- response = {
191- 'is_error' : True ,
192- 'http_code' : data .status_code ,
193- 'message' :
"Whoops, looks like something went wrong on the server. Please contact with out support [email protected] ." 194- }
195- else :
196- response = {
197- 'is_error' : True ,
198- 'http_code' : data .status_code
199- }
200- response .update (data .json ())
179+ try :
180+ result = data .json ()
181+ except :
182+ result = {
183+ 'is_error' : True ,
184+ 'http_code' : data .status_code ,
185+ 'message' : "Response is empty, invalid or not JSON."
186+ }
187+
188+ if data .ok :
189+ errors = {
190+ 'is_error' : False ,
191+ 'http_code' : data .status_code
192+ }
193+ logger .debug ("Handle result: {}" .format (result , ))
201194 else :
202- response = {
195+ errors = {
203196 'is_error' : True ,
204- 'http_code' : data
197+ 'http_code' : data . status_code
205198 }
206- logger .debug ("Hanle result: {}" .format (response , ))
207- return {'data' : response }
199+ if data .status_code == 404 :
200+ errors ['message' ] = "Sorry, the page you are looking for {} could not be found." .format (data .url , )
201+ elif data .status_code == 500 :
202+ errors [
'message' ]
= "Whoops, looks like something went wrong on the server. Please contact with out support [email protected] ." 203+
204+ logger .debug ("Handle result: {}" .format (errors , ))
205+
206+ # return object that maintains backward-compatibility
207+ result .update (errors )
208+ result .update ({'data' : result .copy ()})
209+ return result
208210
209211 def __handle_error (self , custom_message = None ):
210212 """ Process request errors
@@ -215,7 +217,7 @@ def __handle_error(self, custom_message=None):
215217 message = {'is_error' : True }
216218 if custom_message is not None :
217219 message ['message' ] = custom_message
218- logger .error ("Hanle error: {}" .format (message , ))
220+ logger .error ("Handle error: {}" .format (message , ))
219221 return message
220222
221223 # ------------------------------------------------------------------ #
@@ -681,8 +683,8 @@ def smtp_send_mail(self, email):
681683 @return: dictionary with response message
682684 """
683685 logger .info ("Function call: smtp_send_mail" )
684- if ( not email .get ('html ' ) or not email .get ('text' )) and not email .get ('template ' ):
685- return self .__handle_error ('Seems we have empty body ' )
686+ if not email .get ('template ' ) and not email .get ('html' ) and not email .get ('text ' ):
687+ return self .__handle_error ('Missing email body - specify a template, html or text content ' )
686688 elif not email .get ('subject' ):
687689 return self .__handle_error ('Seems we have empty subject' )
688690 elif not email .get ('from' ) or not email .get ('to' ):
0 commit comments