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

Allow passing of subaccount #153

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion sparkpost/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 14 additions & 8 deletions sparkpost/transmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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
38 changes: 37 additions & 1 deletion test/test_base.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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(
Expand Down