Skip to content

Add ability to impersonate subuser #314

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
May 9, 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
9 changes: 9 additions & 0 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ def __init__(self, **opts):
# Support v2 api_key naming
self._apikey = opts.get('api_key', self._apikey)
self._api_key = self._apikey
# Support impersonation of subusers
self._impersonate_subuser = opts.get('impersonate_subuser', None)
self.useragent = 'sendgrid/{0};python'.format(__version__)
self.host = opts.get('host', 'https://api.sendgrid.com')
self.version = __version__


headers = {
"Authorization": 'Bearer {0}'.format(self._apikey),
"User-agent": self.useragent,
"Accept": 'application/json'
}
if self._impersonate_subuser:
headers['On-Behalf-Of'] = self._impersonate_subuser

self.client = python_http_client.Client(host=self.host,
request_headers=headers,
Expand All @@ -49,3 +54,7 @@ def api_key(self):
@api_key.setter
def api_key(self, value):
self._apikey = value

@property
def impersonate_subuser(self):
return self._impersonate_subuser
8 changes: 8 additions & 0 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_apikey_init(self):
my_sendgrid = sendgrid.SendGridAPIClient(apikey="THISISMYKEY")
self.assertEqual(my_sendgrid.apikey, "THISISMYKEY")

def test_impersonate_subuser_init(self):
temp_subuser = '[email protected]'
sg_impersonate = sendgrid.SendGridAPIClient(
host=host, path=self.path,
api_key=os.environ.get('SENDGRID_API_KEY'),
impersonate_subuser=temp_subuser)
self.assertEqual(sg_impersonate.impersonate_subuser, temp_subuser)

def test_useragent(self):
useragent = '{0}{1}{2}'.format('sendgrid/', __version__, ';python')
self.assertEqual(self.sg.useragent, useragent)
Expand Down