Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #68 from matiboy/master
Allow API key to be passed in constructor
  • Loading branch information
Yamil Asusta committed Oct 20, 2017
2 parents 58e38b7 + 25dc34a commit 6186221
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.rst
Expand Up @@ -72,10 +72,34 @@ Example
mail.send()
To create an instance of a SendGridBackend with an API key other than that provided in settings, pass `api_key` to the constructor

.. code::python
from sgbackend import SendGridBackend
from django.core.mail import send_mail
connection = SendGridBackend(api_key='your key')
send_mail(<subject etc>, connection=connection)
License
-------
MIT


Enjoy :)


Development
-----------

Install dependencies::
`pip install -r requirements-dev.txt`

Run the tests with coverage::
`pytest --cov=sgbackend`

If you see the error "No module named sgbackend", run::
`pip install -e .`
5 changes: 4 additions & 1 deletion requirements-dev.txt
@@ -1,3 +1,6 @@
coverage
pytest
pytest-cov
pytest-cov
pytest-django
Django
sendgrid>=3.6.5,<4
5 changes: 4 additions & 1 deletion sgbackend/mail.py
Expand Up @@ -38,7 +38,10 @@ class SendGridBackend(BaseEmailBackend):
def __init__(self, fail_silently=False, **kwargs):
super(SendGridBackend, self).__init__(
fail_silently=fail_silently, **kwargs)
self.api_key = getattr(settings, "SENDGRID_API_KEY", None)
if 'api_key' in kwargs:
self.api_key = kwargs['api_key']
else:
self.api_key = getattr(settings, "SENDGRID_API_KEY", None)

if not self.api_key:
raise ImproperlyConfigured('''
Expand Down
21 changes: 21 additions & 0 deletions tests/test_api_key.py
@@ -0,0 +1,21 @@
from sgbackend import SendGridBackend


def test_read_from_settings_by_default(settings):
"""API key should be read from settings if not provided in init."""
settings.SENDGRID_API_KEY = 'somerandom key'
sg = SendGridBackend()

assert sg.api_key == settings.SENDGRID_API_KEY


def test_read_from_init_if_provided(settings):
"""Should pick up API key from init, if provided."""
settings.SENDGRID_API_KEY = 'somerandom key'
actual_key = 'another'

sg = SendGridBackend(api_key=actual_key)

assert sg.api_key == actual_key

""" Note: no API key configuration is tested in test_mail."""

0 comments on commit 6186221

Please sign in to comment.