Skip to main content

Flask extension for BunnyStream - Easy RabbitMQ integration with automatic connection management and event routing.

Project description

Flask-BunnyStream

CI PyPI version Python versions License: GPL v3 Code style: black Codecov

A Flask extension for integrating BunnyStream messaging system with Flask applications, providing seamless event-driven architecture support.

Table of Contents

Features

  • Flask Extension Pattern: Follows Flask extension best practices with lazy and immediate initialization
  • Event Handler Decorators: Decorator-based system for registering event handlers
  • Application Context Integration: Seamless integration with Flask application context
  • Error Handling: Robust error handling and graceful degradation
  • Type Safety: Full type annotations for better development experience
  • Production Ready: Comprehensive testing and documentation

Quick Start

Installation

# Install from PyPI
pip install flask-bunnystream

# Or install from source
git clone https://github.com/MarcFord/flask-bunnystream.git
cd flask-bunnystream
pip install -e .

Basic Usage

from flask import Flask
from flask_bunnystream import BunnyStream

app = Flask(__name__)

# Configure BunnyStream
app.config['BUNNYSTREAM_MODE'] = 'producer'
app.config['BUNNYSTREAM_EXCHANGE'] = 'my_exchange'
app.config['BUNNYSTREAM_HOST'] = 'localhost'

# Initialize extension
bunnystream = BunnyStream(app)

@app.route('/publish')
def publish_message():
    bunnystream.publish('test.message', {'hello': 'world'})
    return 'Message published!'

Event Handlers

from flask_bunnystream import event_handler, user_event

@event_handler('notification.send')
def handle_notification(event_data):
    print(f"Sending notification: {event_data['message']}")

@user_event('created')
def handle_user_created(event_data):
    print(f"New user: {event_data['user_id']}")

Examples

The examples/ directory contains comprehensive examples for different use cases:

๐Ÿš€ Basic Usage

Learn the fundamental extension usage patterns:

  • Direct and lazy initialization methods
  • Flask config vs explicit configuration
  • Application context usage patterns
  • Basic message publishing

๐ŸŽฏ Event Handlers

Understand the decorator-based event handling system:

  • @event_handler decorator usage
  • Specialized decorators (@user_event, @order_event, @system_event)
  • Event handler registration and management
  • Error handling and isolation

๐Ÿ—๏ธ Full Application

Complete production-ready example:

  • Flask web application with REST API
  • SQLAlchemy database integration
  • Separate event consumer/worker process
  • Docker and Docker Compose setup
  • Production deployment patterns

Configuration

The extension supports configuration through Flask config:

Config Key Description Default
BUNNYSTREAM_MODE Operation mode: 'producer' or 'consumer' 'producer'
BUNNYSTREAM_EXCHANGE RabbitMQ exchange name Required
BUNNYSTREAM_HOST RabbitMQ host 'localhost'
BUNNYSTREAM_PORT RabbitMQ port 5672
BUNNYSTREAM_VHOST RabbitMQ virtual host '/'
BUNNYSTREAM_USER RabbitMQ username 'guest'
BUNNYSTREAM_PASSWORD RabbitMQ password 'guest'

API Reference

Extension Class

BunnyStream(app=None, config=None)

Main Flask extension class.

Methods:

  • init_app(app, config=None) - Initialize with Flask app
  • publish(*args, **kwargs) - Publish message to RabbitMQ
  • is_initialized - Check if extension is initialized
  • warren - Access underlying BunnyStream Warren instance

Event Decorators

@event_handler(event_name, queue_name=None)

Register a function as an event handler.

@user_event(action, queue_name=None)

Convenience decorator for user events (user.{action}).

@order_event(action, queue_name=None)

Convenience decorator for order events (order.{action}).

@system_event(action, queue_name=None)

Convenience decorator for system events (system.{action}).

Utility Functions

get_bunnystream()

Get BunnyStream extension from application context.

register_pending_handlers(app)

Register handlers that were decorated before app initialization.

Development

Prerequisites

  • Python 3.8+
  • RabbitMQ server
  • Flask 2.0+
  • BunnyStream 1.0.5+

Setup Development Environment

# Clone repository
git clone <repository-url>
cd flask-bunnystream

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install in development mode
pip install -e .

# Install development dependencies
pip install pytest pytest-mock pytest-cov

Running Tests

# Run all tests
python -m pytest

# Run with coverage
python -m pytest --cov=flask_bunnystream

# Run specific test file
python -m pytest tests/test_extension.py -v

Testing with RabbitMQ

# Start RabbitMQ using Docker
docker run -d --name rabbitmq \
  -p 5672:5672 -p 15672:15672 \
  rabbitmq:3.12-management

# RabbitMQ Management UI: http://localhost:15672 (guest/guest)

Architecture

The extension follows Flask extension patterns and integrates with BunnyStream:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Flask App     โ”‚โ”€โ”€โ”€โ–ถโ”‚  BunnyStream    โ”‚โ”€โ”€โ”€โ–ถโ”‚   RabbitMQ      โ”‚
โ”‚                 โ”‚    โ”‚  Extension      โ”‚    โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                        โ”‚                        โ”‚
         โ”‚                        โ”‚                        โ”‚
         โ–ผ                        โ–ผ                        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Event Handlers  โ”‚    โ”‚     Warren      โ”‚    โ”‚   Consumers     โ”‚
โ”‚   (Decorators)  โ”‚    โ”‚   (BunnyStream) โ”‚    โ”‚   (Workers)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Contributing

We welcome contributions! Please see our Contributing Guide for details on how to get started.

Quick Contributing Steps

  1. Fork the repository on GitHub
  2. Clone your fork locally
  3. Create a feature branch (git checkout -b feature/amazing-feature)
  4. Make your changes and add tests
  5. Run the test suite (pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to your branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Development Setup

# Clone your fork
git clone https://github.com/YOUR_USERNAME/flask-bunnystream.git
cd flask-bunnystream

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install in development mode
pip install -e .
pip install -r requirements-dev.txt

# Start RabbitMQ for testing
docker run -d --name rabbitmq-dev \
  -p 5672:5672 -p 15672:15672 \
  rabbitmq:3.12-management

Code Quality Standards

We maintain high code quality standards:

  • Code Formatting: Black
  • Security: Bandit, Safety
  • Testing: Pytest with coverage
# Check code quality
black --check src/ tests/ examples/

# Run tests
pytest --cov=flask_bunnystream

Support

Getting Help

Resources

Community

Supported Versions

Version Python Support Flask Support Status
1.x 3.8 - 3.12 2.0+ โœ… Active

License

This project is licensed under the GNU General Public License v3.0 or later (GPL-3.0-or-later) - see the LICENSE file for details.

GPL v3 License Summary

  • โœ… Commercial use - Use in commercial projects
  • โœ… Modification - Modify the source code
  • โœ… Distribution - Distribute the software
  • โœ… Private use - Use privately
  • โœ… Patent use - Use patents from contributors
  • โš ๏ธ Disclose source - Must provide source code when distributing
  • โš ๏ธ License and copyright notice - Must include license and copyright notice
  • โš ๏ธ Same license - Derivative works must use the same license
  • โš ๏ธ State changes - Must indicate changes made to the code
  • โŒ Liability - Authors not liable for damages
  • โŒ Warranty - No warranty provided

Third-Party Licenses

This project depends on:

  • Flask (BSD-3-Clause License)
  • BunnyStream (MIT License)
  • RabbitMQ (Mozilla Public License 2.0)

Dependencies

Core Dependencies

  • Flask - Web framework (โ‰ฅ2.0.0)
  • BunnyStream - Messaging system (โ‰ฅ1.0.5)

Runtime Requirements

  • Python 3.8 - 3.12
  • RabbitMQ server (any recent version)

Development Dependencies

See requirements-dev.txt for a complete list of development dependencies.

Changelog

v1.0.0 (Current)

๐ŸŽ‰ Initial Release

  • โœ… Flask extension with lazy and immediate initialization
  • โœ… Event handler decorator system (@event_handler, @user_event, etc.)
  • โœ… Comprehensive examples and documentation
  • โœ… Production-ready with full test coverage (77 tests)
  • โœ… Docker and Docker Compose support
  • โœ… Type safety with full type annotations
  • โœ… CI/CD with GitHub Actions
  • โœ… PyPI publishing automation

๐Ÿ”ง Technical Features

  • Extension Pattern: Follows Flask extension best practices
  • Error Handling: Robust error handling and graceful degradation
  • Application Context: Seamless integration with Flask application context
  • Configuration: Flexible configuration via Flask config or explicit config
  • Testing: Comprehensive test suite with integration tests

๐Ÿ“ฆ Package Features

  • Multiple Examples: Basic usage, event handlers, and full application
  • Documentation: Detailed guides and API reference
  • Code Quality: Black, Bandit, and Safety integration
  • CI/CD: Automated testing and publishing to PyPI

For detailed changes and migration guides, see our GitHub Releases.


Star โญ this repository if you find it useful!

Made with โค๏ธ by Marc Ford

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

flask_bunnystream-1.0.0.tar.gz (74.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_bunnystream-1.0.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file flask_bunnystream-1.0.0.tar.gz.

File metadata

  • Download URL: flask_bunnystream-1.0.0.tar.gz
  • Upload date:
  • Size: 74.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flask_bunnystream-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0ff2b05cfeec8eafd1416db8c2d5d08972ad8f0f0f0223d8eedaa2e8b11b1170
MD5 484a7dff65c3eda8b1dcfd0cc2f3631a
BLAKE2b-256 cb0e326230d6653d1393cb736567c7d23fc9656aaaabdf6d2dd4d3ccb698c8c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_bunnystream-1.0.0.tar.gz:

Publisher: publish.yml on MarcFord/flask-bunnystream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_bunnystream-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_bunnystream-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e214420508ed30108295f5dde9f0f3597d6a5614949bb0396ed5dbc4897e804
MD5 3cd670f8d4ea3a000db8bba9e7ca6185
BLAKE2b-256 8fc4f0ecf156a0d48ff64abc47cc6105a45da9caa1cf1c44407895586c283fd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_bunnystream-1.0.0-py3-none-any.whl:

Publisher: publish.yml on MarcFord/flask-bunnystream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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