@@ -83,7 +83,7 @@ def __init__(self, user_id, secret, storage_type="FILE", token_file_path="", mem
83
83
self .__token = f .readline ()
84
84
85
85
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 ))
87
87
logger .debug ("Got: '{}'" .format (self .__token , ))
88
88
if not self .__token and not self .__get_token ():
89
89
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
173
173
def __handle_result (self , data ):
174
174
""" Process request results
175
175
176
- @param data:
176
+ @param data: a Response object from the Python Requests package
177
177
@return: dictionary with response message and/or http code
178
178
"""
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 , ))
201
194
else :
202
- response = {
195
+ errors = {
203
196
'is_error' : True ,
204
- 'http_code' : data
197
+ 'http_code' : data . status_code
205
198
}
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
208
210
209
211
def __handle_error (self , custom_message = None ):
210
212
""" Process request errors
@@ -215,7 +217,7 @@ def __handle_error(self, custom_message=None):
215
217
message = {'is_error' : True }
216
218
if custom_message is not None :
217
219
message ['message' ] = custom_message
218
- logger .error ("Hanle error: {}" .format (message , ))
220
+ logger .error ("Handle error: {}" .format (message , ))
219
221
return message
220
222
221
223
# ------------------------------------------------------------------ #
@@ -681,8 +683,8 @@ def smtp_send_mail(self, email):
681
683
@return: dictionary with response message
682
684
"""
683
685
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 ' )
686
688
elif not email .get ('subject' ):
687
689
return self .__handle_error ('Seems we have empty subject' )
688
690
elif not email .get ('from' ) or not email .get ('to' ):
0 commit comments