mirror of
https://github.com/sprockets/sprockets.mixins.correlation.git
synced 2024-11-14 19:29:30 +00:00
Drop support for Tornado 4.2 and Simplify
This commit is contained in:
parent
221df4f773
commit
467ebe575c
4 changed files with 9 additions and 107 deletions
|
@ -1,16 +1,11 @@
|
|||
try:
|
||||
from .mixins import HandlerMixin, AsyncIOHandlerMixin
|
||||
|
||||
except ImportError as error:
|
||||
from .mixins import HandlerMixin
|
||||
except ImportError:
|
||||
|
||||
class HandlerMixin(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise error
|
||||
raise ImportError
|
||||
|
||||
|
||||
class AsyncIOHandlerMixin(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
raise error
|
||||
|
||||
version_info = (1, 0, 2)
|
||||
version_info = (2, 0, 0)
|
||||
__version__ = '.'.join(str(v) for v in version_info[:3])
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import uuid
|
||||
|
||||
from tornado import concurrent, gen, log
|
||||
from tornado import concurrent, log
|
||||
|
||||
|
||||
class HandlerMixin(object):
|
||||
|
@ -41,14 +41,13 @@ class HandlerMixin(object):
|
|||
self.__correlation_id = str(uuid.uuid4())
|
||||
super(HandlerMixin, self).__init__(*args, **kwargs)
|
||||
|
||||
@gen.coroutine
|
||||
def prepare(self):
|
||||
async def prepare(self):
|
||||
# Here we want to copy an incoming Correlation-ID header if
|
||||
# one exists. We also want to set it in the outgoing response
|
||||
# which the property setter does for us.
|
||||
maybe_future = super(HandlerMixin, self).prepare()
|
||||
if concurrent.is_future(maybe_future):
|
||||
yield maybe_future
|
||||
await maybe_future
|
||||
|
||||
correlation_id = self.get_request_header(self.__header_name, None)
|
||||
if correlation_id is not None:
|
||||
|
@ -89,58 +88,6 @@ class HandlerMixin(object):
|
|||
return self.request.headers.get(name, default)
|
||||
|
||||
|
||||
class AsyncIOHandlerMixin(HandlerMixin):
|
||||
"""
|
||||
Mix this in over a ``RequestHandler`` for a correlating header for use
|
||||
with AsyncIO when using ``async def`` and ``await`` style asynchronous
|
||||
request handlers.
|
||||
|
||||
:keyword str correlation_header: the name of the header to use
|
||||
for correlation. If this keyword is omitted, then the header
|
||||
is named ``Correlation-ID``.
|
||||
|
||||
This mix-in ensures that responses include a header that correlates
|
||||
requests and responses. If there header is set on the incoming
|
||||
request, then it will be copied to the outgoing response. Otherwise,
|
||||
a new UUIDv4 will be generated and inserted. The value can be
|
||||
examined or modified via the ``correlation_id`` property.
|
||||
|
||||
The MRO needs to contain something that resembles a standard
|
||||
:class:`tornado.web.RequestHandler`. Specifically, we need the
|
||||
following things to be available:
|
||||
|
||||
- :meth:`~tornado.web.RequestHandler.prepare` needs to be called
|
||||
appropriately
|
||||
- :meth:`~tornado.web.RequestHandler.set_header` needs to exist in
|
||||
the MRO and it needs to overwrite the header value
|
||||
- :meth:`~tornado.web.RequestHandler.set_default_headers` should be
|
||||
called to establish the default header values
|
||||
- ``self.request`` is a object that has a ``headers`` property that
|
||||
contains the request headers as a ``dict``.
|
||||
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
# correlation_id is used from within set_default_headers
|
||||
# which is called from within super().__init__() so we need
|
||||
# to make sure that it is set *BEFORE* we call super.
|
||||
self.__header_name = kwargs.pop(
|
||||
'correlation_header', 'Correlation-ID')
|
||||
self.__correlation_id = str(uuid.uuid4())
|
||||
super(AsyncIOHandlerMixin, self).__init__(*args, **kwargs)
|
||||
|
||||
async def prepare(self):
|
||||
# Here we want to copy an incoming Correlation-ID header if
|
||||
# one exists. We also want to set it in the outgoing response
|
||||
# which the property setter does for us.
|
||||
maybe_future = super(HandlerMixin, self).prepare()
|
||||
if concurrent.is_future(maybe_future):
|
||||
await maybe_future
|
||||
|
||||
correlation_id = self.get_request_header(self.__header_name, None)
|
||||
if correlation_id is not None:
|
||||
self.correlation_id = correlation_id
|
||||
|
||||
|
||||
def correlation_id_logger(handler):
|
||||
""" Custom Tornado access log writer that appends correlation-id.
|
||||
|
||||
|
|
37
tests.py
37
tests.py
|
@ -1,7 +1,7 @@
|
|||
import uuid
|
||||
import unittest
|
||||
|
||||
from tornado import testing, version_info, web
|
||||
from tornado import testing, web
|
||||
|
||||
from sprockets.mixins import correlation
|
||||
|
||||
|
@ -15,16 +15,6 @@ class CorrelatedRequestHandler(correlation.HandlerMixin, web.RequestHandler):
|
|||
self.write('status {0}'.format(status_code))
|
||||
|
||||
|
||||
class AsyncIOCorrelatedRequestHandler(correlation.AsyncIOHandlerMixin,
|
||||
web.RequestHandler):
|
||||
|
||||
def get(self, status_code):
|
||||
status_code = int(status_code)
|
||||
if status_code >= 300:
|
||||
raise web.HTTPError(status_code)
|
||||
self.write('status {0}'.format(status_code))
|
||||
|
||||
|
||||
class CorrelationMixinTests(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
|
@ -45,28 +35,3 @@ class CorrelationMixinTests(testing.AsyncHTTPTestCase):
|
|||
response = self.fetch('/status/500',
|
||||
headers={'Correlation-Id': correlation_id})
|
||||
self.assertEqual(response.headers['correlation-id'], correlation_id)
|
||||
|
||||
|
||||
class AsyncIOCorrelationMixinTests(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
return web.Application([
|
||||
(r'/status/(?P<status_code>\d+)', AsyncIOCorrelatedRequestHandler),
|
||||
])
|
||||
|
||||
@unittest.skipIf(version_info < (4,3,0,0), 'tornado < 4.3')
|
||||
def test_that_correlation_id_is_returned_when_successful(self):
|
||||
response = self.fetch('/status/200')
|
||||
self.assertIsNotNone(response.headers.get('Correlation-ID'))
|
||||
|
||||
@unittest.skipIf(version_info < (4,3,0,0), 'tornado < 4.3')
|
||||
def test_that_correlation_id_is_returned_in_error(self):
|
||||
response = self.fetch('/status/500')
|
||||
self.assertIsNotNone(response.headers.get('Correlation-ID'))
|
||||
|
||||
@unittest.skipIf(version_info < (4,3,0,0), 'tornado < 4.3')
|
||||
def test_that_correlation_id_is_copied_from_request(self):
|
||||
correlation_id = uuid.uuid4().hex
|
||||
response = self.fetch('/status/500',
|
||||
headers={'Correlation-Id': correlation_id})
|
||||
self.assertEqual(response.headers['correlation-id'], correlation_id)
|
||||
|
|
7
tox.ini
7
tox.ini
|
@ -1,5 +1,5 @@
|
|||
[tox]
|
||||
envlist = py35,py36,py37,pypy3,tornado42,tornado43,torando51
|
||||
envlist = py35,py36,py37,pypy3,tornado43,torando51
|
||||
toxworkdir = {toxinidir}/build/tox
|
||||
skip_missing_intepreters = true
|
||||
|
||||
|
@ -9,11 +9,6 @@ deps =
|
|||
tornado
|
||||
commands = {envbindir}/nosetests
|
||||
|
||||
[testenv:tornado42]
|
||||
deps =
|
||||
-rtest-requirements.txt
|
||||
tornado>=4.2,<4.3
|
||||
|
||||
[testenv:tornado43]
|
||||
deps =
|
||||
-rtest-requirements.txt
|
||||
|
|
Loading…
Reference in a new issue