https://github.com/chandr-andr/falcon-deps
Dependency Injector for Falcon Framework
https://github.com/chandr-andr/falcon-deps
async dependency-injection falcon falcon-api falcon-framework python3
Last synced: 9 months ago
JSON representation
Dependency Injector for Falcon Framework
- Host: GitHub
- URL: https://github.com/chandr-andr/falcon-deps
- Owner: chandr-andr
- Created: 2024-10-20T01:24:01.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-24T09:33:17.000Z (about 1 year ago)
- Last Synced: 2024-10-24T11:56:49.826Z (about 1 year ago)
- Topics: async, dependency-injection, falcon, falcon-api, falcon-framework, python3
- Language: Python
- Homepage:
- Size: 135 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Falcon-deps
Dependency injector for [Falcon Framework](https://github.com/falconry/falcon) based on [taskiq-dependencies](https://github.com/taskiq-python/taskiq-dependencies).
## Installation
Install with pip
```bash
pip install falcon-deps
```
Install with poetry
```bash
poetry add falcon-deps
```
## Usage
### Start Usage
It's simple as possible.
```python
from falcon_deps import InjectableResource
from falcon.asgi import App, Request, Response
from taskiq_dependencies import Depends
# Imagine we have a database pool.
async def db_pool(
# Retrieve request object from the actual request.
request: Request = Depends(),
) -> ConnectionPool:
return request.context._pool
class Resource(InjectableResource):
async def on_get(
self,
request: Request,
response: Response,
# Retrieve database pool as a dependency
db_pool: ConnctionPool = Depends(db_pool)
) -> None:
...
app = App()
app.add_route(
"/test",
Resource(),
)
```
### Advanced Usage
Falcon gives option to specify suffix for resource.
If you want to use suffix with `InjectableResource` you need to pass suffix to `InjectableResource` too.
```python
app.add_route(
"/test",
Resource(suffix="bob",),
suffix="bob",
)
```
If some of methods in Resource don't need dependency injection, it's possible to remove them from injection with `exclude_responder_from_inject`.
```python
app.add_route(
"/test",
Resource(
exclude_responder_from_inject={
# Remove on_get and on_post methods from injection.
"on_get",
"on_post",
},
),
)
```