Skip to content

Commit

Permalink
Update sendgrid (#401)
Browse files Browse the repository at this point in the history
* Updating flex sendgrid sample.
* Updating standard sendgrid sample.
  • Loading branch information
Jon Wayne Parrott committed Jul 6, 2016
1 parent 43c8ec6 commit a1f2903
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
20 changes: 10 additions & 10 deletions appengine/flexible/sendgrid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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='<p>Example HTML body.</p>',
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]
Expand Down
21 changes: 13 additions & 8 deletions appengine/flexible/sendgrid/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import main

import mock
import pytest

Expand All @@ -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')
2 changes: 1 addition & 1 deletion appengine/flexible/sendgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
sendgrid==2.2.1
sendgrid==3.0.0
gunicorn==19.6.0
33 changes: 17 additions & 16 deletions appengine/standard/sendgrid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<strong>HTML message body</strong>')
message.set_text('plaintext message body')
message.set_from('Example App Engine Sender <sendgrid@{}>'.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]


Expand All @@ -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([
Expand Down
18 changes: 13 additions & 5 deletions appengine/standard/sendgrid/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import main

import mock
import pytest
import webtest
Expand All @@ -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
2 changes: 1 addition & 1 deletion appengine/standard/sendgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sendgrid==2.2.1
sendgrid==3.0.0

0 comments on commit a1f2903

Please sign in to comment.