mirror of
https://github.com/sprockets/sprockets.mixins.avro-publisher.git
synced 2024-11-29 03:00:26 +00:00
Merge pull request #2 from sprockets/add-install
Add installation method
This commit is contained in:
commit
71a27c50cd
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
|
Requirements
|
||||||
------------
|
------------
|
||||||
- sprockets.mixins.amqp>=0.1.1
|
- sprockets.mixins.amqp>=1.0.0
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
|
@ -34,6 +34,16 @@ This examples demonstrates the most basic usage of ``sprockets.mixins.avro-publi
|
||||||
from tornado import web
|
from tornado import web
|
||||||
from sprockets.mixins import avro_publisher
|
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):
|
class RequestHandler(avro_publisher.AvroPublishingMixin, web.RequestHandler):
|
||||||
|
|
||||||
@gen.coroutine
|
@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,
|
{'content_type': avro_publisher.DATUM_MIME_TYPE,
|
||||||
'type': 'avro-schema-name'})
|
'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__":
|
if __name__ == "__main__":
|
||||||
|
application = make_app()
|
||||||
application.listen(8888)
|
application.listen(8888)
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
ioloop.IOLoop.current().start()
|
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
|
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
|
avro-python3>=1.7.7,<2
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -9,7 +9,7 @@ with open(requires) as handle:
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name='sprockets.mixins.avro-publisher',
|
name='sprockets.mixins.avro-publisher',
|
||||||
version='0.1.1',
|
version='1.0.0',
|
||||||
description='Mixin for publishing events to RabbitMQ as avro datums',
|
description='Mixin for publishing events to RabbitMQ as avro datums',
|
||||||
long_description=open('README.rst').read(),
|
long_description=open('README.rst').read(),
|
||||||
url='https://github.com/sprockets/sprockets.mixins.avro-publisher',
|
url='https://github.com/sprockets/sprockets.mixins.avro-publisher',
|
||||||
|
|
|
@ -28,7 +28,7 @@ from tornado import httpclient
|
||||||
import avro.io
|
import avro.io
|
||||||
import avro.schema
|
import avro.schema
|
||||||
|
|
||||||
version_info = (0, 1, 0)
|
version_info = (1, 0, 0)
|
||||||
__version__ = '.'.join(str(v) for v in version_info)
|
__version__ = '.'.join(str(v) for v in version_info)
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -52,17 +52,6 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
||||||
DATUM_MIME_TYPE = 'application/vnd.apache.avro.datum'
|
DATUM_MIME_TYPE = 'application/vnd.apache.avro.datum'
|
||||||
DEFAULT_SCHEMA_URI_FORMAT = 'http://localhost/avro/%(name)s.avsc'
|
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
|
@gen.coroutine
|
||||||
def amqp_publish(self, exchange, routing_key, body, properties):
|
def amqp_publish(self, exchange, routing_key, body, properties):
|
||||||
"""Publish the message to RabbitMQ
|
"""Publish the message to RabbitMQ
|
||||||
|
@ -73,8 +62,8 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
||||||
:param dict properties: The message properties
|
:param dict properties: The message properties
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if ('content_type' in properties and
|
if (('content_type' in properties and
|
||||||
properties['content_type']) == self.DATUM_MIME_TYPE:
|
properties['content_type']) == self.DATUM_MIME_TYPE):
|
||||||
body = yield self._avro_serialize(properties['type'], body)
|
body = yield self._avro_serialize(properties['type'], body)
|
||||||
yield self.application.amqp.publish(exchange, routing_key, body,
|
yield self.application.amqp.publish(exchange, routing_key, body,
|
||||||
properties)
|
properties)
|
||||||
|
@ -146,3 +135,14 @@ class AvroPublishingMixin(amqp.PublishingMixin):
|
||||||
except avro.io.AvroTypeException as error:
|
except avro.io.AvroTypeException as error:
|
||||||
raise ValueError(error)
|
raise ValueError(error)
|
||||||
raise gen.Return(bytes_io.getvalue())
|
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