Skip to main content

aiomisc-dependency

Dependency injection plugin for aiomisc built with aiodine library and supports pytest fixture style dependency injection.

Installation

Installing from PyPI:

pip3 install aiomisc aiomisc-dependency

How to Use

Register Dependency

To register a dependency, you can use the aiomisc_dependency.dependency decorator.

from aiomisc_dependency import dependency

@dependency
async def pg_engine():
    pg_engine = await create_engine(dsn=pg_url)
    yield pg_engine
    pg_engine.close()
    await pg_engine.wait_closed()

As you can see, a dependency can be an async generator function. Code after yield will be executed on teardown to correctly close the dependency.

Coroutine functions, non-async functions, and generators are also supported.

Use Dependency

To use a dependency, you need to add its name to the __dependencies__ property for every service that depends on it. Specified dependencies will be injected as the service's attributes on entrypoint startup. If you need to map the dependency with a different name, use __dependencies_map__.

from contextlib import suppress
from types import MappingProxyType

import aiohttp
from aiomisc.service.aiohttp import AIOHTTPService

class HealthcheckService(AIOHTTPService):

    __dependencies__ = ('pg_engine',)

    async def create_application(self):
        app = aiohttp.web.Application()
        app.add_routes([aiohttp.web.get('/ping', self.healthcheck_handler)])
        return app

    async def healthcheck_handler(self, request):
        pg_status = False
        with suppress(Exception):
            async with self.pg_engine.acquire() as conn:
                await conn.execute('SELECT 1')
                pg_status = True

        return aiohttp.web.json_response(
            {'db': pg_status},
            status=(200 if pg_status else 500),
        )


class RESTService(AIOHTTPService):

    __dependencies__ = ('pg_engine',)

    ...

class AnotherRESTService(AIOHTTPService):

    __dependencies_map__ = MappingProxyType({'pg_engine': 'engine'})

    ...

If any required dependency is not found on entrypoint startup, a RuntimeError will be raised.

You can set a dependency manually by adding it to the keyword arguments on service creation. This can be convenient in tests.

from unittest import Mock

def test_rest_service():
    pg_engine_mock = Mock()
    service = RESTService(pg_engine=pg_engine_mock)
    ...

Dependencies for Dependencies

You can use dependencies as arguments for other dependencies. Arguments will be injected automatically.

@dependency
async def pg_connection(pg_engine):
    async with pg_engine.acquire() as conn:
        yield conn

loop Built-in Dependency

The built-in loop dependency can be used if your dependency requires an event loop instance.

import aioredis

@dependency
async def redis_pool(loop):
    pool = aioredis.create_pool(redis_url, loop=loop)
    yield pool
    pool.close()
    await pool.wait_closed()

License

MIT

Metadata

Release files for aiomisc-dependency 0.1.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiomisc-dependency 0.1.21
File Size Uploaded
aiomisc_dependency-0.1.21.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiomisc-dependency 0.1.21
File Interpreter ABI Platform
aiomisc_dependency-0.1.21-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / aiomisc_dependency-0.1.21.tar.gz

Download URL aiomisc_dependency-0.1.21.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
da84806db4d4cd33d6320b0a90f899056112f0e875cb7625c47dc84cd0ec5d60
BLAKE2b-256 checksum
How to use checksums
d86404327fb05aa5df8ef5bcfd067d93ce36f2fc2e5313f7631b19c82ab497d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.11.7 Darwin/24.3.0

Release files / aiomisc_dependency-0.1.21-py3-none-any.whl

Download URL aiomisc_dependency-0.1.21-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b01d61daf743b2054281f147352a9139e71945bdbf47e417c50fab143f8cb1d
BLAKE2b-256 checksum
How to use checksums
b42ab4c7291c1862ce0e327c6180546cb5a9436a57b4197836f0884e9af76129
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.11.7 Darwin/24.3.0

Release history Release notifications | RSS feed

This release

0.1.21 This release

2 release files

0.1.20

2 release files

0.1.7

2 release files

0.1.4

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page