Skip to content

Add ability to reset request headers on client attribute #318

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

Merged
merged 1 commit into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 10 additions & 3 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def __init__(self, **opts):
self.host = opts.get('host', 'https://api.sendgrid.com')
self.version = __version__

headers = self._get_default_headers()

self.client = python_http_client.Client(host=self.host,
request_headers=headers,
version=3)

def _get_default_headers(self):
headers = {
"Authorization": 'Bearer {0}'.format(self._apikey),
"User-agent": self.useragent,
Expand All @@ -35,9 +41,10 @@ def __init__(self, **opts):
if self._impersonate_subuser:
headers['On-Behalf-Of'] = self._impersonate_subuser

self.client = python_http_client.Client(host=self.host,
request_headers=headers,
version=3)
return headers

def reset_request_headers(self):
self.client.request_headers = self._get_default_headers()

@property
def apikey(self):
Expand Down
31 changes: 31 additions & 0 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ def test_useragent(self):
def test_host(self):
self.assertEqual(self.sg.host, self.host)

def test_get_default_headers(self):
headers = self.sg._get_default_headers()
self.assertIn('Authorization', headers)
self.assertIn('User-agent', headers)
self.assertIn('Accept', headers)
self.assertNotIn('On-Behalf-Of', headers)

self.sg._impersonate_subuser = '[email protected]'
headers = self.sg._get_default_headers()
self.assertIn('Authorization', headers)
self.assertIn('User-agent', headers)
self.assertIn('Accept', headers)
self.assertIn('On-Behalf-Of', headers)

def test_reset_request_headers(self):
addl_headers = {
'blah': 'test value',
'blah2x': 'another test value',
}
self.sg.client.request_headers.update(addl_headers)
self.assertIn('blah', self.sg.client.request_headers)
self.assertIn('blah2x', self.sg.client.request_headers)

self.sg.reset_request_headers()
self.assertNotIn('blah', self.sg.client.request_headers)
self.assertNotIn('blah2x', self.sg.client.request_headers)

for k,v in self.sg._get_default_headers().items():
self.assertEqual(v, self.sg.client.request_headers[k])


def test_access_settings_activity_get(self):
params = {'limit': 1}
headers = {'X-Mock': 200}
Expand Down