8000 Adapt SendGrid integration to use the updated libraries. by guidopetri · Pull Request #2715 · spotify/luigi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adapt SendGrid integration to use the updated libraries. #2715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 28, 2019
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
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