Skip to content

Commit e97071c

Browse files
committed
Fix Python 3 bytes/str incompatibility in simplejson and postback methods
- sailthru_response.py: use response.text instead of response.content so simplejson (which rejects bytes) receives a str; also catch TypeError in addition to ValueError for robustness - sailthru_client.py: remove redundant json.loads() in receive_verify_post since get_body() already returns a parsed dict - sailthru_client.py: fix get_signature_string() returning bytes instead of str by moving .encode('utf-8') into get_signature_hash() where hashlib needs it - test: update receive_verify_post mock to return a dict matching real get_body() behavior - setup.py: bump version to 2.3.6 Closes INT-1681. Resolves the same issue identified in PR #63 (open since 2017).
1 parent 178e7fe commit e97071c

4 files changed

Lines changed: 10 additions & 14 deletions

File tree

sailthru/sailthru_client.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ def get_signature_string(params, secret):
3131
"""
3232
str_list = [str(item) for item in extract_params(params)]
3333
str_list.sort()
34-
return (secret + ''.join(str_list)).encode('utf-8')
34+
return secret + ''.join(str_list)
3535

3636
def get_signature_hash(params, secret):
3737
"""
3838
Returns an MD5 hash of the signature string for an API call.
3939
@param params: dictionary values to generate signature hash
4040
@param sercret: secret string
4141
"""
42-
return hashlib.md5(get_signature_string(params, secret)).hexdigest()
42+
return hashlib.md5(get_signature_string(params, secret).encode('utf-8')).hexdigest()
4343

4444

4545
class SailthruClient(object):
@@ -589,14 +589,10 @@ def receive_verify_post(self, post_params):
589589

590590
send_response = self.get_send(post_params['send_id'])
591591

592-
try:
593-
send_body = send_response.get_body()
594-
send_json = json.loads(send_body)
595-
if 'email' not in send_body:
596-
return False
597-
if send_json['email'] != post_params['email']:
598-
return False
599-
except ValueError:
592+
send_json = send_response.get_body()
593+
if not send_json or 'email' not in send_json:
594+
return False
595+
if send_json['email'] != post_params['email']:
600596
return False
601597

602598
return True

sailthru/sailthru_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def __init__(self, response):
1111
self.json_error = None
1212

1313
try:
14-
self.json = json.loads(response.content)
15-
except ValueError as e:
14+
self.json = json.loads(response.text)
15+
except (ValueError, TypeError) as e:
1616
self.json = None
1717
self.json_error = str(e)
1818

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='sailthru-client',
4-
version='2.3.5',
4+
version='2.3.6',
55
packages=find_packages(),
66
description='Python client for Sailthru API',
77
long_description=open('README.md').read(),

test/test_sailthru_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_check_for_valid_actions(self):
6464

6565
def test_receive_verify_post(self):
6666
mock_http_request = MagicMock()
67-
mock_http_request.return_value.get_body.return_value = '{"email":"menglander@sailthru.com"}'
67+
mock_http_request.return_value.get_body.return_value = {"email": "menglander@sailthru.com"}
6868

6969
self.client._http_request = mock_http_request
7070

0 commit comments

Comments
 (0)