-
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 (#1073)
- Loading branch information
1 parent
d3ddc8a
commit b5afcca
Showing
4 changed files
with
87 additions
and
2 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
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,37 @@ | ||
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')) | ||
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") | ||
mail = Mail(from_email, to_email, subject, content) | ||
sg.set_sendgrid_data_residency("global") | ||
print(sg.client.host) | ||
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_EU')) | ||
sg.set_sendgrid_data_residency("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.client.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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.client.host) | ||
|
||
def test_host_with_eu_region(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_sendgrid_data_residency("eu") | ||
self.assertEqual("https://api.eu.sendgrid.com",sg.client.host) | ||
|
||
def test_host_with_global_region(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
sg.set_sendgrid_data_residency("global") | ||
self.assertEqual("https://api.sendgrid.com",sg.client.host) | ||
|
||
def test_with_region_is_none(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
with self.assertRaises(ValueError): | ||
sg.set_sendgrid_data_residency(None) | ||
|
||
def test_with_region_is_invalid(self): | ||
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY') | ||
with self.assertRaises(ValueError): | ||
sg.set_sendgrid_data_residency("abc") |