Skip to content

Commit 4780178

Browse files
committed
feat: geolocation setter in sendgrid-python for GDPR compliance
1 parent 4019024 commit 4780178

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

examples/dataresidency/set_region.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
# Example 1
77
# setting region to be "global"
8-
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
9-
sg.set_region("global")
10-
from_email = Email("example@abc.com")
11-
to_email = To("example@abc.com")
8+
# sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
9+
10+
sg = sendgrid.SendGridAPIClient('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')
11+
sg.set_data_residency("global")
12+
from_email = Email("manisingh@twilio.com")
13+
to_email = To("manisingh@twilio.com")
1214
subject = "Sending with SendGrid is Fun"
1315
content = Content("text/plain", "and easy to do anywhere, even with Python")
1416
print(sg.host)
@@ -19,12 +21,14 @@
1921
print(response.body)
2022
print(response.headers)
2123

24+
2225
# Example 2
2326
# setting region to "eu"
24-
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
25-
sg.set_region("eu")
26-
from_email = Email("example@abc.com")
27-
to_email = To("example@abc.com")
27+
sg = sendgrid.SendGridAPIClient('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')
28+
sg.set_host("https://api.eu.sendgrid.com")
29+
# sg.set_data_residency("eu")
30+
from_email = Email("sburman@twilio.com")
31+
to_email = To("sburman@twilio.com")
2832
subject = "Sending with SendGrid is Fun"
2933
content = Content("text/plain", "and easy to do anywhere, even with Python")
3034
print(sg.host)

sendgrid/base_interface.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ def __init__(self, auth, host, impersonate_subuser, region='global'):
2525
"""
2626
from . import __version__
2727
self.auth = auth
28-
if host is not None and region == 'global':
29-
self.set_host(host)
30-
else:
31-
self.set_data_residency(region)
28+
self.host = host
3229
self.impersonate_subuser = impersonate_subuser
3330
self.version = __version__
3431
self.useragent = 'sendgrid/{};python'.format(self.version)
@@ -69,6 +66,10 @@ def send(self, message):
6966

7067
def set_host(self,host):
7168
self.host = host
69+
self.client = python_http_client.Client(
70+
host=self.host,
71+
request_headers=self._default_headers,
72+
version=3)
7273

7374
def set_data_residency(self,region):
7475
"""
@@ -84,5 +85,9 @@ def set_data_residency(self,region):
8485
"""
8586
if region in region_host_dict.keys():
8687
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)
8792
else:
8893
raise ValueError("region can only be \"eu\" or \"global\"")

sendgrid/sendgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SendGridAPIClient(BaseInterface):
3232
def __init__(
3333
self,
3434
api_key=None,
35-
host=None,
35+
host='https://api.sendgrid.com',
3636
region='global',
3737
impersonate_subuser=None):
3838
"""

0 commit comments

Comments
 (0)