Middleware for every occasion
mediate
can be installed from PyPI
pip install mediate
@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!