Skip to content

Commit f8b221d

Browse files
jkeyesjgzamora
authored andcommitted
Allowing US or EU API to be used in the Django backend. (#188)
1 parent cf5fa54 commit f8b221d

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Contributors
2424
- `@puttu <https://github.com/puttu>`_
2525
- Janusz Skonieczny `@wooyek <https://github.com/wooyek>`_
2626
- Richard Dawe `@richdawe77 <https://github.com/rdawemsys>`_
27+
- John Keyes `@jkeyes <https://github.com/jkeyes>`_
2728
- ADD YOURSELF HERE (and link to your github page)

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ The SparkPost python library comes with an email backend for Django. Put the fol
107107
.. code-block:: python
108108
109109
SPARKPOST_API_KEY = 'API_KEY'
110+
SPARKPOST_BASE_URI = 'api.sparkpost.com'
110111
EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'
111112
112113
Replace *API_KEY* with an actual API key that you've generated in `Get a Key`_ section. Check out the `full documentation`_ on the Django email backend.
113114

115+
If you are using an EU account, set *SPARKPOST_BASE_URI* to `api.eu.sparkpost.com`. The default value is `api.sparkpost.com`.
116+
114117
.. _full documentation: https://python-sparkpost.readthedocs.io/en/latest/django/backend.html
115118

116119
Using with Google Cloud

docs/django/backend.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ To configure Django to use SparkPost, put the following configuration in `settin
1111
.. code-block:: python
1212
1313
SPARKPOST_API_KEY = 'API_KEY'
14+
SPARKPOST_BASE_URI = 'api.sparkpost.com'
1415
EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'
1516
1617
Replace *API_KEY* with an actual API key.
1718

19+
If you are using an EU account, set *SPARKPOST_BASE_URI* to `api.eu.sparkpost.com`. The default value is `api.sparkpost.com`.
20+
1821
You can also use `SPARKPOST_OPTIONS` to set options that will apply to every transmission.
1922
For example:
2023

sparkpost/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111

1212
__version__ = '1.3.6'
1313

14+
EU_API = 'api.eu.sparkpost.com'
15+
US_API = 'api.sparkpost.com'
16+
1417

1518
class SparkPost(object):
1619
TRANSPORT_CLASS = RequestsTransport
1720

18-
def __init__(self, api_key=None, base_uri='https://api.sparkpost.com',
21+
def __init__(self, api_key=None, base_uri=US_API,
1922
version='1'):
2023
"Set up the SparkPost API client"
2124
if not api_key:
2225
api_key = self.get_api_key()
2326
if not api_key:
2427
raise SparkPostException("No API key. Improve message.")
2528

26-
self.base_uri = base_uri + '/api/v' + version
29+
self.base_uri = 'https://' + base_uri + '/api/v' + version
2730
self.api_key = api_key
2831

2932
self.metrics = Metrics(self.base_uri, self.api_key,

sparkpost/django/email_backend.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.conf import settings
22
from django.core.mail.backends.base import BaseEmailBackend
33

4-
from sparkpost import SparkPost
4+
from sparkpost import SparkPost, US_API
55

66
from .message import SparkPostMessage
77

@@ -16,8 +16,9 @@ def __init__(self, fail_silently=False, **kwargs):
1616
.__init__(fail_silently=fail_silently, **kwargs)
1717

1818
sp_api_key = getattr(settings, 'SPARKPOST_API_KEY', None)
19+
sp_base_uri = getattr(settings, 'SPARKPOST_BASE_URI', US_API)
1920

20-
self.client = SparkPost(sp_api_key)
21+
self.client = SparkPost(sp_api_key, sp_base_uri)
2122

2223
def send_messages(self, email_messages):
2324
"""

test/django/test_email_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.core.mail import EmailMultiAlternatives
88
from django.utils.functional import empty
99

10+
from sparkpost import EU_API, US_API
1011
from sparkpost.django.email_backend import SparkPostEmailBackend
1112
from sparkpost.django.exceptions import UnsupportedContent
1213
from sparkpost.transmissions import Transmissions
@@ -53,6 +54,17 @@ def test_password_retrieval():
5354
assert backend.client.api_key == API_KEY
5455

5556

57+
def test_default_base_uri_retrieval():
58+
backend = SparkPostEmailBackend()
59+
assert backend.client.base_uri.startswith('https://' + US_API)
60+
61+
62+
def test_eu_base_uri_retrieval():
63+
reconfigure_settings(SPARKPOST_BASE_URI=EU_API)
64+
backend = SparkPostEmailBackend()
65+
assert backend.client.base_uri.startswith('https://' + EU_API)
66+
67+
5668
def test_fail_silently():
5769
# should not raise
5870
with mock.patch.object(Transmissions, 'send') as mock_send:

0 commit comments

Comments
 (0)