Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Contributors
- `@puttu <https://github.com/puttu>`_
- Janusz Skonieczny `@wooyek <https://github.com/wooyek>`_
- Richard Dawe `@richdawe77 <https://github.com/rdawemsys>`_
- John Keyes `@jkeyes <https://github.com/jkeyes>`_
- ADD YOURSELF HERE (and link to your github page)
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/django/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 5 additions & 2 deletions sparkpost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@

__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:
api_key = self.get_api_key()
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,
Expand Down
5 changes: 3 additions & 2 deletions sparkpost/django/email_backend.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
"""
Expand Down
12 changes: 12 additions & 0 deletions test/django/test_email_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down