fastapi-filter is a library that allows the client to implement filters for each route in an API. It uses a Starlette middleware to determine which filters should be executed before the main handler is called.
The library remains in Alpha Mode currently. If any bugs or issues are encountered, feel free to create an Issue or email me directly.
Currently support is limited to fastapi
and it is a dependency. The project will be expanded to include all applications built on top of Starlette
.
- Install the following python libraries
- pip install fastapi
- Add the fastapi_filter directory into your project directory using the git clone commpand.
To begin filtering requests, the CustomFilterMiddleware
class must be configured and registered with our Starlette
application.
If we have the following route in our application,
import fastapi
app = FastAPI()
@app.get("/")
async def HelloWorld():
return "Hello World"
Creating filters is an incredibly simple process. The filter must be of type FunctionType and accept a parameter of request which is of type fastapi.Request .
from datetime import datetime
# The method takes an object request of type fastapi.Request
def TimeRouteFilter(request: Request) -> Request:
time = datetime.now()
print("The time of incoming request to '/' is {0}".format(time))
return request #All filters must return an object of type Request
After our filter is created, we can register it with an instance of FilterAPIRouter
which will overlook all incoming requests with the prefix "/" and implement the neccesary filters.
from fastapi_filter.filter import FilterAPIRouter
my_filter = FilterAPIRouter(prefix = "/")
.includeFilterOnMethod(method = "HelloWorld", filter = TimeRouteFilter)
.includeFilterOnMethod(...)
Alternatively a decorator can be used to register TimeRouteFilter
import fastapi
from fastapi_filter.filter import FilterAPIRouter
my_filter = FilterAPIRouter(prefix = "/")
app = FastAPI()
@my_filter.InsertMethodLevelFilter(filters = [TimeRouteFilter]) #The filters must be passed through in an array
@app.get("/")
async def HelloWorld():
return "Hello World"
Our filter can now be finally registered with our middleware application.
import fastapi
from fastapi import APIRoute
from fastapi_filter.filter import FilterAPIRouter
from fastapi_filter.middleware import CustomFilterMiddleware
from starlette.middleware import Middleware
my_filter = FilterAPIRouter(prefix = "/")
.includeFilterOnMethod(method = "HelloWorld", filter = TimeRouteFilter)
.includeFilterOnMethod(...)
app_middleware = [
Middleware(CustomFilterMiddleware, filter_routers =[my_filter])
]
app = FastAPI(middleware=app_middleware)
@app.get("/")
async def HelloWorld():
return "Hello World"
The documentation is currently a work in progress. You can refer to Examples for examples of implementation.
If you would like to contribute to the project, feel free to create a pull request. Make sure that all documentation is present for any changes.
The library is licensed under GNU GENERAL PUBLIC LICENSE