tombulled / mediate

Middleware for every occasion
https://pypi.org/project/mediate/
MIT License
0 stars 0 forks source link

mediate

Middleware for every occasion

Installation

mediate can be installed from PyPI

pip install mediate

Usage

@middleware

from mediate import middleware

def shout(call_next, name):
    return call_next(name.upper())

def exclaim(call_next, name):
    return call_next(name + "!")

@middleware(shout, exclaim)
def hello(name):
    print(f"Hello, {name}")
>>> hello("sam")
Hello, SAM!

Middleware

Middleware.bind

import mediate

middleware = mediate.Middleware()

@middleware
def shout(call_next, name):
    return call_next(name.upper())

@middleware
def exclaim(call_next, name):
    return call_next(name + "!")

@middleware.bind
def hello(name):
    print(f"Hello, {name}")
>>> hello("sam")
Hello, SAM!

Middleware.compose

import mediate

middleware = mediate.Middleware()

@middleware
def shout(call_next, name):
    return call_next(name.upper())

@middleware
def exclaim(call_next, name):
    return call_next(name + "!")

def hello(name):
    print(f"Hello, {name}")

composed_hello = middleware.compose(hello)
>>> hello("sam")
Hello, sam
>>> composed_hello("sam")
Hello, SAM!