A simple library for working with middlewares in Python.
pip install python-middlewareable
@dataclass
class Request(RequestBase): # inherit from RequestBase
value: str
class OneMiddleware(MiddlewareBase[Request]):
async def handle(
self, request: Request, next_call: MiddlewareNextCallBase[Request]
) -> Request:
# do something before the next middleware
print("OneMiddleware before")
# modify the request
request.value = f"Hello, {request.value} from OneMiddleware"
# call the next middleware
result = await next_call(request)
# do something after the next middleware
print("OneMiddleware after")
# return the result
return result
class App(MiddlewareableBase[Request]):
middlewares = [OneMiddleware] # add your middlewares here
# middlewareable
app = App()
# process request
result = await app.process_middlewares(Request(value="Hello"))
# check the result
print(request)
# output:
# OneMiddleware before
# OneMiddleware after
# Hello from OneMiddleware
You can use the following traits to extend the functionality of this library:
This project is licensed under the MIT License - see the LICENSE file for details.