mirror of
https://github.com/sprockets/sprockets.mixins.avro-publisher.git
synced 2024-11-28 19:29:55 +00:00
Add installation method
Move application initializations out of initialize and into ``sprockets.mixins.avro_publisher.install`` Add sprockets.mixins.amqp.install to setup amqp for application Bump version to 1.0.0
This commit is contained in:
parent
53472d9af1
commit
39f2b5cc6c
5 changed files with 29 additions and 24 deletions
19
README.rst
19
README.rst
|
@ -16,7 +16,7 @@ and can be installed via ``pip`` or ``easy_install``:
|
|||
|
||||
Requirements
|
||||
------------
|
||||
- sprockets.mixins.amqp>=0.1.1
|
||||
- sprockets.mixins.amqp>=1.0.0
|
||||
|
||||
Example
|
||||
-------
|
||||
|
@ -34,6 +34,16 @@ This examples demonstrates the most basic usage of ``sprockets.mixins.avro-publi
|
|||
from tornado import web
|
||||
from sprockets.mixins import avro_publisher
|
||||
|
||||
def make_app(**settings):
|
||||
settings = {'avro_schema_uri_format': 'http://my-schema-repository/%(name)s.avsc'}
|
||||
application = web.Application(
|
||||
[
|
||||
web.url(r'/', RequestHandler),
|
||||
], **settings)
|
||||
|
||||
avro_publisher.install(application)
|
||||
return application
|
||||
|
||||
class RequestHandler(avro_publisher.AvroPublishingMixin, web.RequestHandler):
|
||||
|
||||
@gen.coroutine
|
||||
|
@ -43,13 +53,8 @@ This examples demonstrates the most basic usage of ``sprockets.mixins.avro-publi
|
|||
{'content_type': avro_publisher.DATUM_MIME_TYPE,
|
||||
'type': 'avro-schema-name'})
|
||||
|
||||
settings = {'avro_schema_uri_format': 'http://my-schema-repository/%(name)s.avsc'}
|
||||
application = web.Application([(r"/", RequestHandler),],
|
||||
debug=True,
|
||||
**settings)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
application = make_app()
|
||||
application.listen(8888)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
ioloop.IOLoop.current().start()
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
sprockets.mixins.amqp>=0.1.1,<1
|
||||
sprockets.mixins.amqp>=1.0.0,<2
|
||||
avro>=1.7.7,<2
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
sprockets.mixins.amqp>=0.1.1,<1
|
||||
sprockets.mixins.amqp>=1.0.0,<2
|
||||
avro-python3>=1.7.7,<2
|
||||
|
|
2
setup.py
2
setup.py
|
@ -9,7 +9,7 @@ with open(requires) as handle:
|
|||
|
||||
setuptools.setup(
|
||||
name='sprockets.mixins.avro-publisher',
|
||||
version='0.1.1',
|
||||
version='1.0.0',
|
||||
description='Mixin for publishing events to RabbitMQ as avro datums',
|
||||
long_description=open('README.rst').read(),
|
||||
url='https://github.com/sprockets/sprockets.mixins.avro-publisher',
|
||||
|
|
|
@ -28,7 +28,7 @@ from tornado import httpclient
|
|||
import avro.io
|
||||
import avro.schema
|
||||
|
||||
version_info = (0, 1, 0)
|
||||
version_info = (1, 0, 0)
|
||||
__version__ = '.'.join(str(v) for v in version_info)
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
@ -52,17 +52,6 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
|||
DATUM_MIME_TYPE = 'application/vnd.apache.avro.datum'
|
||||
DEFAULT_SCHEMA_URI_FORMAT = 'http://localhost/avro/%(name)s.avsc'
|
||||
|
||||
def initialize(self):
|
||||
"""Initialize the RequestHandler ensuring there is an a dict for
|
||||
caching avro schemas.
|
||||
|
||||
"""
|
||||
super(AvroPublishingMixin, self).initialize()
|
||||
if 'avro_schema_uri_format' not in self.application.settings:
|
||||
LOGGER.warning('avro_schema_uri_format is not set, using default')
|
||||
if not hasattr(self.application, 'avro'):
|
||||
self.application.avro = {}
|
||||
|
||||
@gen.coroutine
|
||||
def amqp_publish(self, exchange, routing_key, body, properties):
|
||||
"""Publish the message to RabbitMQ
|
||||
|
@ -73,8 +62,8 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
|||
:param dict properties: The message properties
|
||||
|
||||
"""
|
||||
if ('content_type' in properties and
|
||||
properties['content_type']) == self.DATUM_MIME_TYPE:
|
||||
if (('content_type' in properties and
|
||||
properties['content_type']) == self.DATUM_MIME_TYPE):
|
||||
body = yield self._avro_serialize(properties['type'], body)
|
||||
yield self.application.amqp.publish(exchange, routing_key, body,
|
||||
properties)
|
||||
|
@ -146,3 +135,14 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
|||
except avro.io.AvroTypeException as error:
|
||||
raise ValueError(error)
|
||||
raise gen.Return(bytes_io.getvalue())
|
||||
|
||||
|
||||
def install(application, **kwargs):
|
||||
"""Call this to install avro publishing for the Tornado application."""
|
||||
amqp.install(application, **kwargs)
|
||||
|
||||
if 'avro_schema_uri_format' not in application.settings:
|
||||
LOGGER.warning('avro_schema_uri_format is not set, using default')
|
||||
|
||||
setattr(application, 'avro', {})
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue