Skip to main content

Extracted the dependency injection process from fastapi.

Project description

fastapi_di

Extracted the dependency injection process from fastapi. Dependency injection by fastapi_di is only available in the async environment.

Requirement

  • Python 3.8+

Installation

poetry install fastapi_di

Getting started

Dependency injection is done by decorating the function and calling do as follows.

import asyncio
from fastapi import Depends
from fastapi_di import DI

di = DI()


def get_db():
    yield {1: {"id": 1, "name": "bob", "memo": ""}}


@di.task()
async def update_user(db=Depends(get_db), *, user_id: int, memo: str):
    record = db[user_id]
    record["memo"] = memo
    return record


async def main():
    return await update_user.do(user_id=1, memo="test")


result = asyncio.run(main())
print(result)
# => {'id': 1, 'name': 'bob', 'memo': 'test'}}

warning

This library is in the experimental stage.

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_di-0.0.1.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

fastapi_di-0.0.1-py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 3

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