Fastapi-class

Latest version: v3.7.0

Safety actively analyzes 706267 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 2 of 2

3.1.0

⚠️ Drop Support of Python 3.8 | 3.9 & New Break Changes

As you create more complex FastAPI applications, you may find yourself frequently repeating the same dependencies in multiple related endpoints.

We Introduce this new version **showcases the usage of CVB in FastAPI** by yezz123 in https://github.com/yezz123/fastapi-class/pull/82

A common question people have as they become more comfortable with FastAPI is how they can reduce the number of times they have to copy/paste the same dependency into related routes.

`fastapi_class` provides a `class-based view` decorator `View` to help reduce the amount of boilerplate necessary when developing related routes.

> Highly inspired by [Fastapi-utils](https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/), Thanks to [dmontagu](https://github.com/dmontagu) for the great work.

- Example:

python
from fastapi import FastAPI, APIRouter, Query
from pydantic import BaseModel
from fastapi_class import View

app = FastAPI()
router = APIRouter()

class ItemModel(BaseModel):
id: int
name: str
description: str = None

View(router)
class ItemView:
def post(self, item: ItemModel):
return item

def get(self, item_id: int = Query(..., gt=0)):
return {"item_id": item_id}

app.include_router(router)


Response model 📦

`Exception` in list need to be either function that return `fastapi.HTTPException` itself. In case of a function it is required to have all of it's arguments to be `optional`.

py
from fastapi import FastAPI, APIRouter, HTTPException, status
from fastapi.responses import PlainTextResponse
from pydantic import BaseModel

from fastapi_class import View

app = FastAPI()
router = APIRouter()

NOT_AUTHORIZED = HTTPException(401, "Not authorized.")
NOT_ALLOWED = HTTPException(405, "Method not allowed.")
NOT_FOUND = lambda item_id="item_id": HTTPException(404, f"Item with {item_id} not found.")

class ItemResponse(BaseModel):
field: str | None = None

view(router)
class MyView:
exceptions = {
"__all__": [NOT_AUTHORIZED],
"put": [NOT_ALLOWED, NOT_FOUND]
}

RESPONSE_MODEL = {
"put": ItemResponse
}

RESPONSE_CLASS = {
"delete": PlainTextResponse
}

def get(self):
...
def put(self):
...
def delete(self):
...

app.include_router(router)


Customized Endpoints

py
from fastapi import FastAPI, APIRouter, HTTPException
from fastapi.responses import PlainTextResponse
from pydantic import BaseModel

from fastapi_class import View, endpoint

app = FastAPI()
router = APIRouter()

NOT_AUTHORIZED = HTTPException(401, "Not authorized.")
NOT_ALLOWED = HTTPException(405, "Method not allowed.")
NOT_FOUND = lambda item_id="item_id": HTTPException(404, f"Item with {item_id} not found.")
EXCEPTION = HTTPException(400, "Example.")

class UserResponse(BaseModel):
field: str | None = None

View(router)
class MyView:
exceptions = {
"__all__": [NOT_AUTHORIZED],
"put": [NOT_ALLOWED, NOT_FOUND],
"edit": [EXCEPTION]
}

RESPONSE_MODEL = {
"put": UserResponse,
"edit": UserResponse
}

RESPONSE_CLASS = {
"delete": PlainTextResponse
}

def get(self):
...
def put(self):
...
def delete(self):
...
endpoint(("PUT",), path="edit")
def edit(self):
...


Dependencies 🔨

* ⬆ Bump requests from 2.28.1 to 2.28.2 by dependabot in https://github.com/yezz123/fastapi-class/pull/62
* ⬆ Bump pytest from 7.2.0 to 7.2.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/63
* ⬆ Bump pre-commit from 2.21.0 to 3.0.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/64
* ⬆ Bump pre-commit from 3.0.0 to 3.0.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/65
* ⬆ Bump pre-commit from 3.1.1 to 3.2.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/85
* ⬆ Update fastapi requirement from <0.95.0,>=0.65.2 to >=0.65.2,<0.96.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/86
* ⬆ Bump pytest-asyncio from 0.20.3 to 0.21.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/87
* ⬆ Bump pypa/gh-action-pypi-publish from 1.7.1 to 1.8.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/88


**Full Changelog**: https://github.com/yezz123/fastapi-class/compare/2.0.0...3.1.0

2.0.0

What's Changed
* Bump mkdocs-material from 8.0.2 to 8.0.3 by dependabot in https://github.com/yezz123/fastapi-class/pull/12
* Bump mkdocs-material from 8.0.3 to 8.0.4 by dependabot in https://github.com/yezz123/fastapi-class/pull/13
* Bump mkdocs-material from 8.0.4 to 8.0.5 by dependabot in https://github.com/yezz123/fastapi-class/pull/14
* Bump mkdocs-material from 8.0.5 to 8.1.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/16
* Bump fastapi from 0.70.0 to 0.70.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/15
* Bump mkdocs-material from 8.1.0 to 8.1.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/17
* Bump mkdocs-material from 8.1.1 to 8.1.2 by dependabot in https://github.com/yezz123/fastapi-class/pull/18
* Bump mkdocs-markdownextradata-plugin from 0.2.4 to 0.2.5 by dependabot in https://github.com/yezz123/fastapi-class/pull/19
* Bump mkdocs-material from 8.1.2 to 8.1.3 by dependabot in https://github.com/yezz123/fastapi-class/pull/20
* Bump pydantic from 1.8.2 to 1.9.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/21
* Bump mkdocs-material from 8.1.3 to 8.1.4 by dependabot in https://github.com/yezz123/fastapi-class/pull/22
* Bump requests from 2.26.0 to 2.27.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/23
* Bump fastapi from 0.70.1 to 0.73.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/31
* Bump mkdocs-material from 8.1.4 to 8.1.8 by dependabot in https://github.com/yezz123/fastapi-class/pull/30
* Bump requests from 2.27.0 to 2.27.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/24
* Bump mkdocs-material from 8.1.8 to 8.1.9 by dependabot in https://github.com/yezz123/fastapi-class/pull/32
* Bump pytest from 6.2.5 to 7.0.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/33
* Bump mkdocs-material from 8.1.9 to 8.1.10 by dependabot in https://github.com/yezz123/fastapi-class/pull/34
* Bump pytest from 7.0.0 to 7.0.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/36
* Bump mkdocs-material from 8.1.10 to 8.2.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/37
* Bump fastapi from 0.73.0 to 0.74.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/39
* Bump mkdocs-material from 8.2.1 to 8.2.3 by dependabot in https://github.com/yezz123/fastapi-class/pull/40
* Bump fastapi from 0.74.1 to 0.75.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/42
* Bump mkdocs-material from 8.2.3 to 8.2.5 by dependabot in https://github.com/yezz123/fastapi-class/pull/43
* Bump pytest from 7.0.1 to 7.1.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/44
* chore(feat): add explicit arguments by yezz123 in https://github.com/yezz123/fastapi-class/pull/52
* chore(feat): Migrate from setuptools to Flit by yezz123 in https://github.com/yezz123/fastapi-class/pull/53
* feat: Add patch function by yezz123 in https://github.com/yezz123/fastapi-class/pull/54
* 🐛 Overwrite the partial function by yezz123 in https://github.com/yezz123/fastapi-class/pull/55
* Add CodeQL workflow for GitHub code scanning by lgtm-com in https://github.com/yezz123/fastapi-class/pull/56
* :bookmark: Version 2.0.0 of FastAPI Class by yezz123 in https://github.com/yezz123/fastapi-class/pull/58
* ⬆ Bump requests from 2.27.1 to 2.28.1 by dependabot in https://github.com/yezz123/fastapi-class/pull/61
* ⬆ Update fastapi requirement from <0.87.0,>=0.65.2 to >=0.65.2,<0.90.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/60
* ⬆ Bump codecov/codecov-action from 1 to 3 by dependabot in https://github.com/yezz123/fastapi-class/pull/59

New Contributors
* lgtm-com made their first contribution in https://github.com/yezz123/fastapi-class/pull/56

**Full Changelog**: https://github.com/yezz123/fastapi-class/compare/1.1.2...2.0.0

1.1.2

What's Changed
* Chore: Uncover some Functions by yezz123 in https://github.com/yezz123/fastapi-class/pull/8
* Bump mkdocs-material from 7.2.6 to 8.0.2 by dependabot in https://github.com/yezz123/fastapi-class/pull/9
* Bump fastapi from 0.68.1 to 0.70.0 by dependabot in https://github.com/yezz123/fastapi-class/pull/11


**Full Changelog**: https://github.com/yezz123/fastapi-class/compare/1.1.1...1.1.2

1.1.1

What's Changed

- We Just Build the Documentation using MKdocs <https://yezz123.github.io/fastapi-class/>
- Improve README.
- Bump to Python3.10

Pull Requests

* Create FastAPI Class Documentation ✨ by yezz123 in https://github.com/yezz123/fastapi-class/pull/6
* Bump to 1.1.1 ✨ by yezz123 in https://github.com/yezz123/fastapi-class/pull/7


**Full Changelog**: https://github.com/yezz123/fastapi-class/compare/1.1.0...1.1.1

1.1.0

What's Changed
* Add More Classifiers & Fix Typo ✨ by yezz123 in https://github.com/yezz123/fastapi-class/pull/3


**Full Changelog**: https://github.com/yezz123/fastapi-class/compare/1.0.0...1.1.0

1.0.0

Classes and Decorators to use FastAPI with `class based routing`. In particular this allows you to
construct an **instance** of a class and have methods of that instance be route handlers for FastAPI & Python 3.8.

- Older Versions of Python:
- Unfortunately this does not work with `async` routes with Python versions less than 3.8 [due to bugs in `inspect.iscoroutinefunction`](https://stackoverflow.com/a/52422903/1431244). Specifically with older versions of Python `iscoroutinefunction` incorrectly returns false so `async` routes aren't `await`'d. We therefore only support Python versions >= 3.8.

Example 🐢

py
from ping import pong
Some fictional ping pong class
from fastapi_class import Routable, get, delete

def parse_arg() -> argparse.Namespace:
"""parse command line arguments."""
...


class UserRoutes(Routable):
"""Inherits from Routable."""

Note injection here by simply passing values to the constructor. Other injection frameworks also
supported as there's nothing special about this __init__ method.
def __init__(self, pong: pong) -> None:
"""Constructor. The pong is injected here."""
self.__pong = pong

get('/user/{name}')
def get_user_by_name(name: str) -> User:
Use our injected pong instance.
return self.__pong.get_user_by_name(name)

delete('/user/{name}')
def delete_user(name: str) -> None:
self.__pong.delete(name)


def main():
args = parse_args()
Configure the pomg per command line arguments
pong = pong(args.url, args.user, args.password)
Simple intuitive injection
user_routes = UserRoutes(pong)

app = FastAPI()
router member inherited from Routable and configured per the annotations.
app.include_router(user_routes.router)

Page 2 of 2

© 2025 Safety CLI Cybersecurity Inc. All Rights Reserved.