Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: geolocation setter in sendgrid-python for GDPR compliance #1073

Merged
merged 22 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
30b1ca6
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
1e1299d
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
9d433a6
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
bb9535f
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
b81337d
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
7e56049
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
e7b9578
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
3bd6d6f
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
cf2cc5d
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
4019024
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 14, 2023
6ceb524
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 16, 2023
2a9930f
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 16, 2023
5885985
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 16, 2023
3dc77d8
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 20, 2023
da3f03c
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 20, 2023
f86747b
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 22, 2023
0a95877
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 22, 2023
41312f4
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 22, 2023
9f73f25
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 22, 2023
7eb07f7
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 22, 2023
d760424
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 23, 2023
7fc733d
feat: geolocation setter in sendgrid-python for GDPR compliance
manisha1997 Nov 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/dataresidency/set_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sendgrid
import os

from sendgrid import Email, To, Content, Mail

# Example 1
# setting region to be "global"

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("[email protected]")
to_email = To("[email protected]")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
print(sg.client.host)
manisha1997 marked this conversation as resolved.
Show resolved Hide resolved
mail = Mail(from_email, to_email, subject, content)
sg.set_sendgrid_data_residency("global")
response = sg.client.mail.send.post(request_body=mail.get())
print(response)
print(response.status_code)
print(response.body)
print(response.headers)

# Example 2
# setting region to "eu"
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY_EU'))
sg.set_sendgrid_data_residency("eu")
from_email = Email("[email protected]")
to_email = To("[email protected]")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
print(sg.client.host)
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response)
print(response.status_code)
print(response.body)
print(response.headers)
23 changes: 22 additions & 1 deletion sendgrid/base_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import python_http_client

region_host_dict = {'eu':'https://api.eu.sendgrid.com','global':'https://api.sendgrid.com'}

class BaseInterface(object):
def __init__(self, auth, host, impersonate_subuser):
Expand All @@ -22,10 +23,10 @@ def __init__(self, auth, host, impersonate_subuser):
"""
from . import __version__
self.auth = auth
self.host = host
self.impersonate_subuser = impersonate_subuser
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
self.host = host

self.client = python_http_client.Client(
host=self.host,
Expand Down Expand Up @@ -60,3 +61,23 @@ def send(self, message):
message = message.get()

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

def set_sendgrid_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: string
:return:
"""
if region in region_host_dict.keys():
self.host = region_host_dict[region]
if self._default_headers is not None:
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\"")
27 changes: 27 additions & 0 deletions test/unit/test_sendgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import sendgrid

class UnitTests(unittest.TestCase):
def test_host_with_no_region(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
self.assertEqual("https://api.sendgrid.com",sg.client.host)

def test_host_with_eu_region(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
sg.set_sendgrid_data_residency("eu")
self.assertEqual("https://api.eu.sendgrid.com",sg.client.host)

def test_host_with_global_region(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
sg.set_sendgrid_data_residency("global")
self.assertEqual("https://api.sendgrid.com",sg.client.host)

def test_with_region_is_none(self):
manisha1997 marked this conversation as resolved.
Show resolved Hide resolved
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
with self.assertRaises(ValueError):
sg.set_sendgrid_data_residency(None)

def test_with_region_is_invalid(self):
sg = sendgrid.SendGridAPIClient(api_key='MY_API_KEY')
with self.assertRaises(ValueError):
sg.set_sendgrid_data_residency("abc")
Loading