-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from friedcell/tornado
More extensibility and Tornado integration
- Loading branch information
Showing
17 changed files
with
433 additions
and
32 deletions.
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.