diff --git a/appengine/flexible/sendgrid/main.py b/appengine/flexible/sendgrid/main.py index fcafce4cb37a..f7e31a205d22 100644 --- a/appengine/flexible/sendgrid/main.py +++ b/appengine/flexible/sendgrid/main.py @@ -18,6 +18,7 @@ from flask import Flask, render_template, request import sendgrid +from sendgrid.helpers import mail # [START config] SENDGRID_API_KEY = os.environ['SENDGRID_API_KEY'] @@ -40,19 +41,18 @@ def send_email(): return ('Please provide an email address in the "to" query string ' 'parameter.'), 400 - sg = sendgrid.SendGridClient(SENDGRID_API_KEY) + sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY) - message = sendgrid.Mail( - to=to, - subject='This is a test email', - html='

Example HTML body.

', - text='Example text body.', - from_email=SENDGRID_SENDER) + to_email = mail.Email(to) + from_email = mail.Email(SENDGRID_SENDER) + subject = 'This is a test email' + content = mail.Content('text/plain', 'Example message.') + message = mail.Mail(from_email, subject, to_email, content) - status, response = sg.send(message) + response = sg.client.mail.send.post(request_body=message.get()) - if status != 200: - return 'An error occurred: {}'.format(response), 500 + if response.status_code != 200: + return 'An error occurred: {}'.format(response.body), 500 return 'Email sent.' # [END example] diff --git a/appengine/flexible/sendgrid/main_test.py b/appengine/flexible/sendgrid/main_test.py index 3bad53851545..7577665f83f1 100644 --- a/appengine/flexible/sendgrid/main_test.py +++ b/appengine/flexible/sendgrid/main_test.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import main - import mock import pytest @@ -34,11 +32,18 @@ def test_get(app): assert r.status_code == 200 -@mock.patch.object( - main.sendgrid.SendGridClient, 'send', return_value=(200, "OK")) -def test_post(send_mock, app): - r = app.post('/send/email', data={ +@mock.patch('python_http_client.client.Client._make_request') +def test_post(make_request_mock, app): + response = mock.Mock() + response.getcode.return_value = 200 + response.read.return_value = 'OK' + response.info.return_value = {} + make_request_mock.return_value = response + + app.post('/send/email', data={ 'to': 'user@example.com' }) - assert r.status_code == 200 - assert send_mock.called + + assert make_request_mock.called + request = make_request_mock.call_args[0][1] + assert 'user@example.com' in request.data.decode('utf-8') diff --git a/appengine/flexible/sendgrid/requirements.txt b/appengine/flexible/sendgrid/requirements.txt index d0b399041c63..d89e43d4460e 100644 --- a/appengine/flexible/sendgrid/requirements.txt +++ b/appengine/flexible/sendgrid/requirements.txt @@ -1,3 +1,3 @@ Flask==0.11.1 -sendgrid==2.2.1 +sendgrid==3.0.0 gunicorn==19.6.0 diff --git a/appengine/standard/sendgrid/main.py b/appengine/standard/sendgrid/main.py index 942242f88896..1191b3e75ccf 100644 --- a/appengine/standard/sendgrid/main.py +++ b/appengine/standard/sendgrid/main.py @@ -16,29 +16,31 @@ # [START sendgrid-imp] import sendgrid +from sendgrid.helpers import mail # [END sendgrid-imp] import webapp2 # make a secure connection to SendGrid # [START sendgrid-config] SENDGRID_API_KEY = 'your-sendgrid-api-key' -SENDGRID_DOMAIN = 'your-sendgrid-domain' +SENDGRID_SENDER = 'your-sendgrid-sender' # [END sendgrid-config] -sg = sendgrid.SendGridClient(SENDGRID_API_KEY) - def send_simple_message(recipient): # [START sendgrid-send] - message = sendgrid.Mail() - message.set_subject('message subject') - message.set_html('HTML message body') - message.set_text('plaintext message body') - message.set_from('Example App Engine Sender '.format( - SENDGRID_DOMAIN)) - message.add_to(recipient) - status, msg = sg.send(message) - return (status, msg) + + sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY) + + to_email = mail.Email(recipient) + from_email = mail.Email(SENDGRID_SENDER) + subject = 'This is a test email' + content = mail.Content('text/plain', 'Example message.') + message = mail.Mail(from_email, subject, to_email, content) + + response = sg.client.mail.send.post(request_body=message.get()) + + return response # [END sendgrid-send] @@ -59,10 +61,9 @@ def get(self): class SendEmailHandler(webapp2.RequestHandler): def post(self): recipient = self.request.get('recipient') - (status, msg) = send_simple_message(recipient) - self.response.set_status(status) - if status == 200: - self.response.write(msg) + sg_response = send_simple_message(recipient) + self.response.set_status(sg_response.status_code) + self.response.write(sg_response.body) app = webapp2.WSGIApplication([ diff --git a/appengine/standard/sendgrid/main_test.py b/appengine/standard/sendgrid/main_test.py index f03ad69e9a0b..918493fc8e14 100644 --- a/appengine/standard/sendgrid/main_test.py +++ b/appengine/standard/sendgrid/main_test.py @@ -13,7 +13,6 @@ # limitations under the License. import main - import mock import pytest import webtest @@ -29,9 +28,18 @@ def test_get(app): assert response.status_int == 200 -@mock.patch.object(main.sg, 'send', return_value=(200, "OK")) -def test_post(send_mock, app): +@mock.patch('python_http_client.client.Client._make_request') +def test_post(make_request_mock, app): + response = mock.Mock() + response.getcode.return_value = 200 + response.read.return_value = 'OK' + response.info.return_value = {} + make_request_mock.return_value = response + app.post('/send', { - 'recipient': 'waprin@google.com' + 'recipient': 'user@example.com' }) - send_mock.assert_called_once_with(mock.ANY) + + assert make_request_mock.called + request = make_request_mock.call_args[0][1] + assert 'user@example.com' in request.data diff --git a/appengine/standard/sendgrid/requirements.txt b/appengine/standard/sendgrid/requirements.txt index 6efb6fdf814a..25c401066803 100644 --- a/appengine/standard/sendgrid/requirements.txt +++ b/appengine/standard/sendgrid/requirements.txt @@ -1 +1 @@ -sendgrid==2.2.1 +sendgrid==3.0.0