fastapi-depends-extension


Namefastapi-depends-extension JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-10-01 07:26:25
maintainerNone
docs_urlNone
authorDaniil Solynin
requires_python<4.0,>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Fastapi depends extension

![PyPI - Version](https://img.shields.io/pypi/v/fastapi-depends-extension?color=green)
[![Supported versions](https://img.shields.io/pypi/pyversions/fastapi-depends-extension.svg)](https://pypi.python.org/pypi/fastapi-depends-extension)
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/didhat/fastapi-depends-ext/github-ci.yml)](https://github.com/reagento/dishka/actions)

### Purpose

This library extends capabilities of standard Depends di in FastaAPI and add Resources that creating only once and than
dependency always return created object at first time.
This is convenient when we create some kind of connection in the database or want to use a long-time object for the
entire application. The extension can also correctly handle resource closures on shutdown application in the familiar
Depends style. With this extension you don't need to create global variables and other strange things in your code.

### Quickstart

1. Install extension

```shell
pip install fastapi-depends-extension
```

2. Create FastApi application and setup di extenstion

```python
from fastapi import FastAPI, Depends
from extdepends import setup_extend_di, on_di_shutdown

app = FastAPI(lifespan=on_di_shutdown)
setup_extend_di(app)
```

or you can extend your own lifespan

```python
from extdepends import setup_extend_di, on_di_shutdown


async def app_lifespan(app):
    print("your own code")
    yield
    await on_di_shutdown(app)
    print("your close code")


app = FastApi(lifespan=app_lifespan)
```

3. After that you can define your resource and use it in routes almost just like common Depends function, resource only
   does not support callable classes. All current methods for creating resources are described below. Notice that
   function resource_with_close will always return the same resource. Also it is not possible call this function call
   outside di FastAPI, because of serious modification under the hood.

```python
@resource
async def resource_with_close():
    print("open resource logic")
    yield "resource"
    print("close resource login on shutdown application")


app.get("/test")


async def test(dep=Depends(resource_with_close)):
    return dep
```

also you can define others Depends resources or common depends function in resource and it will be work

```python

@resource
def setting():
    return {"setting": 12}


def common_depends():
    return 12


@resource
async def resource_with_close(settings=Depends(setting), common=Depends(common_depends)):
    print(f"open resource logic, with setting: {settings} and common depends {common}")
    yield "resource"
    print("close resource on shutdown resource")
```

### All methods for creating different resources:

1. Resource with closing supports asynchronous and synchronous generators:

```python

@resource
async def async_resource_with_close():
    print("open async resource")
    yield "resource"
    print("close resource on shutdown app")

@resource
def sync_resource_with_close():
   print("open sync resource")
   yield "resource"
   print("close resource on shutdown app")

```

2. Resource with closing has contextmanager support is such way:

```python
from contextlib import asynccontextmanager, contextmanager

@resource
@asynccontextmanager
async def async_context_manager():
   print("open async context manager depends")
   yield "resource"
   print("close async context manager")

@resource
@contextmanager
async def sync_context_manager():
   print("open sync context manager depends")
   yield "resource"
   print("close sync context manager depends")

```

The resource will open a context manager and return the resource that was used throughout the lifecycle of the application. Upon shutdown, the context manager will be closed.

3. Resource without close logic, it's just like singletone

```python

@resource
async def async_resource():
    await asyncio.sleep(2)
    return "resource"

@resource
def sync_resource():
   return "resource"
```


### Limitations

1. Callable classes has not support yet
2. Does not support functions with *args and **kwargs in arguments
3. Decorated functions also may not work correctly





            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-depends-extension",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Daniil Solynin",
    "author_email": "solynynd@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/22/ab/1638ba3b2efbb0e5ff016b9d31cb56a0a1c08e0d030783666e27fe0b0a39/fastapi_depends_extension-0.2.0.tar.gz",
    "platform": null,
    "description": "## Fastapi depends extension\n\n![PyPI - Version](https://img.shields.io/pypi/v/fastapi-depends-extension?color=green)\n[![Supported versions](https://img.shields.io/pypi/pyversions/fastapi-depends-extension.svg)](https://pypi.python.org/pypi/fastapi-depends-extension)\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/didhat/fastapi-depends-ext/github-ci.yml)](https://github.com/reagento/dishka/actions)\n\n### Purpose\n\nThis library extends capabilities of standard Depends di in FastaAPI and add Resources that creating only once and than\ndependency always return created object at first time.\nThis is convenient when we create some kind of connection in the database or want to use a long-time object for the\nentire application. The extension can also correctly handle resource closures on shutdown application in the familiar\nDepends style. With this extension you don't need to create global variables and other strange things in your code.\n\n### Quickstart\n\n1. Install extension\n\n```shell\npip install fastapi-depends-extension\n```\n\n2. Create FastApi application and setup di extenstion\n\n```python\nfrom fastapi import FastAPI, Depends\nfrom extdepends import setup_extend_di, on_di_shutdown\n\napp = FastAPI(lifespan=on_di_shutdown)\nsetup_extend_di(app)\n```\n\nor you can extend your own lifespan\n\n```python\nfrom extdepends import setup_extend_di, on_di_shutdown\n\n\nasync def app_lifespan(app):\n    print(\"your own code\")\n    yield\n    await on_di_shutdown(app)\n    print(\"your close code\")\n\n\napp = FastApi(lifespan=app_lifespan)\n```\n\n3. After that you can define your resource and use it in routes almost just like common Depends function, resource only\n   does not support callable classes. All current methods for creating resources are described below. Notice that\n   function resource_with_close will always return the same resource. Also it is not possible call this function call\n   outside di FastAPI, because of serious modification under the hood.\n\n```python\n@resource\nasync def resource_with_close():\n    print(\"open resource logic\")\n    yield \"resource\"\n    print(\"close resource login on shutdown application\")\n\n\napp.get(\"/test\")\n\n\nasync def test(dep=Depends(resource_with_close)):\n    return dep\n```\n\nalso you can define others Depends resources or common depends function in resource and it will be work\n\n```python\n\n@resource\ndef setting():\n    return {\"setting\": 12}\n\n\ndef common_depends():\n    return 12\n\n\n@resource\nasync def resource_with_close(settings=Depends(setting), common=Depends(common_depends)):\n    print(f\"open resource logic, with setting: {settings} and common depends {common}\")\n    yield \"resource\"\n    print(\"close resource on shutdown resource\")\n```\n\n### All methods for creating different resources:\n\n1. Resource with closing supports asynchronous and synchronous generators:\n\n```python\n\n@resource\nasync def async_resource_with_close():\n    print(\"open async resource\")\n    yield \"resource\"\n    print(\"close resource on shutdown app\")\n\n@resource\ndef sync_resource_with_close():\n   print(\"open sync resource\")\n   yield \"resource\"\n   print(\"close resource on shutdown app\")\n\n```\n\n2. Resource with closing has contextmanager support is such way:\n\n```python\nfrom contextlib import asynccontextmanager, contextmanager\n\n@resource\n@asynccontextmanager\nasync def async_context_manager():\n   print(\"open async context manager depends\")\n   yield \"resource\"\n   print(\"close async context manager\")\n\n@resource\n@contextmanager\nasync def sync_context_manager():\n   print(\"open sync context manager depends\")\n   yield \"resource\"\n   print(\"close sync context manager depends\")\n\n```\n\nThe resource will open a context manager and return the resource that was used throughout the lifecycle of the application. Upon shutdown, the context manager will be closed.\n\n3. Resource without close logic, it's just like singletone\n\n```python\n\n@resource\nasync def async_resource():\n    await asyncio.sleep(2)\n    return \"resource\"\n\n@resource\ndef sync_resource():\n   return \"resource\"\n```\n\n\n### Limitations\n\n1. Callable classes has not support yet\n2. Does not support functions with *args and **kwargs in arguments\n3. Decorated functions also may not work correctly\n\n\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.2.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3759e617544afd9fe4f19b79d40cae1fcd519a0ad1549874cc8a3ac0f002f724",
                "md5": "6fdd1b47ac8b7ce4d1d3b5450b11c73c",
                "sha256": "402c49c94323b300c16ce619818c7d9c7712c336477839ccee53cb266c014f85"
            },
            "downloads": -1,
            "filename": "fastapi_depends_extension-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6fdd1b47ac8b7ce4d1d3b5450b11c73c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 3686,
            "upload_time": "2024-10-01T07:26:24",
            "upload_time_iso_8601": "2024-10-01T07:26:24.078732Z",
            "url": "https://files.pythonhosted.org/packages/37/59/e617544afd9fe4f19b79d40cae1fcd519a0ad1549874cc8a3ac0f002f724/fastapi_depends_extension-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "22ab1638ba3b2efbb0e5ff016b9d31cb56a0a1c08e0d030783666e27fe0b0a39",
                "md5": "4a3f4ee8220d930112bdf49b26495f1f",
                "sha256": "50ee7a8d6cc552a8fc79d419627bdf78d03ed2255de0addaf2bcbae972a86401"
            },
            "downloads": -1,
            "filename": "fastapi_depends_extension-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4a3f4ee8220d930112bdf49b26495f1f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 3091,
            "upload_time": "2024-10-01T07:26:25",
            "upload_time_iso_8601": "2024-10-01T07:26:25.590843Z",
            "url": "https://files.pythonhosted.org/packages/22/ab/1638ba3b2efbb0e5ff016b9d31cb56a0a1c08e0d030783666e27fe0b0a39/fastapi_depends_extension-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-01 07:26:25",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "fastapi-depends-extension"
}
        
Elapsed time: 1.09081s