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

Client cleanup #557

Merged
merged 7 commits into from
Jun 7, 2018
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
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
DEALINGS IN THE SOFTWARE.
92 changes: 48 additions & 44 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


import os
import warnings

import python_http_client

from .version import __version__
Expand All @@ -32,70 +34,72 @@ class SendGridAPIClient(object):
https://github.com/sendgrid/sendgrid-python
"""

def __init__(self, **opts):
def __init__(
self,
apikey=None,
api_key=None,
impersonate_subuser=None,
host='https://api.sendgrid.com',
**opts): # TODO: remove **opts for 6.x release
"""
Construct SendGrid v3 API object.

:params host: Base URL for the API call
:type host: string
:params apikey: SendGrid API key to use. Defaults to environment var.
:type apikey: string
Note that underlying client being set up during initialization, therefore changing
attributes in runtime will not affect HTTP client behaviour.

:param apikey: SendGrid API key to use. If not provided, key will be read from
environment variable "SENDGRID_API_KEY"
:type apikey: basestring
:param api_key: SendGrid API key to use. Provides backward compatibility
.. deprecated:: 5.3
Use apikey instead
:type api_key: basestring
:param impersonate_subuser: the subuser to impersonate. Will be passed by
"On-Behalf-Of" header by underlying client.
See https://sendgrid.com/docs/User_Guide/Settings/subusers.html for more details
:type impersonate_subuser: basestring
:param host: base URL for API calls
:type host: basestring
:param opts: dispatcher for deprecated arguments. Added for backward-compatibility
with `path` parameter. Should be removed during 6.x release
"""
self.path = opts.get(
'path', os.path.abspath(os.path.dirname(__file__)))
self._apikey = opts.get('apikey', os.environ.get('SENDGRID_API_KEY'))
# 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)
if opts:
warnings.warn(
'Unsupported argument(s) provided: {}'.format(list(opts.keys())),
DeprecationWarning)
self.apikey = apikey or api_key or os.environ.get('SENDGRID_API_KEY')
self.impersonate_subuser = impersonate_subuser
self.host = host
self.useragent = 'sendgrid/{0};python'.format(__version__)
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,
request_headers=self._default_headers,
version=3)

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

return headers

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

@property
def apikey(self):
"""The API key (also accessible as api_key)."""
return self._apikey

@apikey.setter
def apikey(self, value):
self._apikey = value
self.client.request_headers = self._default_headers

@property
def api_key(self):
"""The API key (also accessible as apikey)."""
return self._apikey
"""
Alias for reading API key
.. deprecated:: 5.3
Use apikey instead
"""
return self.apikey

@api_key.setter
def api_key(self, value):
self._apikey = value

@property
def impersonate_subuser(self):
"""
The subuser you are impersonating.

If present, this is the value of the "On-Behalf-Of" header.
"""
return self._impersonate_subuser
self.apikey = value
15 changes: 6 additions & 9 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def setUpClass(cls):
cls.path = '{0}{1}'.format(
os.path.abspath(
os.path.dirname(__file__)), '/..')
cls.sg = sendgrid.SendGridAPIClient(
host=host, path=cls.path,
api_key=os.environ.get('SENDGRID_API_KEY'))
cls.sg = sendgrid.SendGridAPIClient(host=host)
cls.devnull = open(os.devnull, 'w')
prism_cmd = None

Expand Down Expand Up @@ -98,8 +96,7 @@ def test_api_key_setter(self):
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'),
host=host,
impersonate_subuser=temp_subuser)
self.assertEqual(sg_impersonate.impersonate_subuser, temp_subuser)

Expand All @@ -111,14 +108,14 @@ def test_host(self):
self.assertEqual(self.sg.host, self.host)

def test_get_default_headers(self):
headers = self.sg._get_default_headers()
headers = self.sg._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.sg.impersonate_subuser = '[email protected]'
headers = self.sg._default_headers
self.assertIn('Authorization', headers)
self.assertIn('User-agent', headers)
self.assertIn('Accept', headers)
Expand All @@ -137,7 +134,7 @@ def test_reset_request_headers(self):
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():
for k,v in self.sg._default_headers.items():
self.assertEqual(v, self.sg.client.request_headers[k])

def test_hello_world(self):
Expand Down