Package to create API controllers that can inherit routes
Project description
FastAPI Controller
Write Fast API Controllers (Classes) that can inherit route information from it's parent. This also allows to create a path prefix from a template and add api version information in the template.
This utility library is written on top of fastapi-utils.
Install
$ pip install fastapi-controller
Example
import uvicorn
from pydantic import BaseModel
from fastapi import Header, Depends, FastAPI
from fastapi_controller import create_controller, controller, register_controllers_to_app
# Create a controller with a path prefix template that can use the
# controller name and version as template variables
# For a controller named PeopleController, {controller} is replaced
# by `people`
ControllerBase = create_controller("/v{version}/{controller}", "1.0")
def get_jwt_user(auth: str = Header(...)) -> str:
""" Pretend this function gets a UserID from a JWT in the auth header """
return auth
class GetModel(BaseModel):
hello: str
class DefaultController(ControllerBase):
user_id: str = Depends(get_jwt_user)
@controller.get("/", response_model=GetModel)
def get_all(self) -> dict:
return {"hello": self.user_id}
@controller.get("/{model_id}")
def get_one(self, model_id: int):
return {"one": model_id}
class PeopleController(DefaultController):
# The people controller will have the two GET routes from the
# parent class as well as the post route defined here.
@controller.post("/")
def create(self):
return {"create": self.user_id}
class ScheduledJobsController(DefaultController):
# The jobs controller will have the two GET routes from the parent
# class with one of the routes implementation overridden.
def get_one(self, model_id: int):
# This'll inherit the route information GET /{model_id} from the
# parent class and override functionality of the route
pass
if __name__ == '__main__':
app = FastAPI()
# This function registers all the child controllers who have
# ControllerBase as their parent in their inheritance heirarchy.
# Intermediate Controllers between the ControllerBase and the
# Child Controllers are ignored
# For e.g. For the following inheritance heirarchy
# ControllerBase -> DefaultController -> DerivedController -> FunctionsController -> PeopleController
# Only the PeopleController is used for registering
# the API routes with all the routes defined in the parent controllers
# available in the child controller.
register_controllers_to_app(app, ControllerBase)
uvicorn.run(app, host="0.0.0.0", port=8000)
With the above example, the following API routes will be registered:
GET /v1.0/people/
GET /v1.0/people/{model_id}
POST /v1.0/people/
GET /v1.0/scheduled_jobs/
GET /v1.0/scheduled_jobs/{model_id}
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi-controller-0.0.8.tar.gz.
File metadata
- Download URL: fastapi-controller-0.0.8.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f472a6eb53ef33f2404dd85c303fa02a2bb35e86e036b3b272ceae61f75ea711
|
|
| MD5 |
bb175d4da63a3a4eb7d8bfbc0cc3a6c6
|
|
| BLAKE2b-256 |
49301172f5455bdb409aec348fc2b6406e07a0fc97763c87c92b96cecd993a37
|
File details
Details for the file fastapi_controller-0.0.8-py3-none-any.whl.
File metadata
- Download URL: fastapi_controller-0.0.8-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d184e9b90c2990e0eb5c4e0c492449dce246275c6f8fd22417a678e88700fe0
|
|
| MD5 |
e860de59a1a364adbf4aed8a88566acc
|
|
| BLAKE2b-256 |
1c4b20655d71a1b91da3335dd6401aead4d7fce94b72dccbc6a14a9335026ac3
|