diff --git a/AUTHORS.rst b/AUTHORS.rst index 8929276..c633ec0 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -24,4 +24,5 @@ Contributors - `@puttu `_ - Janusz Skonieczny `@wooyek `_ - Richard Dawe `@richdawe77 `_ +- John Keyes `@jkeyes `_ - ADD YOURSELF HERE (and link to your github page) diff --git a/README.rst b/README.rst index 49864d9..33c7633 100644 --- a/README.rst +++ b/README.rst @@ -107,10 +107,13 @@ The SparkPost python library comes with an email backend for Django. Put the fol .. code-block:: python SPARKPOST_API_KEY = 'API_KEY' + SPARKPOST_BASE_URI = 'api.sparkpost.com' EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend' 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. +If you are using an EU account, set *SPARKPOST_BASE_URI* to `api.eu.sparkpost.com`. The default value is `api.sparkpost.com`. + .. _full documentation: https://python-sparkpost.readthedocs.io/en/latest/django/backend.html Using with Google Cloud diff --git a/docs/django/backend.rst b/docs/django/backend.rst index 1f7d511..39bb37d 100644 --- a/docs/django/backend.rst +++ b/docs/django/backend.rst @@ -11,10 +11,13 @@ To configure Django to use SparkPost, put the following configuration in `settin .. code-block:: python SPARKPOST_API_KEY = 'API_KEY' + SPARKPOST_BASE_URI = 'api.sparkpost.com' EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend' Replace *API_KEY* with an actual API key. +If you are using an EU account, set *SPARKPOST_BASE_URI* to `api.eu.sparkpost.com`. The default value is `api.sparkpost.com`. + You can also use `SPARKPOST_OPTIONS` to set options that will apply to every transmission. For example: diff --git a/sparkpost/__init__.py b/sparkpost/__init__.py index 233c7af..f1fcc0a 100644 --- a/sparkpost/__init__.py +++ b/sparkpost/__init__.py @@ -11,11 +11,14 @@ __version__ = '1.3.6' +EU_API = 'api.eu.sparkpost.com' +US_API = 'api.sparkpost.com' + class SparkPost(object): TRANSPORT_CLASS = RequestsTransport - def __init__(self, api_key=None, base_uri='https://api.sparkpost.com', + def __init__(self, api_key=None, base_uri=US_API, version='1'): "Set up the SparkPost API client" if not api_key: @@ -23,7 +26,7 @@ def __init__(self, api_key=None, base_uri='https://api.sparkpost.com', if not api_key: raise SparkPostException("No API key. Improve message.") - self.base_uri = base_uri + '/api/v' + version + self.base_uri = 'https://' + base_uri + '/api/v' + version self.api_key = api_key self.metrics = Metrics(self.base_uri, self.api_key, diff --git a/sparkpost/django/email_backend.py b/sparkpost/django/email_backend.py index 1e44a10..6b8ec84 100644 --- a/sparkpost/django/email_backend.py +++ b/sparkpost/django/email_backend.py @@ -1,7 +1,7 @@ from django.conf import settings from django.core.mail.backends.base import BaseEmailBackend -from sparkpost import SparkPost +from sparkpost import SparkPost, US_API from .message import SparkPostMessage @@ -16,8 +16,9 @@ def __init__(self, fail_silently=False, **kwargs): .__init__(fail_silently=fail_silently, **kwargs) sp_api_key = getattr(settings, 'SPARKPOST_API_KEY', None) + sp_base_uri = getattr(settings, 'SPARKPOST_BASE_URI', US_API) - self.client = SparkPost(sp_api_key) + self.client = SparkPost(sp_api_key, sp_base_uri) def send_messages(self, email_messages): """ diff --git a/test/django/test_email_backend.py b/test/django/test_email_backend.py index e13d768..516c0b1 100644 --- a/test/django/test_email_backend.py +++ b/test/django/test_email_backend.py @@ -7,6 +7,7 @@ from django.core.mail import EmailMultiAlternatives from django.utils.functional import empty +from sparkpost import EU_API, US_API from sparkpost.django.email_backend import SparkPostEmailBackend from sparkpost.django.exceptions import UnsupportedContent from sparkpost.transmissions import Transmissions @@ -53,6 +54,17 @@ def test_password_retrieval(): assert backend.client.api_key == API_KEY +def test_default_base_uri_retrieval(): + backend = SparkPostEmailBackend() + assert backend.client.base_uri.startswith('https://' + US_API) + + +def test_eu_base_uri_retrieval(): + reconfigure_settings(SPARKPOST_BASE_URI=EU_API) + backend = SparkPostEmailBackend() + assert backend.client.base_uri.startswith('https://' + EU_API) + + def test_fail_silently(): # should not raise with mock.patch.object(Transmissions, 'send') as mock_send: