treethought / flask-assistant

Framework for Building Virtual Assistants with Dialogflow and python
Apache License 2.0
380 stars 100 forks source link

basic support for Dependency Injection #135

Open jose-carmona opened 4 years ago

jose-carmona commented 4 years ago

Support for Dependency Injection based on Injector.

Example

Provider:

class AppModule(Module):
    @provider
    @singleton
    def provide_str(self) -> str:
        return 'TEST INJECTED'

    @provider
    @singleton
    def provide_int(self) -> int:
        return 42

Init:

app = Flask(__name__)
module = AppModule()
injector = Injector([module])
assist = Assistant(app, project_id="test-project-id", injector=injector)

Dependency Injection in action decorator:

@assist.context("context_1")
@assist.action("intent_with_context_injects_params")
@inject
def intent_with_context_injects_params(speech: str, p1, p2, i: int):
    return ask("context_1:" +speech + ":" + p1 + ":" + p2 + ":" +str(i))