Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/troyan-dy/fastapi-depends
You can use your FastAPI dependencies not only in FastAPI applications
https://github.com/troyan-dy/fastapi-depends
async async-await asyncio fastapi lib library pip pydantic python python-type python-types python-typing python3
Last synced: 25 days ago
JSON representation
You can use your FastAPI dependencies not only in FastAPI applications
- Host: GitHub
- URL: https://github.com/troyan-dy/fastapi-depends
- Owner: troyan-dy
- License: mit
- Created: 2022-01-06T07:23:11.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-09T22:50:30.000Z (almost 3 years ago)
- Last Synced: 2024-11-15T04:28:07.588Z (about 1 month ago)
- Topics: async, async-await, asyncio, fastapi, lib, library, pip, pydantic, python, python-type, python-types, python-typing, python3
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-depends
You can use your FastAPI dependencies not only in FastAPI applications
[PYPI](https://pypi.org/project/fastapi-depends/)
## Install
pip
```bash
pip install fastapi-depends
```poetry
```bash
poetry add fastapi-depends
```## Usage
### Simple
Simple example for calling dependencies, without binding to `fastapi.Request` object
```python
import asynciofrom fastapi import Depends
from fastapi_depends import inject
async def str_dep():
return "str_dep"@inject
async def main(pos_value: str, regular_value: str, str_dep=Depends(str_dep)):
return (pos_value, regular_value, str_dep)if __name__ == "__main__":
result = asyncio.run(main("pos_value", regular_value="regular_value"))
print(f"{result=}")```
### With your application
Example of getting a `fastapi.Request` object with one property `app`
```python
import asynciofrom fastapi import Depends, Request
from fastapi_depends import DepContainer
container = DepContainer()
async def str_dep(request: Request):
return request.app.app_value@container.inject
async def main(pos_value: str, regular_value: str, str_dep=Depends(str_dep)):
return (pos_value, regular_value, str_dep)class MyApp:
def __init__(self, app_value):
self.app_value = app_valueapp = MyApp(app_value="app_value")
container.setup_app(app)if __name__ == "__main__":
result = asyncio.run(main("pos_value", regular_value="regular_value"))
print(f"{result=}")```
### Register the method in your application
An example similar to the previous one, but only for a method you can specify a key with which it will be written in `DepContainer.callback_map`
```python
import asynciofrom fastapi import Depends, Request
from fastapi_depends import DepContainer
async def str_dep(request: Request):
return request.app.app_valuecontainer = DepContainer()
@container.register("my_key")
async def main(pos_value: str, regular_value: str, str_dep=Depends(str_dep)):
return (pos_value, regular_value, str_dep)class MyApp:
def __init__(self, app_value):
self.app_value = app_valueapp = MyApp(app_value="app_value")
container.setup_app(app)if __name__ == "__main__":
result = asyncio.run(container.callback_map["my_key"]("pos_value", regular_value="regular_value"))
print(f"{result=}")```