FastAPI dependencies and utilities.
Project description
Source code: https://github.com/volfpeter/fasted
Documentation and examples: https://volfpeter.github.io/fasted
FastED
FastAPI dependencies and utilities.
Installation
The package is available on PyPI and can be installed with:
$ pip install fasted
Features
selfdependent
Decorator that let's you use your business objects' instance methods as FastAPI dependencies without writing any additional code.
Supports:
- Sync and async instance methods.
- Sync and async generator methods.
- Inheritence and
super()
calls is decorated methods. - An optional factory (FastAPI dependency) for creating the
self
instance. If not set, the class'__init__()
method will serve as the dependency for creating theself
instance. - Decorated instance methods will behave as expected if called directly.
Example use:
from typing import Annotated
from fastapi import Depends, FastAPI
from fasted import selfdependent
def double() -> "Multiplier":
# Dependency that returns a Multiplier with base = 2.
return Multiplier(2)
class Multiplier:
def __init__(self, base: float) -> None:
self.base = base
@selfdependent()
def multiply(self, mul: float) -> float:
# `__init__()` will be used as the dependency to create `self`, so the route
# where this method is used will have a `base` and a `mul` query parameter.
return self.base * mul
@selfdependent(factory=double)
async def double(self, mul: float) -> float:
# `double()` will be used as the dependency to create `self`, so the route
# where this method is used will only have a `mul` query parameter.
return self.base * mul
app = FastAPI()
@app.get("/multiply")
def multiply_route(value: Annotated[float, Depends(Multiplier.multiply)]) -> float:
# FastAPI will create the `Multiplier` instance based on `Multiplier.__init__()` and
# automatically feed this `instance` as `self` to `Multiplier.multiply()` to calculate
# the value of the dependency.
return value
@app.get("/double")
def double_route(value: Annotated[float, Depends(Multiplier.double)]) -> float:
# FastAPI will create the `Multiplier` instance using the `double()` factory (dependency)
# and automatically feed this instance as `self` to `Multiplier.multiply()` to
# calculate the value of the dependency.
return value
Dependency
Generic type for FastAPI dependencies.
Example use:
from typing import Annotated, Generator
from fastapi import FastAPI, APIRouter
from fasted import Dependency
# from x import Session
def make_api(make_session: Dependency[Session]) -> APIRouter:
DependsSession = Annotated[Session, Depends(make_session)]
api = APIRouter()
@api.get("/")
def get(session: DependsSession) -> int:
return 4
return api
def make_db_session() -> Generator[Session, None, None]:
with Session(database) as session:
yield session
app = FastAPI()
app.include_router(make_api(make_db_session), prefix="/random-number")
Dependencies
Being a FastAPI utility library, the only dependency is (and will remain) fastapi
.
Development
Use ruff
for linting and formatting, mypy
for static code analysis, and pytest
for testing.
The documentation is built with mkdocs-material
and mkdocstrings
.
Contributing
All contributions are welcome.
License - MIT
The package is open-sourced under the conditions of the MIT license.
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
File details
Details for the file fasted-0.2404.2.tar.gz
.
File metadata
- Download URL: fasted-0.2404.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f08145ac89edd1a7ac25f1e8a76ae260c524959a5195c23509a103c3a92353fb |
|
MD5 | b3b4da19ee353188068551a916ed9ac1 |
|
BLAKE2b-256 | 2c3b85486aeff22948f2439483f080843e4431a40100172d8fabcb5cb0ce0b22 |
File details
Details for the file fasted-0.2404.2-py3-none-any.whl
.
File metadata
- Download URL: fasted-0.2404.2-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4f59dbc1393817559c5fa0c0e226e72178c1eccb8b0e2c2a143d4d47cac19cb |
|
MD5 | e1ad77487c8c76889f783a5e495fc851 |
|
BLAKE2b-256 | a68e60b17fe1ea4c1c95c98042adadf88035ecb1a86d60c8adedafa3edea161a |