Library for writting simple AMQP handlers with type hints and pydantic support
Project description
Cornflower
Library for writing RabbitMQ message handlers with pydantic validation.
Example use:
Handler callback can accept zero or one argument of pydantic.BaseModel
type. Message body will be validated automatically against specified schema.
from cornflower import MessageQueue, OutputMessage, MessageDeliveryMode
from pydantic import BaseModel
class UserMessage(BaseModel):
username: str
message: str
queue = MessageQueue(url="amqp://user:password@host:port")
@queue.listen(routing_key="user.registered")
def handle_user_register(message: UserMessage) -> None:
# do something with validated message
...
@queue.listen(routing_key="user.login")
def handle_user_login() -> None:
# callback with no arguments, handle message without
# validating its body
# sending message
user_message = UserMessage(username="example", message="this is example")
queue.dispatch(
message=OutputMessage(
body=user_message.dict(),
routing_key="user.logout",
delivery_mode=MessageDeliveryMode.PERSISTENT,
)
)
if __name__ == "__main__":
queue.run()
Optional configuration
from cornflower import MessageQueue
from cornflower.options import QueueOptions, TransportOptions, ConsumerOptions
queue = MessageQueue(
queue_options=QueueOptions(
durable=True,
exclusive=False,
auto_delete=False,
),
consumer_options=ConsumerOptions(
prefetch_count=10
),
transport_options=TransportOptions(
confirm_publish=True,
)
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cornflower-0.1.0.tar.gz
(4.8 kB
view hashes)
Built Distribution
Close
Hashes for cornflower-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d83ca17b1f4cec8aa8d32a25ac5daed4a4c5907440676ca838247447db313699 |
|
MD5 | 4467c116ce5d58056a7947983382be5e |
|
BLAKE2b-256 | 093fd7343c6028093b12c4cf978b49d7c28249cf45d17e45eec78c47878fd125 |