Skip to content

Commit

Permalink
feat: geolocation setter in sendgrid-python for GDPR compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
manisha1997 committed Nov 16, 2023
1 parent 4019024 commit 6ceb524
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions examples/dataresidency/set_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

# Example 1
# setting region to be "global"
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
sg.set_region("global")
# sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))

sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
sg.set_data_residency("global")
from_email = Email("[email protected]")
to_email = To("[email protected]")
subject = "Sending with SendGrid is Fun"
Expand All @@ -19,10 +21,12 @@
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")
sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
sg.set_host("https://api.eu.sendgrid.com")
# sg.set_data_residency("eu")
from_email = Email("[email protected]")
to_email = To("[email protected]")
subject = "Sending with SendGrid is Fun"
Expand Down
13 changes: 9 additions & 4 deletions sendgrid/base_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def __init__(self, auth, host, impersonate_subuser, region='global'):
"""
from . import __version__
self.auth = auth
if host is not None and region == 'global':
self.set_host(host)
else:
self.set_data_residency(region)
self.host = host
self.impersonate_subuser = impersonate_subuser
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
Expand Down Expand Up @@ -69,6 +66,10 @@ def send(self, message):

def set_host(self,host):
self.host = host
self.client = python_http_client.Client(
host=self.host,
request_headers=self._default_headers,
version=3)

def set_data_residency(self,region):
"""
Expand All @@ -84,5 +85,9 @@ def set_data_residency(self,region):
"""
if region in region_host_dict.keys():
self.host = region_host_dict[region]
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\"")
2 changes: 1 addition & 1 deletion sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SendGridAPIClient(BaseInterface):
def __init__(
self,
api_key=None,
host=None,
host='https://api.sendgrid.com',
region='global',
impersonate_subuser=None):
"""
Expand Down

0 comments on commit 6ceb524

Please sign in to comment.