injector icon indicating copy to clipboard operation
injector copied to clipboard

FastAPI support

Open Trinkes opened this issue 2 years ago • 2 comments

Is there any way to use Injector library with FastAPI endpoints? The ideia would be to replace completely their Depends feature.

Trinkes avatar Aug 23 '23 11:08 Trinkes

I've used fastapi-injector and it works fairly well. https://pypi.org/project/fastapi-injector/

jack-michaud avatar Sep 03 '23 19:09 jack-michaud

Hey @Trinkes, sadly I can't say I know anything about this, I never had a chance to use FastAPI.

In general my answer would be that some custom integration would be required which usually boils down to:

  • Implementing a custom Injector Scope
  • Implementing some code that goes around and replaces all callables you want to inject into with something like https://github.com/python-injector/flask_injector/blob/004b9b52727e9699d23168a600c490ee5de7a395/flask_injector/init.py#L86 so that whenever they're called the dependency injection happens transparently and extra parameters are provided

Sadly no good how-to guide explaining that.

@jack-michaud mentioned an interesting integration project which I didn't know about – thank you – maybe that'll help you

jstasiak avatar Sep 05 '23 23:09 jstasiak