Skip to main content

aiomisc-dependency - dependency injection in aiomisc

Project description

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

Installation

Installing from pypi:

pip3 install aiomisc aiomisc-dependency

How to use

Register dependency

To register dependency you can use 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 dependency can be 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 dependency you need to add it’s name to __dependencies__ property for every service which depends on it. Specified dependencies will be injected as service’s attributes on entrypoint startup.

from contextlib import suppress

import aiohttp
from aiomisc import Service
from aiomisc.service.aiohttp import AIOHTTPService

class HealthcheckService(Service):

    __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',)

    ...

If any required dependency won’t be found on entrypoint startup, RuntimeError will be raised.

You can set a dependency manually by adding it to kw arguments on service creation. This could 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 injected automatically.

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

loop built-in dependency

Built-in loop dependency can be used if your dependency requires 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

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

aiomisc_dependency-0.1.7.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

aiomisc_dependency-0.1.7-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file aiomisc_dependency-0.1.7.tar.gz.

File metadata

  • Download URL: aiomisc_dependency-0.1.7.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiomisc_dependency-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7a7e0b4b9ff288e58734b83135ef6ea31916d404e70970642f1ccc59eb6a5a09
MD5 cfa33128b7a5c88bd7521bd76a9ef267
BLAKE2b-256 249c0d25ca081d6f1487634af2557af7816d869d4359d4e584e48065a5e23d99

See more details on using hashes here.

File details

Details for the file aiomisc_dependency-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: aiomisc_dependency-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiomisc_dependency-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 1b1af44421c3e733ca03846e6362b5ae52b96a23f611dbfb60e46d7a12333b44
MD5 6f1b0b68cb5ab93b3594daf6b29f697e
BLAKE2b-256 7ab1ce892f8d65932215ccb57738c078535109ae5b501dc287998d8574cb374f

See more details on using hashes here.

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