Skip to content

Commit

Permalink
Adapt SendGrid integration to use the updated libraries. (#2715)
Browse files Browse the repository at this point in the history
* Adapt SendGrid integration to use the updated libraries.

* DRY Mail() generation, no retrieval of sendgrid response

* pep 8 fixes

* fix sendgrid notifications test

* pep 8 fix

* fix notification test to not use raise_errors = True since that is not a kwarg for SendGridAPIClient anymore
  • Loading branch information
guidopetri authored and dlstadther committed May 28, 2019
1 parent e222841 commit 2de1b68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
27 changes: 13 additions & 14 deletions luigi/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,9 @@ class smtp(luigi.Config):


class sendgrid(luigi.Config):
username = luigi.parameter.Parameter(
config_path=dict(section='email', name='SENDGRID_USERNAME'),
description='Username for sendgrid login')
password = luigi.parameter.Parameter(
config_path=dict(section='email', name='SENDGRID_PASSWORD'),
description='Username for sendgrid login')
apikey = luigi.parameter.Parameter(
config_path=dict(section='email', name='SENGRID_API_KEY'),
description='API key for SendGrid login')


def generate_email(sender, subject, message, recipients, image_png):
Expand Down Expand Up @@ -234,16 +231,18 @@ def send_email_ses(sender, subject, message, recipients, image_png):

def send_email_sendgrid(sender, subject, message, recipients, image_png):
import sendgrid as sendgrid_lib
client = sendgrid_lib.SendGridClient(
sendgrid().username, sendgrid().password, raise_errors=True)
to_send = sendgrid_lib.Mail()
to_send.add_to(recipients)
to_send.set_from(sender)
to_send.set_subject(subject)
client = sendgrid_lib.SendGridAPIClient(sendgrid().apikey)

to_send = sendgrid_lib.Mail(
from_email=sender,
to_emails=recipients,
subject=subject)

if email().format == 'html':
to_send.set_html(message)
to_send.add_content(message, 'text/html')
else:
to_send.set_text(message)
to_send.add_content(message, 'text/plain')

if image_png:
to_send.add_attachment(image_png)

Expand Down
11 changes: 5 additions & 6 deletions test/notifications_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,18 @@ def setUp(self):
def tearDown(self):
del sys.modules['sendgrid']

@with_config({"sendgrid": {"username": "Nikola",
"password": "jahuS"}})
@with_config({"sendgrid": {"apikey": "456abcdef123"}})
def test_sends_sendgrid_email(self):
"""
Call notifications.send_email_sendgrid with fixture parameters
and check that SendGridClient is properly called.
and check that SendGridAPIClient is properly called.
"""

with mock.patch('sendgrid.SendGridClient') as SendgridClient:
with mock.patch('sendgrid.SendGridAPIClient') as SendGridAPIClient:
notifications.send_email_sendgrid(*self.notification_args)

SendgridClient.assert_called_once_with("Nikola", "jahuS", raise_errors=True)
self.assertTrue(SendgridClient.return_value.send.called)
SendGridAPIClient.assert_called_once_with("456abcdef123")
self.assertTrue(SendGridAPIClient.return_value.send.called)


class TestSESEmail(unittest.TestCase, NotificationFixture):
Expand Down

0 comments on commit 2de1b68

Please sign in to comment.