From f86747b1d688178f509b14809e4363d7bc0e3269 Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 22 Nov 2023 14:01:25 +0530 Subject: [PATCH] feat: geolocation setter in sendgrid-python for GDPR compliance --- sendgrid/base_interface.py | 18 +++--------------- sendgrid/sendgrid.py | 3 +-- test/unit/test_sendgrid.py | 22 +--------------------- 3 files changed, 5 insertions(+), 38 deletions(-) diff --git a/sendgrid/base_interface.py b/sendgrid/base_interface.py index b95847d5..0288cdfe 100644 --- a/sendgrid/base_interface.py +++ b/sendgrid/base_interface.py @@ -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, @@ -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, @@ -67,13 +62,6 @@ 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. @@ -81,7 +69,7 @@ def set_data_residency(self, region): 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(): diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index 608bd352..d8d0610d 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -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. @@ -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) diff --git a/test/unit/test_sendgrid.py b/test/unit/test_sendgrid.py index 72b88ba6..ef5db13e 100644 --- a/test/unit/test_sendgrid.py +++ b/test/unit/test_sendgrid.py @@ -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): @@ -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) \ No newline at end of file + sg.set_data_residency("abc") \ No newline at end of file