Flask extension for BunnyStream - Easy RabbitMQ integration with automatic connection management and event routing.
Project description
Flask-BunnyStream
A Flask extension for integrating BunnyStream messaging system with Flask applications, providing seamless event-driven architecture support.
Table of Contents
- Features
- Quick Start
- Examples
- Configuration
- API Reference
- Development
- Architecture
- Contributing
- Support
- License
- Dependencies
- Changelog
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_handlerdecorator 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 apppublish(*args, **kwargs)- Publish message to RabbitMQis_initialized- Check if extension is initializedwarren- 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
- Fork the repository on GitHub
- Clone your fork locally
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes and add tests
- Run the test suite (
pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to your branch (
git push origin feature/amazing-feature) - 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
- ๐ Documentation: Check our comprehensive examples and guides
- ๐ Bug Reports: Use our bug report template
- ๐ก Feature Requests: Use our feature request template
- โ Questions: Use our question template
- ๐ฌ Discussions: Join our GitHub Discussions
Resources
- Extension Best Practices - Detailed extension usage patterns
- Decorators Documentation - Event handler system documentation
- Examples Directory - Comprehensive usage examples
- Contributing Guide - How to contribute to the project
- BunnyStream Documentation - Underlying messaging system
Community
- GitHub Issues: Report bugs and request features
- GitHub Discussions: Community discussions and Q&A
- Pull Requests: Contribute code improvements
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ff2b05cfeec8eafd1416db8c2d5d08972ad8f0f0f0223d8eedaa2e8b11b1170
|
|
| MD5 |
484a7dff65c3eda8b1dcfd0cc2f3631a
|
|
| BLAKE2b-256 |
cb0e326230d6653d1393cb736567c7d23fc9656aaaabdf6d2dd4d3ccb698c8c0
|
Provenance
The following attestation bundles were made for flask_bunnystream-1.0.0.tar.gz:
Publisher:
publish.yml on MarcFord/flask-bunnystream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_bunnystream-1.0.0.tar.gz -
Subject digest:
0ff2b05cfeec8eafd1416db8c2d5d08972ad8f0f0f0223d8eedaa2e8b11b1170 - Sigstore transparency entry: 250719733
- Sigstore integration time:
-
Permalink:
MarcFord/flask-bunnystream@3665c72274b7e08565b768d6da352911156b1047 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MarcFord
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3665c72274b7e08565b768d6da352911156b1047 -
Trigger Event:
push
-
Statement type:
File details
Details for the file flask_bunnystream-1.0.0-py3-none-any.whl.
File metadata
- Download URL: flask_bunnystream-1.0.0-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e214420508ed30108295f5dde9f0f3597d6a5614949bb0396ed5dbc4897e804
|
|
| MD5 |
3cd670f8d4ea3a000db8bba9e7ca6185
|
|
| BLAKE2b-256 |
8fc4f0ecf156a0d48ff64abc47cc6105a45da9caa1cf1c44407895586c283fd6
|
Provenance
The following attestation bundles were made for flask_bunnystream-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on MarcFord/flask-bunnystream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_bunnystream-1.0.0-py3-none-any.whl -
Subject digest:
5e214420508ed30108295f5dde9f0f3597d6a5614949bb0396ed5dbc4897e804 - Sigstore transparency entry: 250719745
- Sigstore integration time:
-
Permalink:
MarcFord/flask-bunnystream@3665c72274b7e08565b768d6da352911156b1047 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/MarcFord
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3665c72274b7e08565b768d6da352911156b1047 -
Trigger Event:
push
-
Statement type: