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 22, 2023
1 parent da3f03c commit f86747b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 38 deletions.
18 changes: 3 additions & 15 deletions sendgrid/base_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
region_host_dict = {'eu':'https://api.eu.sendgrid.com','global':'https://api.sendgrid.com'}

class BaseInterface(object):
def __init__(self, auth, host, impersonate_subuser, region=None):
def __init__(self, auth, host, impersonate_subuser):
"""
Construct the Twilio SendGrid v3 API object.
Note that the underlying client is being set up during initialization,
Expand All @@ -20,18 +20,13 @@ def __init__(self, auth, host, impersonate_subuser, region=None):
:type impersonate_subuser: string
:param host: base URL for API calls
:type host: string
:param region: To determine the region which can only be 'global' or 'eu'
:type region: string
"""
from . import __version__
self.auth = auth
self.impersonate_subuser = impersonate_subuser
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
if region is None:
self.host = host
else:
self.set_data_residency(region=region)
self.host = host

self.client = python_http_client.Client(
host=self.host,
Expand Down Expand Up @@ -67,21 +62,14 @@ def send(self, message):

return self.client.mail.send.post(request_body=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):
"""
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:
:param region: string
:return:
"""
if region in region_host_dict.keys():
Expand Down
3 changes: 1 addition & 2 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(
self,
api_key=None,
host='https://api.sendgrid.com',
region=None,
impersonate_subuser=None):
"""
Construct the Twilio SendGrid v3 API object.
Expand All @@ -58,5 +57,5 @@ def __init__(
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
auth = 'Bearer {}'.format(self.api_key)

super(SendGridAPIClient, self).__init__(auth, host, impersonate_subuser,region)
super(SendGridAPIClient, self).__init__(auth, host, impersonate_subuser)

22 changes: 1 addition & 21 deletions test/unit/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ def test_host_with_global_region(self):
sg.set_data_residency("global")
self.assertEqual("https://api.sendgrid.com",sg.client.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_data_residency("eu")
self.assertEqual("https://api.eu.sendgrid.com",sg.client.host)

def test_host_with_eu_first_host_second(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
sg.set_data_residency("eu")
sg.set_host("https://sendgrid.com")
self.assertEqual("https://sendgrid.com",sg.client.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.client.host)

def test_with_region_is_none(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
with self.assertRaises(ValueError):
Expand All @@ -40,8 +24,4 @@ def test_with_region_is_none(self):
def test_with_region_is_invalid(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
with self.assertRaises(ValueError):
sg.set_data_residency("abc")

def test_host_with_both_host_and_region_in_constructor(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY',host="https://example.com",region="eu")
self.assertEqual("https://api.eu.sendgrid.com",sg.client.host)
sg.set_data_residency("abc")

0 comments on commit f86747b

Please sign in to comment.