-
Notifications
You must be signed in to change notification settings - Fork 68
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
More extensibility and Tornado integration #94
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
248eaee
removing an extra print
ef018fc
moved get_api_key to class for easier subclassing
78f6f91
split out transport for easier subclassing
3b6561e
split out _fetch_get to help with async transport subclassing
47ee0e8
make APIException more resilient
32ae8ae
add tornado integration that uses async transport
d4759af
update authors and version
59893b3
pep8
256a9d8
fixed py3 - tornado body is bytes, not str
d1dc0e5
updated version
5ee8845
__init__ never returns
63efb64
Merge branch 'master' of https://github.com/sparkpost/python-sparkpos…
9d7a191
save details in error for possible inspection later
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
wheel | ||
twine | ||
Django>=1.7,<1.10 | ||
tornado>=3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import sparkpost | ||
|
||
from .exceptions import SparkPostAPIException | ||
from .base import TornadoTransport | ||
from .transmissions import Transmissions | ||
|
||
__all__ = ["SparkPost", "TornadoTransport", "SparkPostAPIException", | ||
"Transmissions"] | ||
|
||
|
||
class SparkPost(sparkpost.SparkPost): | ||
TRANSPORT_CLASS = TornadoTransport | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(SparkPost, self).__init__(*args, **kwargs) | ||
self.transmissions = Transmissions(self.base_uri, self.api_key, | ||
self.TRANSPORT_CLASS) | ||
self.transmission = self.transmissions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
from tornado import gen | ||
from tornado.httpclient import AsyncHTTPClient, HTTPError | ||
|
||
from .exceptions import SparkPostAPIException | ||
|
||
|
||
class TornadoTransport(object): | ||
@gen.coroutine | ||
def request(self, method, uri, headers, **kwargs): | ||
if "data" in kwargs: | ||
kwargs["body"] = kwargs.pop("data") | ||
client = AsyncHTTPClient() | ||
try: | ||
response = yield client.fetch(uri, method=method, headers=headers, | ||
**kwargs) | ||
except HTTPError as ex: | ||
raise SparkPostAPIException(ex.response) | ||
if response.code == 204: | ||
raise gen.Return(True) | ||
if response.code == 200: | ||
result = None | ||
try: | ||
result = json.loads(response.body.decode("utf-8")) | ||
except: | ||
pass | ||
if result: | ||
if 'results' in result: | ||
raise gen.Return(result['results']) | ||
raise gen.Return(result) | ||
raise SparkPostAPIException(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
|
||
from ..exceptions import SparkPostAPIException as RequestsSparkPostAPIException | ||
|
||
|
||
class SparkPostAPIException(RequestsSparkPostAPIException): | ||
def __init__(self, response, *args, **kwargs): | ||
errors = None | ||
try: | ||
data = json.loads(response.body.decode("utf-8")) | ||
if data: | ||
errors = data['errors'] | ||
errors = [e['message'] + ': ' + e.get('description', '') | ||
for e in errors] | ||
except: | ||
pass | ||
if not errors: | ||
errors = [response.body.decode("utf-8") or ""] | ||
self.status = response.code | ||
self.response = response | ||
self.errors = errors | ||
message = """Call to {uri} returned {status_code}, errors: | ||
|
||
{errors} | ||
""".format( | ||
uri=response.effective_url, | ||
status_code=response.code, | ||
errors='\n'.join(errors) | ||
) | ||
super(RequestsSparkPostAPIException, self).__init__(message, *args, | ||
**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .utils import wrap_future | ||
from ..transmissions import Transmissions as SyncTransmissions | ||
|
||
|
||
class Transmissions(SyncTransmissions): | ||
def get(self, transmission_id): | ||
results = self._fetch_get(transmission_id) | ||
return wrap_future(results, lambda f: f["transmission"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from tornado.concurrent import Future | ||
|
||
|
||
def wrap_future(future, convert): | ||
wrapper = Future() | ||
|
||
def handle_future(future): | ||
try: | ||
wrapper.set_result(convert(future.result())) | ||
except Exception as ex: | ||
wrapper.set_exception(ex) | ||
|
||
future.add_done_callback(handle_future) | ||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason this had to be moved out into a separate function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support async transports fetch methods should only return whatever they fetched - in async it will be a future, not a real value that can be manipulated. It enables code in sparkpost/tornado/transmissions.py