Skip to content

Commit a1f2903

Browse files
author
Jon Wayne Parrott
authored
Update sendgrid (#401)
* Updating flex sendgrid sample. * Updating standard sendgrid sample.
1 parent 43c8ec6 commit a1f2903

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

appengine/flexible/sendgrid/main.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from flask import Flask, render_template, request
2020
import sendgrid
21+
from sendgrid.helpers import mail
2122

2223
# [START config]
2324
SENDGRID_API_KEY = os.environ['SENDGRID_API_KEY']
@@ -40,19 +41,18 @@ def send_email():
4041
return ('Please provide an email address in the "to" query string '
4142
'parameter.'), 400
4243

43-
sg = sendgrid.SendGridClient(SENDGRID_API_KEY)
44+
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
4445

45-
message = sendgrid.Mail(
46-
to=to,
47-
subject='This is a test email',
48-
html='<p>Example HTML body.</p>',
49-
text='Example text body.',
50-
from_email=SENDGRID_SENDER)
46+
to_email = mail.Email(to)
47+
from_email = mail.Email(SENDGRID_SENDER)
48+
subject = 'This is a test email'
49+
content = mail.Content('text/plain', 'Example message.')
50+
message = mail.Mail(from_email, subject, to_email, content)
5151

52-
status, response = sg.send(message)
52+
response = sg.client.mail.send.post(request_body=message.get())
5353

54-
if status != 200:
55-
return 'An error occurred: {}'.format(response), 500
54+
if response.status_code != 200:
55+
return 'An error occurred: {}'.format(response.body), 500
5656

5757
return 'Email sent.'
5858
# [END example]

appengine/flexible/sendgrid/main_test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import main
16-
1715
import mock
1816
import pytest
1917

@@ -34,11 +32,18 @@ def test_get(app):
3432
assert r.status_code == 200
3533

3634

37-
@mock.patch.object(
38-
main.sendgrid.SendGridClient, 'send', return_value=(200, "OK"))
39-
def test_post(send_mock, app):
40-
r = app.post('/send/email', data={
35+
@mock.patch('python_http_client.client.Client._make_request')
36+
def test_post(make_request_mock, app):
37+
response = mock.Mock()
38+
response.getcode.return_value = 200
39+
response.read.return_value = 'OK'
40+
response.info.return_value = {}
41+
make_request_mock.return_value = response
42+
43+
app.post('/send/email', data={
4144
4245
})
43-
assert r.status_code == 200
44-
assert send_mock.called
46+
47+
assert make_request_mock.called
48+
request = make_request_mock.call_args[0][1]
49+
assert '[email protected]' in request.data.decode('utf-8')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==0.11.1
2-
sendgrid==2.2.1
2+
sendgrid==3.0.0
33
gunicorn==19.6.0

appengine/standard/sendgrid/main.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616

1717
# [START sendgrid-imp]
1818
import sendgrid
19+
from sendgrid.helpers import mail
1920
# [END sendgrid-imp]
2021
import webapp2
2122

2223
# make a secure connection to SendGrid
2324
# [START sendgrid-config]
2425
SENDGRID_API_KEY = 'your-sendgrid-api-key'
25-
SENDGRID_DOMAIN = 'your-sendgrid-domain'
26+
SENDGRID_SENDER = 'your-sendgrid-sender'
2627
# [END sendgrid-config]
2728

28-
sg = sendgrid.SendGridClient(SENDGRID_API_KEY)
29-
3029

3130
def send_simple_message(recipient):
3231
# [START sendgrid-send]
33-
message = sendgrid.Mail()
34-
message.set_subject('message subject')
35-
message.set_html('<strong>HTML message body</strong>')
36-
message.set_text('plaintext message body')
37-
message.set_from('Example App Engine Sender <sendgrid@{}>'.format(
38-
SENDGRID_DOMAIN))
39-
message.add_to(recipient)
40-
status, msg = sg.send(message)
41-
return (status, msg)
32+
33+
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
34+
35+
to_email = mail.Email(recipient)
36+
from_email = mail.Email(SENDGRID_SENDER)
37+
subject = 'This is a test email'
38+
content = mail.Content('text/plain', 'Example message.')
39+
message = mail.Mail(from_email, subject, to_email, content)
40+
41+
response = sg.client.mail.send.post(request_body=message.get())
42+
43+
return response
4244
# [END sendgrid-send]
4345

4446

@@ -59,10 +61,9 @@ def get(self):
5961
class SendEmailHandler(webapp2.RequestHandler):
6062
def post(self):
6163
recipient = self.request.get('recipient')
62-
(status, msg) = send_simple_message(recipient)
63-
self.response.set_status(status)
64-
if status == 200:
65-
self.response.write(msg)
64+
sg_response = send_simple_message(recipient)
65+
self.response.set_status(sg_response.status_code)
66+
self.response.write(sg_response.body)
6667

6768

6869
app = webapp2.WSGIApplication([

appengine/standard/sendgrid/main_test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import main
16-
1716
import mock
1817
import pytest
1918
import webtest
@@ -29,9 +28,18 @@ def test_get(app):
2928
assert response.status_int == 200
3029

3130

32-
@mock.patch.object(main.sg, 'send', return_value=(200, "OK"))
33-
def test_post(send_mock, app):
31+
@mock.patch('python_http_client.client.Client._make_request')
32+
def test_post(make_request_mock, app):
33+
response = mock.Mock()
34+
response.getcode.return_value = 200
35+
response.read.return_value = 'OK'
36+
response.info.return_value = {}
37+
make_request_mock.return_value = response
38+
3439
app.post('/send', {
35-
'recipient': 'waprin@google.com'
40+
'recipient': 'user@example.com'
3641
})
37-
send_mock.assert_called_once_with(mock.ANY)
42+
43+
assert make_request_mock.called
44+
request = make_request_mock.call_args[0][1]
45+
assert '[email protected]' in request.data
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sendgrid==2.2.1
1+
sendgrid==3.0.0

0 commit comments

Comments
 (0)