Skip to main content

Extends FastAPI Depends classes to simple way of modifying them after creating

Project description

CDNJS CDNJS

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 callable object)
  • class methods
  • static methods
  • instance methods
  • property returning callable

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 dependency
  • from_super - bool, on true, will use attribute method_name from super class like super().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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-depends-ext-0.2.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

fastapi_depends_ext-0.2.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-depends-ext-0.2.1.tar.gz.

File metadata

  • Download URL: fastapi-depends-ext-0.2.1.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

Hashes for fastapi-depends-ext-0.2.1.tar.gz
Algorithm Hash digest
SHA256 57da1b177a9f9f2fa1012e7bd0197ff85b40c610e90faa42e3e9681d5eb0c07a
MD5 c466b2bd9b853403c305c447b5b79e42
BLAKE2b-256 25b2809baca870a81787229e87a386119517205f7c57ac5b817f963211e838cb

See more details on using hashes here.

File details

Details for the file fastapi_depends_ext-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_depends_ext-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8eb0c6e57799712057c1cff89b68f206111431546ac27888b373b8279547a5e8
MD5 c3ccc76e9345854871201033e9b19141
BLAKE2b-256 6c64ce790c110158ba10c8186e98f51be95b5574c06aedac94da2dabcc7d0f01

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page