Skip to content

Commit 8488c83

Browse files
authored
Merge pull request #78 from sailthru/INT-1681-fix-python3-bytes-compatibility
Fix Python 3 bytes/str incompatibility in simplejson and postback methods
2 parents 178e7fe + cc949ff commit 8488c83

7 files changed

Lines changed: 20 additions & 17 deletions

File tree

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@
4343
2.3.5
4444
===
4545
- Add request timeout argument
46+
47+
2.3.6
48+
===
49+
- Fix Python 3 bytes/str incompatibility: use response.text instead of response.content so simplejson (required dependency) receives a str
50+
- Fix receive_verify_post to use the dict returned by get_body() directly instead of calling json.loads on it again
51+
- Fix get_signature_string returning bytes instead of str; encoding moved to get_signature_hash where hashlib requires it
52+
- Sync version string across __init__.py, sailthru_http.py User-Agent, and setup.py

sailthru/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
__doc__ = 'Python binding for Sailthru API based on Requests'
77
__copyright__ = 'Copyright 2012-2015, Sailthru Inc.'
88
__license__ = 'MIT'
9-
__version__ = '2.3.1'
9+
__version__ = '2.3.6'

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 isinstance(send_json, dict):
594+
return False
595+
if send_json.get('email') != post_params['email']:
600596
return False
601597

602598
return True

sailthru/sailthru_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def sailthru_http_request(url, data, method, file_data=None, headers=None, reque
3434
data = flatten_nested_hash(data)
3535
method = method.upper()
3636
params, data = (None, data) if method == 'POST' else (data, None)
37-
sailthru_headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.5', platform.python_version())}
37+
sailthru_headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.6', platform.python_version())}
3838
if headers and isinstance(headers, dict):
3939
for key, value in sailthru_headers.items():
4040
headers[key] = value

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: 2 additions & 2 deletions
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

@@ -126,7 +126,7 @@ def test_hardbounce_blast_invalid_json(self):
126126

127127
def test_hardbounce_valid_json(self):
128128
mock_http_request = MagicMock()
129-
mock_http_request.return_value.get_body.return_value = '{"email": "menglander@sailthru.com"}'
129+
mock_http_request.return_value.get_body.return_value = {"email": "menglander@sailthru.com"}
130130

131131
self.client._http_request = mock_http_request
132132

0 commit comments

Comments
 (0)