fastapi-depends-ext


Namefastapi-depends-ext JSON
Version 0.2.2 PyPI version JSON
download
home_pagehttps://github.com/Nikakto/fastapi-depends-ext
SummaryExtends FastAPI Depends classes to simple way of modifying them after creating
upload_time2023-02-04 13:33:30
maintainer
docs_urlNone
authorNikakto
requires_python>=3.8,<4.0
licenseMIT
keywords fastapi depends dependencies
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![CDNJS](https://img.shields.io/badge/Python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-2334D058)
![CDNJS](https://shields.io/badge/FastAPI-%3E=0.7.0-009485)

# 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

```python
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` (must return `callable` that 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 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:
```python
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
```python
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

```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Nikakto/fastapi-depends-ext",
    "name": "fastapi-depends-ext",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "fastapi,depends,dependencies",
    "author": "Nikakto",
    "author_email": "mcgish@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/5b/34/2c7a6980deef558a9705814fab01de0e74066f13a3814da60684fe190ddc/fastapi-depends-ext-0.2.2.tar.gz",
    "platform": null,
    "description": "![CDNJS](https://img.shields.io/badge/Python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-2334D058)\n![CDNJS](https://shields.io/badge/FastAPI-%3E=0.7.0-009485)\n\n# FastAPI depends extension\n\n## Introduction\n\nSometimes 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.\n\n## Installation\n\n```\npip install fastapi-depends-ext\n```\n\n## Tutorial\n\n#### DependsAttr\n\n```python\nfrom typing import List\n\nfrom fastapi import Depends\nfrom fastapi import FastAPI\nfrom fastapi import Query\nfrom pydantic import conint\n\nfrom fastapi_depends_ext import DependsAttr\nfrom fastapi_depends_ext import DependsAttrBinder\n\n\nclass ItemsPaginated(DependsAttrBinder):\n    _items = list(range(100))\n\n    async def get_page(self, page: conint(ge=1) = Query(1)):\n        return page\n\n    async def items(self, page: int = DependsAttr(\"get_page\")):\n        _slice = slice(page * 10, (page + 1) * 10)\n        return self._items[_slice]\n\n\nclass ItemsSquarePaginated(ItemsPaginated):\n    async def items(self, items: List[int] = DependsAttr(\"items\", from_super=True)):\n        return [i**2 for i in items]\n\n\napp = FastAPI()\n\n\n@app.get(\"/\")\ndef items_list(items: List[int] = Depends(ItemsPaginated().items)) -> List[int]:\n    return items\n\n\n@app.get(\"/square\")\ndef items_list_square(items: List[int] = Depends(ItemsSquarePaginated().items)) -> List[int]:\n    return items\n```\n\nUse `DependsAttr` to `Depends` from current instance attributes. All examples use `asyncio`, but you can write all methods synchronous.\n\n`DependsAttr` support next properties:\n- class variables (must contains `callable` object)\n- class methods\n- static methods\n- instance methods\n- `property` (must return `callable` that will be used as dependency)\n\nYour class must inherit from `DependsAttrBinder` and attributes must be `DependsAttr`. `DependsAttrBinder` automatically patch all methods with `DependsAttr` by instance attributes.\n\n`DependsAttr` arguments:\n- `method_name` - `str`, name of instance attribute to use as dependency\n- `from_super` - `bool`, on true, will use attribute `method_name` from super class like `super().method_name()`\n- `use_cache` - `bool`, allow to cache depends result for the same dependencies in request\n\n#### DependsExt\n\nUseless(?) class created to proof of concept of patching methods and correct work `FastAPI` applications.\n\n`DependsExt` allow you define default values of method arguments after `FastAPI` endpoint has been created.  \n\nExample:\n```python\nfrom fastapi import FastAPI\nfrom fastapi import Query\n\nfrom fastapi_depends_ext import DependsExt\n\n\ndef pagination(page: int = Query()):\n    return page\n\n\ndepends = DependsExt(pagination)\n\n\napp = FastAPI()\n\n\n@app.on_event(\"startup\")\ndef setup_depends():\n    depends.bind(page=Query(1))\n\n\n@app.get(\"/\")\ndef get_method(value: int = depends) -> int:\n    return value\n\n```\n\nIs equivalent for\n```python\nfrom fastapi import Depends\nfrom fastapi import FastAPI\nfrom fastapi import Query\n\n\ndef pagination(page: int = Query(1)):\n    return page\n\n\napp = FastAPI()\n\n\n@app.get(\"/\")\ndef get_method(value: int = Depends(pagination)) -> int:\n    return value\n\n```",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Extends FastAPI Depends classes to simple way of modifying them after creating",
    "version": "0.2.2",
    "split_keywords": [
        "fastapi",
        "depends",
        "dependencies"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79febd8248c056908d48e3b84c5dbdc26602af098f8a86ff0ef538e521a7f43c",
                "md5": "4ce0bc4ed64e62959163f6695c7ae39a",
                "sha256": "37014631dc6717dec0f8ea0e88c555d68d688ec80b3b8639476df5e810e23bd6"
            },
            "downloads": -1,
            "filename": "fastapi_depends_ext-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4ce0bc4ed64e62959163f6695c7ae39a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6773,
            "upload_time": "2023-02-04T13:33:32",
            "upload_time_iso_8601": "2023-02-04T13:33:32.141778Z",
            "url": "https://files.pythonhosted.org/packages/79/fe/bd8248c056908d48e3b84c5dbdc26602af098f8a86ff0ef538e521a7f43c/fastapi_depends_ext-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b342c7a6980deef558a9705814fab01de0e74066f13a3814da60684fe190ddc",
                "md5": "8c3524a0b147b164e1b32fa2feced798",
                "sha256": "202ef2f2fcfb805f7422d1c689a9c979569e540fa134fca5df0ca672735fdd96"
            },
            "downloads": -1,
            "filename": "fastapi-depends-ext-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "8c3524a0b147b164e1b32fa2feced798",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 6747,
            "upload_time": "2023-02-04T13:33:30",
            "upload_time_iso_8601": "2023-02-04T13:33:30.212210Z",
            "url": "https://files.pythonhosted.org/packages/5b/34/2c7a6980deef558a9705814fab01de0e74066f13a3814da60684fe190ddc/fastapi-depends-ext-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-04 13:33:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Nikakto",
    "github_project": "fastapi-depends-ext",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "fastapi-depends-ext"
}
        
Elapsed time: 0.03474s