Last updated:
0 purchases
pythonmiddlewareable 1.0.0
Python-Middlewareable
A simple library for working with middlewares in Python.
Installation
pip install python-middlewareable
Usage
1. Define the payload structure
@dataclass
class Request(RequestBase): # inherit from RequestBase
name: str
2. Create a middleware
class OneMiddleware(MiddlewareBase[Request]):
async def handle(
self, request: Request, next_call: MiddlewareNextCallBase[Request]
) -> None:
request.name = request.name + " from OneMiddleware"
print("OneMiddleware before")
await next_call(request)
print("OneMiddleware after")
3. Add the MiddlewareableBase trait to the class that will use the middlewares
class App(MiddlewareableBase[Request]):
middlewares = [OneMiddleware] # add your middlewares here
4. Instantiate and use it
# middlewareable
app = App()
# process request
result = await app.process_middlewares(Request(name="Hello"))
# check the result
print(request)
# output:
# OneMiddleware before
# OneMiddleware after
# Hello from OneMiddleware
Traits
You can use the following traits to extend the functionality of your classes:
AutoInstantiable
DataStructurable
License
This project is licensed under the MIT License - see the LICENSE file for details.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.