diff --git a/LICENSE.txt b/LICENSE.txt index 8fc986624..69511d70c 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -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. \ No newline at end of file +DEALINGS IN THE SOFTWARE. diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index b6c731b94..0f09bd542 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -14,6 +14,8 @@ import os +import warnings + import python_http_client from .version import __version__ @@ -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 diff --git a/test/test_sendgrid.py b/test/test_sendgrid.py index f71381c64..c545cbb2d 100644 --- a/test/test_sendgrid.py +++ b/test/test_sendgrid.py @@ -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 @@ -98,8 +96,7 @@ def test_api_key_setter(self): def test_impersonate_subuser_init(self): temp_subuser = 'abcxyz@this.is.a.test.subuser' 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) @@ -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 = 'ladida@testsubuser.sendgrid' - headers = self.sg._get_default_headers() + self.sg.impersonate_subuser = 'ladida@testsubuser.sendgrid' + headers = self.sg._default_headers self.assertIn('Authorization', headers) self.assertIn('User-agent', headers) self.assertIn('Accept', headers) @@ -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):