I have configured a dependencies.py
where I'm injecting a set of dependencies to different services by using python's binder.bind(my_config)
. The goal is being able to easily inject those services to each endpoint of my API. The problem arises when I pass that service as an argument to my endpoint , after having injected that service via its name. So:
import inject
from fastapi import APIRouter, HTTPException, Request, Depends
from src.services.chords import ChordsService
router = APIRouter(prefix="/chords")
@router.get("")
@inject.params(chords_service=ChordsService)
def get_chords(chords_service: ChordsService, req: Request, key: str, suffix: str = None, instrument: str = None):
params = dict(req.query_params)
return chords_service.get(params)
This does not work. I've tried changing the order of get_chords
' arguments. All I'm getting is different errors, but the one that appears the most is the following:
ChordsService is not a valid pydantic field type
I've read a bit about the use of pydantic in FastAPI and I see why I get this error, but I'm stuck at trying to inject those services. Is there a way to do it? Thanks!