Extends FastAPI Depends classes to simple way of modifying them after creating
Project description
FastAPI depends extension
Introduction
Sometimes your FastAPI dependencies have to get value from functions cannot be available on initialization. The problem is particularly acute to use class dependencies with inheritance. This project try to solve problem of modify Depends after application initialization.
Installation
pip install fastapi-depends-ext
Tutorial
DependsAttr
from typing import List
from fastapi import Depends
from fastapi import FastAPI
from fastapi import Query
from pydantic import conint
from fastapi_depends_ext import DependsAttr
from fastapi_depends_ext import DependsAttrBinder
class ItemsPaginated(DependsAttrBinder):
_items = list(range(100))
async def get_page(self, page: conint(ge=1) = Query(1)):
return page
async def items(self, page: int = DependsAttr("get_page")):
_slice = slice(page * 10, (page + 1) * 10)
return self._items[_slice]
class ItemsSquarePaginated(ItemsPaginated):
async def items(self, items: List[int] = DependsAttr("items", from_super=True)):
return [i**2 for i in items]
app = FastAPI()
@app.get("/")
def items_list(items: List[int] = Depends(ItemsPaginated().items)) -> List[int]:
return items
@app.get("/square")
def items_list_square(items: List[int] = Depends(ItemsSquarePaginated().items)) -> List[int]:
return items
Use DependsAttr to Depends from current instance attributes. All examples use asyncio, but you can write all methods synchronous.
DependsAttr support next properties:
- class variables (must contains
callableobject) - class methods
- static methods
- instance methods
property(must returncallablethat will be used as dependency)
Your class must inherit from DependsAttrBinder and attributes must be DependsAttr. DependsAttrBinder automatically patch all methods with DependsAttr by instance attributes.
DependsAttr arguments:
method_name-str, name of instance attribute to use as dependencyfrom_super-bool, on true, will use attributemethod_namefrom super class likesuper().method_name()use_cache-bool, allow to cache depends result for the same dependencies in request
DependsExt
Useless(?) class created to proof of concept of patching methods and correct work FastAPI applications.
DependsExt allow you define default values of method arguments after FastAPI endpoint has been created.
Example:
from fastapi import FastAPI
from fastapi import Query
from fastapi_depends_ext import DependsExt
def pagination(page: int = Query()):
return page
depends = DependsExt(pagination)
app = FastAPI()
@app.on_event("startup")
def setup_depends():
depends.bind(page=Query(1))
@app.get("/")
def get_method(value: int = depends) -> int:
return value
Is equivalent for
from fastapi import Depends
from fastapi import FastAPI
from fastapi import Query
def pagination(page: int = Query(1)):
return page
app = FastAPI()
@app.get("/")
def get_method(value: int = Depends(pagination)) -> int:
return value
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
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-depends-ext-0.2.2.tar.gz.
File metadata
- Download URL: fastapi-depends-ext-0.2.2.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.1 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
202ef2f2fcfb805f7422d1c689a9c979569e540fa134fca5df0ca672735fdd96
|
|
| MD5 |
8c3524a0b147b164e1b32fa2feced798
|
|
| BLAKE2b-256 |
5b342c7a6980deef558a9705814fab01de0e74066f13a3814da60684fe190ddc
|
File details
Details for the file fastapi_depends_ext-0.2.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_depends_ext-0.2.2-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.1 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37014631dc6717dec0f8ea0e88c555d68d688ec80b3b8639476df5e810e23bd6
|
|
| MD5 |
4ce0bc4ed64e62959163f6695c7ae39a
|
|
| BLAKE2b-256 |
79febd8248c056908d48e3b84c5dbdc26602af098f8a86ff0ef538e521a7f43c
|