Injector support in nameko
Project description
injector based dependency injection mechanism for the nameko services. Project is similar to flask-injector.
Problem
Nameko provides dependency injection mechanism, built-in in the framework. It works in many cases but there are limitations:
All the dependencies are injected regardless whether they are used in the entrypoint. For instance, all the dependencies will be injected for /health HTTP entry point.
Dependencies cannot depend on each other.
Scope is an implementation detail. Frequency of the dependency creation depends on the DependencyProvider implementation.
Solution
The library provides an alternative dependency injection mechanism to the one that is bult in in nameko. There are several types of request scope that can be used out of the box without special injector modules declarations.
nameko_injector.core.ServiceConfig
from nameko.containers.ServiceContainer
from nameko.containers.WorkerContext
An example:
import json
from nameko_injector.core import NamekoInjector, ServiceConfig
from nameko.web.handlers import http
INJECTOR = NamekoInjector()
@INJECTOR.decorate_service
class Service:
name = 'service-name'
@http('GET', '/health')
def health(self, request):
return {'status': 'ok'}
@http('GET', '/config')
def view_config(self, request, config: ServiceConfig):
return json.dumps(config)
Development
tox
TODO
testing: Add the tests for RPC entrypoints
document testing with the library
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.