Skip to content

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.

License

Notifications You must be signed in to change notification settings

PratyushMakkar/fastapi-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚙️ fastapi-filter

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.

📥 Dependancies And Installtion

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.

  1. Install the following python libraries
  • pip install fastapi
  1. Add the fastapi_filter directory into your project directory using the git clone commpand.

📌 Quickstart

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"

Documentation

The documentation is currently a work in progress. You can refer to Examples for examples of implementation.

🛠️ Contributing to the project

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.

📜 License

The library is licensed under GNU GENERAL PUBLIC LICENSE

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages