Skip to content

Commit 30b1ca6

Browse files
committed
feat: geolocation setter in sendgrid-python for GDPR compliance
1 parent 2fe1459 commit 30b1ca6

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sendgrid
2+
import os
3+
4+
from sendgrid import Email, To, Content, Mail
5+
6+
# Example 1
7+
# 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")
12+
subject = "Sending with SendGrid is Fun"
13+
content = Content("text/plain", "and easy to do anywhere, even with Python")
14+
print(sg.host)
15+
mail = Mail(from_email, to_email, subject, content)
16+
response = sg.client.mail.send.post(request_body=mail.get())
17+
print(response)
18+
print(response.status_code)
19+
print(response.body)
20+
print(response.headers)
21+
22+
# Example 2
23+
# 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")
28+
subject = "Sending with SendGrid is Fun"
29+
content = Content("text/plain", "and easy to do anywhere, even with Python")
30+
print(sg.host)
31+
mail = Mail(from_email, to_email, subject, content)
32+
response = sg.client.mail.send.post(request_body=mail.get())
33+
print(response)
34+
print(response.status_code)
35+
print(response.body)
36+
print(response.headers)

sendgrid/base_interface.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class BaseInterface(object):
5-
def __init__(self, auth, host, impersonate_subuser):
5+
def __init__(self, auth, host, region, impersonate_subuser):
66
"""
77
Construct the Twilio SendGrid v3 API object.
88
Note that the underlying client is being set up during initialization,
@@ -19,10 +19,15 @@ def __init__(self, auth, host, impersonate_subuser):
1919
:type impersonate_subuser: string
2020
:param host: base URL for API calls
2121
:type host: string
22+
:param region: To determine the region which can only be 'global' or 'eu'
23+
:type region: string
2224
"""
2325
from . import __version__
2426
self.auth = auth
25-
self.host = host
27+
if host is not None and region == 'global':
28+
self.set_host(host)
29+
else:
30+
self.set_region(region)
2631
self.impersonate_subuser = impersonate_subuser
2732
self.version = __version__
2833
self.useragent = 'sendgrid/{};python'.format(self.version)
@@ -60,3 +65,24 @@ def send(self, message):
6065
message = message.get()
6166

6267
return self.client.mail.send.post(request_body=message)
68+
69+
def set_host(self,host):
70+
self.host = host
71+
72+
def set_region(self,region):
73+
"""
74+
* Client libraries contain setters for specifying region/edge.
75+
* This allows support global and eu regions only. This set will likely expand in the future.
76+
* Global should be the default
77+
* Global region means the message should be sent through:
78+
* HTTP: api.sendgrid.com
79+
* EU region means the message should be sent through:
80+
* HTTP: api.eu.sendgrid.com
81+
:param region:
82+
:return:
83+
"""
84+
region_host_dict = {'eu':'https://api.eu.sendgrid.com','global':'https://api.sendgrid.com'}
85+
if region in region_host_dict.keys():
86+
self.host = region_host_dict[region]
87+
else:
88+
raise ValueError("region can only be \"eu\" or \"global\"")

sendgrid/sendgrid.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class SendGridAPIClient(BaseInterface):
3232
def __init__(
3333
self,
3434
api_key=None,
35-
host='https://api.sendgrid.com',
35+
host=None,
36+
region='global',
3637
impersonate_subuser=None):
3738
"""
3839
Construct the Twilio SendGrid v3 API object.
@@ -51,8 +52,11 @@ def __init__(
5152
:type impersonate_subuser: string
5253
:param host: base URL for API calls
5354
:type host: string
55+
:param region: To determine the region which can only be 'global' or 'eu'
56+
:type region: string
5457
"""
5558
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
5659
auth = 'Bearer {}'.format(self.api_key)
5760

58-
super(SendGridAPIClient, self).__init__(auth, host, impersonate_subuser)
61+
super(SendGridAPIClient, self).__init__(auth, host, region, impersonate_subuser)
62+

test/unit/test_sendgrid.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
import sendgrid
3+
4+
class UnitTests(unittest.TestCase):
5+
def test_host_with_no_region(self):
6+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
7+
self.assertEqual("https://api.sendgrid.com",sg.host)
8+
9+
def test_host_with_eu_region(self):
10+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
11+
sg.set_region("eu")
12+
self.assertEqual("https://api.eu.sendgrid.com",sg.host)
13+
14+
def test_host_with_global_region(self):
15+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
16+
sg.set_region("global")
17+
self.assertEqual("https://api.sendgrid.com",sg.host)
18+
19+
def test_host_with_host_first_eu_region_second(self):
20+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
21+
sg.set_host("https://sendgrid.com")
22+
sg.set_region("eu")
23+
self.assertEqual("https://api.eu.sendgrid.com",sg.host)
24+
25+
def test_host_with_eu_first_host_second(self):
26+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
27+
sg.set_region("eu")
28+
sg.set_host("https://sendgrid.com")
29+
self.assertEqual("https://sendgrid.com",sg.host)
30+
31+
def test_host_using_constructor(self):
32+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY',host='https://sendgrid.com')
33+
self.assertEqual("https://sendgrid.com",sg.host)
34+
35+
def test_with_region_is_none(self):
36+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
37+
with self.assertRaises(ValueError):
38+
sg.set_region("")
39+
40+
def test_with_region_is_none(self):
41+
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
42+
with self.assertRaises(ValueError):
43+
sg.set_region("abc")

0 commit comments

Comments
 (0)