Skip to main content

gridflow-python-mediator

Generic mediator pattern implementation with zero dependencies and pipeline behavior support.

Extracted from: GridFlow backend/src/apps/token_generator/application/common/mediator/

Installation

# Core (zero dependencies)
pip install gridflow-python-mediator

# With logging support
pip install gridflow-python-mediator[logging]

Features

  • Zero Dependencies: Pure Python implementation
  • Pipeline Behaviors: Logging, timing, validation
  • Type-Safe: Full generic type support
  • Async/Sync: Supports both async and sync handlers

Usage

Basic Mediator

from gridflow_python_mediator import Mediator

# Define request and handler
class GetUserRequest:
    def __init__(self, user_id: int):
        self.user_id = user_id

class GetUserHandler:
    async def handle(self, request: GetUserRequest):
        # Fetch user logic
        return {"id": request.user_id, "name": "John"}

# Setup mediator
mediator = Mediator()
mediator.register_handler(GetUserRequest, GetUserHandler())

# Send request
request = GetUserRequest(user_id=1)
result = await mediator.send(request)

With CQRS

from python_cqrs_core import BaseCommand, ICommandHandler
from gridflow_python_mediator import Mediator

class CreateUserCommand(BaseCommand):
    name: str
    email: str

class CreateUserHandler(ICommandHandler[CreateUserCommand, int]):
    async def handle(self, command: CreateUserCommand) -> int:
        # Create user
        return user_id

mediator = Mediator()
mediator.register_handler(CreateUserCommand, CreateUserHandler())

cmd = CreateUserCommand(name="John", email="john@example.com")
user_id = await mediator.send(cmd)

Pipeline Behaviors

from gridflow_python_mediator import (
    Mediator,
    LoggingBehavior,
    TimingBehavior,
    ValidationBehavior
)

mediator = Mediator()

# Add behaviors (executed in order)
mediator.add_pipeline_behavior(LoggingBehavior().handle)
mediator.add_pipeline_behavior(TimingBehavior().handle)
mediator.add_pipeline_behavior(ValidationBehavior().handle)

# Behaviors run before handler
result = await mediator.send(request)

Custom Behavior

async def auth_behavior(request, handler):
    """Check authentication before handler."""
    if not request.user_id:
        raise ValueError("Unauthorized")
    return None  # Continue to handler

mediator.add_pipeline_behavior(auth_behavior)

API Reference

Mediator

Generic request/response dispatcher.

Methods:

register_handler(request_type, handler)

Register handler for request type.

Parameters:

  • request_type (Type): Request class
  • handler (Any): Handler instance with handle() method

async send(request) -> TResult

Dispatch request to handler.

Parameters:

  • request (TRequest): Request to send

Returns:

  • Result from handler

add_pipeline_behavior(behavior)

Add pipeline behavior.

Parameters:

  • behavior (Callable): Async function(request, handler) -> Optional[result]

Built-in Behaviors

LoggingBehavior

Logs request type before/after handler.

Requires: structlog>=23.0.0

TimingBehavior

Measures and logs execution duration.

Requires: structlog>=23.0.0

ValidationBehavior

Validates Pydantic models before handler.

Design Patterns

Mediator Pattern

Decouples senders from receivers:

  • Single point of dispatch
  • Handler registration
  • Pipeline behaviors

Pipeline Pattern

Behaviors execute in order:

  1. Logging
  2. Timing
  3. Validation
  4. Handler

Dependencies

  • Core: None (zero dependencies!)
  • Optional: structlog>=23.0.0 (for logging behaviors)

License

MIT

Metadata

Release files for gridflow-python-mediator 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gridflow-python-mediator 0.1.1
File Size Uploaded Provenance Actions
gridflow_python_mediator-0.1.1.tar.gz 7.1 kB With provenance Details for gridflow_python_mediator-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gridflow-python-mediator 0.1.1
File Interpreter ABI Platform Actions
gridflow_python_mediator-0.1.1-py3-none-any.whl Python 3 none any Details for gridflow_python_mediator-0.1.1-py3-none-any.whl

Total release size: 13.9 kB

Release files / gridflow_python_mediator-0.1.1.tar.gz

Download URL gridflow_python_mediator-0.1.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1f11e7b53ff657aecee2ef609f6603e07d0c32509d54258a52adefe22feff2c7
BLAKE2b-256 checksum
How to use checksums
dd1fe0c3be51942350fda4068a26d77be037bf71f4cfc73c0b85be9ac33c9ac7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Release files / gridflow_python_mediator-0.1.1-py3-none-any.whl

Download URL gridflow_python_mediator-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec0741fa7ae5be23068c9cb02c9324be8f768a89a6cf163b8f122f685a1af1c8
BLAKE2b-256 checksum
How to use checksums
335cc470a8bdd438661223408195bef114b33cf4285b32b5922f9771cdabb210
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Security for gridflow-python-mediator 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for gridflow_python_mediator-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page