I am new to flask and python. I want to implement a Dependency injection container and access the dependencies inside different modules. My first try looks something like:
class AppModule(Module):
def __init__(self, app):
self.app = app
"""Configure the application."""
def configure(self, binder):
client = self.configure_cosmos_client()
binder.bind(CosmosClient, to=client, scope=singleton)
binder.bind(Dao, to=Dao, scope=singleton)
def configure_cosmos_client(self) -> CosmosClient:
return CosmosClient(
url_connection=self.app.config.get('ENDPOINT'),
auth={'masterKey': self.app.config.get('PRIMARYKEY')}
)
app = Flask(__name__)
injector = Injector([AppModule(app)])
FlaskInjector(app=app, injector=injector)
app.run()
and further inside a module, I want to get the CosmosClient dependency something like:
class Dao:
cosmos_client = None
def __init__(self):
self.cosmos_client = DI.get(CosmosClient)
Is there any way to achieve this? Please note "DI.get" is just an example since I could not find how to access these dependencies apart from injecting the dependencies into the route.