Skip to main content

Lightweight handler routing library for modular apps.

Project description

Bazario

Bazario is a lightweight, expressive library for routing requests and notifications to the right handlers with ease. Designed for modular applications, it simplifies your codebase while staying powerful and flexible.

📚 Documentation


🚀 Quick Start

Installation

Install Bazario with pip:

pip install bazario

Define Your Logic

1. Create a Request and its Handler

Define what you want to do with a Request and specify how it’s handled:

# application/requests/create_user.py
from bazario import Request, RequestHandler

class CreateUserRequest(Request[str]):
    def __init__(self, username: str):
        self.username = username

class CreateUserHandler(RequestHandler[CreateUserRequest, str]):
    def handle(self, request: CreateUserRequest) -> str:
        return f"User {request.username} created!"

2. Create a Notification and its Handler

Use Notification to broadcast events:

# application/notifications/user_created.py
from bazario import Notification, NotificationHandler

class UserCreatedNotification(Notification):
    def __init__(self, user_id: str):
        self.user_id = user_id

class UserCreatedHandler(NotificationHandler[UserCreatedNotification]):
    def handle(self, notification: UserCreatedNotification) -> None:
        print(f"Notification: User {notification.user_id} was created.")

Bring It All Together

Set up your Dispatcher to handle requests and notifications seamlessly:

# bootstap/ioc.py
from bazario import Dispatcher, Resolver, Sender, Publisher
from bazario.resolvers.your_di_framework import YourDIFrameworkResolver

from your_di_framework import Container

def provide_registry() -> Registry:
    registry = Registry()
    registry.add_request_handler(CreateUserRequest, CreateUserHandler)
    registry.add_notification_handlers(UserCreatedNotification, UserCreatedHandler)
    return registry


def provide_resolver(container: Container) -> Resolver:
    return YourDIFrameworkResolver(container)


def provide_dispatcher(registry: Registry, resolver: Resolver) -> Dispatcher:
    return Dispatcher(resolver, registry)


def provide_container() -> Container:
    container = Container()
    container.register(Registry, provide_registry)
    container.register(Resolver, provide_resolver)
    container.register(Dispatcher, provide_dispatcher)
    container.register_aliases(Dispatcher, aliases=[Sender, Publisher])
    return container


# presentation/rest/users.py
from your_framework import post
from your_di_framework import inject
from bazario import Sender, Publisher

from application.requests.create_user import CreateUserRequest
from application.notifications.user_created import UserCreatedNotification

@post("/")
@inject
def create_user(sender: Sender, publisher: Publisher) -> None:
    response = sender.send(CreateUserRequest("john_doe"))
    print(response)  # Output: User john_doe created!
    publisher.publish(UserCreatedNotification("123"))  # Output: Notification: User 123 was created.

✨ Key Features

  • Request Handling: A streamlined mechanism for handling requests with clear separation of responsibilities.
  • Event Handling: Unified event publication and handling (Notifications) supporting both standard and async/await syntax while maintaining efficient in-memory processing.
  • Modular Architecture: Clear separation of business logic, ports, and infrastructure, simplifying development and maintenance.
  • IoC Container Integration: Support for DI frameworks enabling easy dependency management and modular configuration.
  • Testability: Use of abstractions to easily mock infrastructure adapters for unit testing.
  • Asynchronous Support: The bazario.asyncio package enables asynchronous handling, providing flexibility for applications requiring async logic.
  • Dependency Separation: Controllers delegate handler resolution to Bazario, focusing solely on request parsing. This improves separation of responsibilities and enhances code maintainability.
  • Pipeline Behaviors: Flexible middleware system for implementing cross-cutting concerns like logging, validation, and error handling without modifying handler logic.
  • Configurable Processing Chain: Ability to create custom processing pipelines for both requests and notifications, enabling sophisticated pre- and post-processing workflows.

Bazario is optimized for synchronous in-memory processing and handler routing, making it ideal for applications requiring modularity, simplicity, and flexible handler management.


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

bazario-0.4.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

bazario-0.4.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file bazario-0.4.1.tar.gz.

File metadata

  • Download URL: bazario-0.4.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for bazario-0.4.1.tar.gz
Algorithm Hash digest
SHA256 4497888312ada98fd3c8b155c5e03821982119137c706cb0d8849fa089c15676
MD5 30fc6def8a9bafa90c113e7a8757be40
BLAKE2b-256 a82135d316ac4f02ab1e6fbb09e5e8bffde717c3c8d0018622e1199bb1c6a9b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for bazario-0.4.1.tar.gz:

Publisher: publishing.yml on chessenjoyer17/bazario

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

File details

Details for the file bazario-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: bazario-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for bazario-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6aee20daebcc4cdde4110aa584337c4b851a882d98ba8ae4aca5cb4c301b0067
MD5 8de97b7bf60e69858ea97952ead27d87
BLAKE2b-256 6c17c39d0f6dc7e4e403cf911746990c9ff2fd5ab2eadf2f46f6730f618e6196

See more details on using hashes here.

Provenance

The following attestation bundles were made for bazario-0.4.1-py3-none-any.whl:

Publisher: publishing.yml on chessenjoyer17/bazario

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