A simple library for working with middlewares in Python
Project description
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:
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for python_middlewareable-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58709091fdb9c8f27624f2e09dc6317d9603b9c513710c30623ca74227260b4d |
|
MD5 | 35086f71b6bb5a518fff857cd52daeb8 |
|
BLAKE2b-256 | bbc2f37d47f16c009ed5e702ec8dff2baa391758fe36a4e4ba9f917e3f57c050 |
Close
Hashes for python_middlewareable-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44e860b4acef6ba95064776b83cd97e09f5f426bd18de74b0ea64af9f1940fec |
|
MD5 | e4b96ed2637e987945992207552aa4c7 |
|
BLAKE2b-256 | 64acefc47208b02736aa74e5abe34f2e0d6dc834fa14a0fbc3ded42c710b9b14 |