Skip to content

Commit 28e97dd

Browse files
committed
Add ability to reset request headers on client attribute
Because of the way SendGridAPIClient and python_http_client are implemented, request headers are always cached. This becomes a problem when executing a sequence of API requests that require varying request header values using the same SendGridAPIClient instance. This method allows the caller to reset the request headers on the client instance to their default.
1 parent 4ffc00e commit 28e97dd

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

sendgrid/sendgrid.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def __init__(self, **opts):
2626
self.host = opts.get('host', 'https://api.sendgrid.com')
2727
self.version = __version__
2828

29+
headers = self._get_default_headers()
2930

31+
self.client = python_http_client.Client(host=self.host,
32+
request_headers=headers,
33+
version=3)
34+
35+
def _get_default_headers(self):
3036
headers = {
3137
"Authorization": 'Bearer {0}'.format(self._apikey),
3238
"User-agent": self.useragent,
@@ -35,9 +41,10 @@ def __init__(self, **opts):
3541
if self._impersonate_subuser:
3642
headers['On-Behalf-Of'] = self._impersonate_subuser
3743

38-
self.client = python_http_client.Client(host=self.host,
39-
request_headers=headers,
40-
version=3)
44+
return headers
45+
46+
def reset_request_headers(self):
47+
self.client.request_headers = self._get_default_headers()
4148

4249
@property
4350
def apikey(self):

test/test_sendgrid.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@ def test_useragent(self):
7878
def test_host(self):
7979
self.assertEqual(self.sg.host, self.host)
8080

81+
def test_get_default_headers(self):
82+
headers = self.sg._get_default_headers()
83+
self.assertIn('Authorization', headers)
84+
self.assertIn('User-agent', headers)
85+
self.assertIn('Accept', headers)
86+
self.assertNotIn('On-Behalf-Of', headers)
87+
88+
self.sg._impersonate_subuser = 'ladida@testsubuser.sendgrid'
89+
headers = self.sg._get_default_headers()
90+
self.assertIn('Authorization', headers)
91+
self.assertIn('User-agent', headers)
92+
self.assertIn('Accept', headers)
93+
self.assertIn('On-Behalf-Of', headers)
94+
95+
def test_reset_request_headers(self):
96+
addl_headers = {
97+
'blah': 'test value',
98+
'blah2x': 'another test value',
99+
}
100+
self.sg.client.request_headers.update(addl_headers)
101+
self.assertIn('blah', self.sg.client.request_headers)
102+
self.assertIn('blah2x', self.sg.client.request_headers)
103+
104+
self.sg.reset_request_headers()
105+
self.assertNotIn('blah', self.sg.client.request_headers)
106+
self.assertNotIn('blah2x', self.sg.client.request_headers)
107+
108+
for k,v in self.sg._get_default_headers().items():
109+
self.assertEqual(v, self.sg.client.request_headers[k])
110+
111+
81112
def test_access_settings_activity_get(self):
82113
params = {'limit': 1}
83114
headers = {'X-Mock': 200}

0 commit comments

Comments
 (0)