Open
Description
Trying to inject a service from declarative container into sanic handler - but service never get's injected.
container example:
class AppContainer(containers.DeclarativeContainer):
# https://python-dependency-injector.ets-labs.org/providers/inject_self.html
__self__ = providers.Self()
# This will wire automatically - once you instantiate the container
wiring_config = containers.WiringConfiguration(
packages=[
"xasperuze.routers",
],
)
# Add your container initialization code below
# ========================================================================
# Application config
appconfig: providers.Configuration = providers.Configuration()
# jinja service
svc_templater: Singleton[Environment] = providers.Singleton(
Jinjia2Factory().get_svc,
app_path=appconfig.app_path,
THEME_REL_PATH=appconfig.THEME_REL_PATH,
SITE_THEME_NAME=appconfig.SITE_THEME_NAME,
ENV_NAME=appconfig.ENV_NAME,
# Injecting the vite loader service as a provider, so we only resolve it when needed.
# Otherwise "providers.Self()" wont be available
svc_vite_loader=svc_vite_loader.provider,
utils=utils,
)
Injecting service into route in sanic:
from dependency_injector.wiring import Provide, inject
bp = Blueprint("my_page3")
def get_blueprint() -> Blueprint:
@bp.route("/test/route<path:path>", methods=["GET"])
@inject
async def handler_rd(
request,
path: str,
svc_templater: Environment = Provide[AppContainer.svc_templater],
):
#This doesnt work
templater_injected = svc_templater
#This works
templater_from_ctx: Environment = request.ctx.container.svc_templater()
return text(f"RD - {path}")
I can pull my service from a container I share via sanic context (ex: templater_from_ctx), but the one I try to inject via Provide[AppContainer.svc_templater] - always comes back as Provider object.
Metadata
Metadata
Assignees
Labels
No labels