Skip to content

Commit b8b8533

Browse files
authored
Merge pull request #160 from wooyek/feature/django-backend-headers-support
Support for EmailMessage.extra_headers and transactional option
2 parents c2a25aa + f65836e commit b8b8533

File tree

8 files changed

+49
-5
lines changed

8 files changed

+49
-5
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ Patches and suggestions
2121
- `@gnarvaja <https://github.com/gnarvaja>`_
2222
- `@pegler <https://github.com/pegler>`_
2323
- `@puttu <https://github.com/puttu>`_
24+
- Janusz Skonieczny `@wooyek <https://github.com/wooyek>`_
2425
- ADD YOURSELF HERE (and link to your github page)

sparkpost/django/message.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ def __init__(self, message):
9090
if hasattr(message, 'campaign'):
9191
formatted['campaign'] = message.campaign
9292

93+
if message.extra_headers:
94+
formatted['custom_headers'] = message.extra_headers
95+
if 'X-MSYS-API' in message.extra_headers:
96+
import json
97+
msys_api = json.loads(message.extra_headers['X-MSYS-API'])
98+
if msys_api and msys_api.get('options', {}).get('transactional', False): # noqa: E501
99+
formatted['transactional'] = True
100+
93101
super(SparkPostMessage, self).__init__(formatted)

sparkpost/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ class SparkPostException(Exception):
55
class SparkPostAPIException(SparkPostException):
66
"Handle 4xx and 5xx errors from the SparkPost API"
77
def __init__(self, response, *args, **kwargs):
8+
# noinspection PyBroadException
89
try:
910
errors = response.json()['errors']
1011
error_template = "{message} Code: {code} Description: {desc} \n"
1112
errors = [error_template.format(message=e.get('message', ''),
1213
code=e.get('code', 'none'),
1314
desc=e.get('description', 'none'))
1415
for e in errors]
15-
except:
16+
# TODO: select exception to catch here
17+
except: # noqa: E722
1618
errors = [response.text or ""]
1719
self.status = response.status_code
1820
self.response = response

sparkpost/tornado/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def request(self, method, uri, headers, **kwargs):
2020
raise gen.Return(True)
2121
if response.code == 200:
2222
result = None
23+
# noinspection PyBroadException
2324
try:
2425
result = json.loads(response.body.decode("utf-8"))
25-
except:
26+
# TODO: select exception to catch here
27+
except: # noqa: E722
2628
pass
2729
if result:
2830
if 'results' in result:

sparkpost/tornado/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
class SparkPostAPIException(RequestsSparkPostAPIException):
77
def __init__(self, response, *args, **kwargs):
88
errors = None
9+
# noinspection PyBroadException
910
try:
1011
data = json.loads(response.body.decode("utf-8"))
1112
if data:
1213
errors = data['errors']
1314
errors = [e['message'] + ': ' + e.get('description', '')
1415
for e in errors]
15-
except:
16+
# TODO: select exception to catch here
17+
except: # noqa: E722
1618
pass
1719
if not errors:
1820
errors = [response.body.decode("utf-8") or ""]

sparkpost/transmissions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ def send(self, **kwargs):
251251
"""
252252

253253
payload = self._translate_keys(**kwargs)
254-
results = self.request('POST', self.uri, data=json.dumps(payload))
254+
data = json.dumps(payload)
255+
results = self.request('POST', self.uri, data=data)
255256
return results
256257

257258
def _fetch_get(self, transmission_id):

test/django/test_message.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,30 @@ def test_reply_to():
228228

229229
assert message(reply_to=['[email protected]',
230230
'[email protected]']) == expected
231+
232+
233+
def test_extra_headers():
234+
email_message = EmailMessage(**base_options)
235+
email_message.extra_headers['FOO'] = 'bar'
236+
237+
actual = SparkPostMessage(email_message)
238+
expected = dict(
239+
custom_headers={'FOO': 'bar'},
240+
)
241+
expected.update(base_expected)
242+
assert actual == expected
243+
244+
245+
def test_transactional():
246+
email_message = EmailMessage(**base_options)
247+
import json
248+
msys_api = json.dumps({'options': {'transactional': True}})
249+
email_message.extra_headers['X-MSYS-API'] = msys_api
250+
251+
actual = SparkPostMessage(email_message)
252+
expected = dict(
253+
custom_headers={'X-MSYS-API': msys_api},
254+
transactional=True,
255+
)
256+
expected.update(base_expected)
257+
assert actual == expected

test/test_templates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
try:
22
from urllib.parse import urlparse
3-
except:
3+
# TODO: select exception to catch here
4+
except: # noqa: E722
45
from urlparse import urlparse
56

67
import pytest

0 commit comments

Comments
 (0)