Skip to main content

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


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 details)

Uploaded Source

Built Distribution

cornflower-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file cornflower-0.1.0.tar.gz.

File metadata

  • Download URL: cornflower-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.0 Windows/10

File hashes

Hashes for cornflower-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5144c8fbe29fba7925f42fdd9ba449565709ba3121016691312fe7dea3c462af
MD5 91a302bd214246a671a46119d1345db8
BLAKE2b-256 fb3a29147ec2691709288225535a19db206523a05894f21cf49afafbc4b22ce6

See more details on using hashes here.

File details

Details for the file cornflower-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cornflower-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.0 Windows/10

File hashes

Hashes for cornflower-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d83ca17b1f4cec8aa8d32a25ac5daed4a4c5907440676ca838247447db313699
MD5 4467c116ce5d58056a7947983382be5e
BLAKE2b-256 093fd7343c6028093b12c4cf978b49d7c28249cf45d17e45eec78c47878fd125

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page