Skip to content

Commit 3dc77d8

Browse files
committed
feat: geolocation setter in sendgrid-python for GDPR compliance
1 parent 5885985 commit 3dc77d8

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

examples/dataresidency/set_region.py

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
# Example 1
77
# setting region to be "global"
8-
# sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
98

109
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
1110
sg.set_data_residency("global")

sendgrid/base_interface.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ def __init__(self, auth, host, impersonate_subuser, region='global'):
2525
"""
2626
from . import __version__
2727
self.auth = auth
28-
self.host = host
2928
self.impersonate_subuser = impersonate_subuser
3029
self.version = __version__
3130
self.useragent = 'sendgrid/{};python'.format(self.version)
31+
if region is None:
32+
self.host = host
33+
else:
34+
self.set_data_residency(region=region)
3235

3336
self.client = python_http_client.Client(
3437
host=self.host,
@@ -64,30 +67,29 @@ def send(self, message):
6467

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

67-
def set_host(self,host):
70+
def set_host(self, host):
6871
self.host = host
6972
self.client = python_http_client.Client(
7073
host=self.host,
7174
request_headers=self._default_headers,
7275
version=3)
7376

74-
def set_data_residency(self,region):
77+
def set_data_residency(self, region):
7578
"""
76-
* Client libraries contain setters for specifying region/edge.
77-
* This allows support global and eu regions only. This set will likely expand in the future.
78-
* Global should be the default
79-
* Global region means the message should be sent through:
80-
* HTTP: api.sendgrid.com
81-
* EU region means the message should be sent through:
82-
* HTTP: api.eu.sendgrid.com
79+
Client libraries contain setters for specifying region/edge.
80+
This supports global and eu regions only. This set will likely expand in the future.
81+
Global is the default residency (or region)
82+
Global region means the message will be sent through https://api.sendgrid.com
83+
EU region means the message will be sent through https://api.eu.sendgrid.com
8384
:param region:
8485
:return:
8586
"""
8687
if region in region_host_dict.keys():
8788
self.host = region_host_dict[region]
88-
self.client = python_http_client.Client(
89-
host=self.host,
90-
request_headers=self._default_headers,
91-
version=3)
89+
if self._default_headers is not None:
90+
self.client = python_http_client.Client(
91+
host=self.host,
92+
request_headers=self._default_headers,
93+
version=3)
9294
else:
9395
raise ValueError("region can only be \"eu\" or \"global\"")

sendgrid/sendgrid.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(
3333
self,
3434
api_key=None,
3535
host='https://api.sendgrid.com',
36-
region='global',
36+
region=None,
3737
impersonate_subuser=None):
3838
"""
3939
Construct the Twilio SendGrid v3 API object.

test/unit/test_sendgrid.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ def test_with_region_is_none(self):
4040
def test_with_region_is_invalid(self):
4141
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
4242
with self.assertRaises(ValueError):
43-
sg.set_data_residency("abc")
43+
sg.set_data_residency("abc")
44+
45+
def test_host_with_both_host_and_region_in_constructor(self):
46+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY',host="https://example.com",region="eu")
47+
self.assertEqual("https://api.eu.sendgrid.com",sg.client.host)

0 commit comments

Comments
 (0)