-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: geolocation setter in sendgrid-python for GDPR compliance
- Loading branch information
1 parent
2fe1459
commit 30b1ca6
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import sendgrid | ||
import os | ||
|
||
from sendgrid import Email, To, Content, Mail | ||
|
||
# Example 1 | ||
# setting region to be "global" | ||
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) | ||
sg.set_region("global") | ||
from_email = Email("[email protected]") | ||
to_email = To("[email protected]") | ||
subject = "Sending with SendGrid is Fun" | ||
content = Content("text/plain", "and easy to do anywhere, even with Python") | ||
print(sg.host) | ||
mail = Mail(from_email, to_email, subject, content) | ||
response = sg.client.mail.send.post(request_body=mail.get()) | ||
print(response) | ||
print(response.status_code) | ||
print(response.body) | ||
print(response.headers) | ||
|
||
# Example 2 | ||
# setting region to "eu" | ||
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) | ||
sg.set_region("eu") | ||
from_email = Email("[email protected]") | ||
to_email = To("[email protected]") | ||
subject = "Sending with SendGrid is Fun" | ||
content = Content("text/plain", "and easy to do anywhere, even with Python") | ||
print(sg.host) | ||
mail = Mail(from_email, to_email, subject, content) | ||
response = sg.client.mail.send.post(request_body=mail.get()) | ||
print(response) | ||
print(response.status_code) | ||
print(response.body) | ||
print(response.headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
import sendgrid | ||
|
||
class UnitTests(unittest.TestCase): | ||
def test_host_with_no_region(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
self.assertEqual("https://api.sendgrid.com",sg.host) | ||
|
||
def test_host_with_eu_region(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_region("eu") | ||
self.assertEqual("https://api.eu.sendgrid.com",sg.host) | ||
|
||
def test_host_with_global_region(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_region("global") | ||
self.assertEqual("https://api.sendgrid.com",sg.host) | ||
|
||
def test_host_with_host_first_eu_region_second(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_host("https://sendgrid.com") | ||
sg.set_region("eu") | ||
self.assertEqual("https://api.eu.sendgrid.com",sg.host) | ||
|
||
def test_host_with_eu_first_host_second(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_region("eu") | ||
sg.set_host("https://sendgrid.com") | ||
self.assertEqual("https://sendgrid.com",sg.host) | ||
|
||
def test_host_using_constructor(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY',host='https://sendgrid.com') | ||
self.assertEqual("https://sendgrid.com",sg.host) | ||
|
||
def test_with_region_is_none(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
with self.assertRaises(ValueError): | ||
sg.set_region("") | ||
|
||
def test_with_region_is_none(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
with self.assertRaises(ValueError): | ||
sg.set_region("abc") |