Skip to content

Commit 5d2f64c

Browse files
drjwelchJason Welch
andauthored
Resolve issues and improve result (#32)
* Update requirements for python3 * Fix token handling, logging spelling * Improve __handle_result --------- Co-authored-by: Jason Welch <[email protected]>
1 parent 721d281 commit 5d2f64c

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ A simple SendPulse REST client library and example for Python.
77
## Install using pipy
88

99
```sh
10-
# version 0.0.3
1110
pip install pysendpulse
1211
```
1312

pysendpulse/pysendpulse.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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'):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__version__
77
)
88

9-
install_requires = ['python3-memcached', ]
9+
install_requires = ['python3-memcached', 'requests', 'deprecated']
1010

1111
if version_info.major == 2:
1212
install_requires = ['python-memcached', 'requests', 'simplejson']

0 commit comments

Comments
 (0)