Skip to content

Commit

Permalink
feat: geolocation setter in sendgrid-python for GDPR compliance (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
manisha1997 authored Nov 23, 2023
1 parent d3ddc8a commit b5afcca
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: pip install --user ruff
- run: ruff --format=github --ignore="E722,F40,F841" --line-length=681 --target-version=py37 .
- run: ruff --ignore="E722,F40,F841" --line-length=320 --target-version=py37 .
37 changes: 37 additions & 0 deletions examples/dataresidency/set_region.py
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)
23 changes: 22 additions & 1 deletion sendgrid/base_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import python_http_client

region_host_dict = {'eu':'https://api.eu.sendgrid.com','global':'https://api.sendgrid.com'}

class BaseInterface(object):
def __init__(self, auth, host, impersonate_subuser):
Expand All @@ -22,10 +23,10 @@ def __init__(self, auth, host, impersonate_subuser):
"""
from . import __version__
self.auth = auth
self.host = host
self.impersonate_subuser = impersonate_subuser
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
self.host = host

self.client = python_http_client.Client(
host=self.host,
Expand Down Expand Up @@ -60,3 +61,23 @@ def send(self, message):
message = message.get()

return self.client.mail.send.post(request_body=message)

def set_sendgrid_data_residency(self, region):
"""
Client libraries contain setters for specifying region/edge.
This supports global and eu regions only. This set will likely expand in the future.
Global is the default residency (or region)
Global region means the message will be sent through https://api.sendgrid.com
EU region means the message will be sent through https://api.eu.sendgrid.com
:param region: string
:return:
"""
if region in region_host_dict.keys():
self.host = region_host_dict[region]
if self._default_headers is not None:
self.client = python_http_client.Client(
host=self.host,
request_headers=self._default_headers,
version=3)
else:
raise ValueError("region can only be \"eu\" or \"global\"")
27 changes: 27 additions & 0 deletions test/unit/test_sendgrid.py
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")

0 comments on commit b5afcca

Please sign in to comment.