From eeed74a8aa14c38ab3c39ce54a2c84b464b5d5e1 Mon Sep 17 00:00:00 2001 From: Rich Leland Date: Wed, 24 May 2017 13:47:57 -0400 Subject: [PATCH 1/2] Add subaccount header and test to base resource --- sparkpost/base.py | 4 +++- test/test_base.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/sparkpost/base.py b/sparkpost/base.py index 4c3c467..bb721ed 100644 --- a/sparkpost/base.py +++ b/sparkpost/base.py @@ -32,10 +32,12 @@ def uri(self): return "%s/%s" % (self.base_uri, self.key) def request(self, method, uri, **kwargs): + subaccount = kwargs.pop('subaccount', 0) headers = { 'User-Agent': 'python-sparkpost/' + sparkpost.__version__, 'Content-Type': 'application/json', - 'Authorization': self.api_key + 'Authorization': self.api_key, + 'X-MSYS-SUBACCOUNT': subaccount } response = self.transport.request(method, uri, headers=headers, **kwargs) diff --git a/test/test_base.py b/test/test_base.py index aff6b79..f02cf09 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -1,7 +1,9 @@ import pytest import responses +from mock import patch -from sparkpost.base import Resource +import sparkpost +from sparkpost.base import Resource, RequestsTransport from sparkpost.exceptions import SparkPostAPIException @@ -22,6 +24,40 @@ def test_uri(): assert resource.uri == fake_uri +def test_default_headers(): + expected_headers = { + 'User-Agent': 'python-sparkpost/' + sparkpost.__version__, + 'Content-Type': 'application/json', + 'Authorization': fake_api_key, + 'X-MSYS-SUBACCOUNT': 0 + } + + with patch.object(RequestsTransport, + 'request', + return_value=None) as request_mock: + resource = create_resource() + resource.request('GET', resource.uri) + request_mock.assert_called_with('GET', resource.uri, + headers=expected_headers) + + +def test_subaccount_header(): + expected_headers = { + 'User-Agent': 'python-sparkpost/' + sparkpost.__version__, + 'Content-Type': 'application/json', + 'Authorization': fake_api_key, + 'X-MSYS-SUBACCOUNT': 123 + } + + with patch.object(RequestsTransport, + 'request', + return_value=None) as request_mock: + resource = create_resource() + resource.request('GET', resource.uri, subaccount=123) + request_mock.assert_called_with('GET', resource.uri, + headers=expected_headers) + + @responses.activate def test_success_request(): responses.add( From b16c46848a5ff51bbf1a481e43ac85637500e9be Mon Sep 17 00:00:00 2001 From: Rich Leland Date: Wed, 24 May 2017 14:21:10 -0400 Subject: [PATCH 2/2] Extract subaccount on all transmissions calls --- sparkpost/transmissions.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/sparkpost/transmissions.py b/sparkpost/transmissions.py index dcb7f86..4729fe2 100644 --- a/sparkpost/transmissions.py +++ b/sparkpost/transmissions.py @@ -251,15 +251,18 @@ def send(self, **kwargs): """ payload = self._translate_keys(**kwargs) - results = self.request('POST', self.uri, data=json.dumps(payload)) + subaccount = kwargs.pop('subaccount', 0) + results = self.request('POST', self.uri, data=json.dumps(payload), + subaccount=subaccount) return results - def _fetch_get(self, transmission_id): + def _fetch_get(self, transmission_id, **kwargs): uri = "%s/%s" % (self.uri, transmission_id) - results = self.request('GET', uri) + subaccount = kwargs.pop('subaccount', 0) + results = self.request('GET', uri, subaccount=subaccount) return results - def get(self, transmission_id): + def get(self, transmission_id, **kwargs): """ Get a transmission by ID @@ -268,7 +271,7 @@ def get(self, transmission_id): :returns: the requested transmission if found :raises: :exc:`SparkPostAPIException` if transmission is not found """ - results = self._fetch_get(transmission_id) + results = self._fetch_get(transmission_id, **kwargs) return results['transmission'] def list(self, **kwargs): @@ -285,9 +288,11 @@ def list(self, **kwargs): 'check https://sparkpo.st/5qcj4.' warnings.warn(warn_msg, DeprecationWarning) - return self.request('GET', self.uri, params=kwargs) + subaccount = kwargs.pop('subaccount', 0) + return self.request('GET', self.uri, params=kwargs, + subaccount=subaccount) - def delete(self, transmission_id): + def delete(self, transmission_id, **kwargs): """ Delete a transmission by ID @@ -298,5 +303,6 @@ def delete(self, transmission_id): or Canceled """ uri = "%s/%s" % (self.uri, transmission_id) - results = self.request('DELETE', uri) + subaccount = kwargs.pop('subaccount', 0) + results = self.request('DELETE', uri, subaccount=subaccount) return results