I'm working on a FastAPI application, and I want to create multi-part paths. What I mean by this is I know how to create a path like this for all the REST methods:
/api/people/{person_id}
but what's a good way to create this:
/api/people/{person_id}/accounts/{account_id}
I could just keep adding routes in the "people" routes module to create the additional accounts paths, but I feel like there should be a separate "accounts" routes module that could be included in the "people" routes module, and I'm just missing something.
Am I over-thinking this?
CodePudding user response:
In addition to what I have mentioned in the comments, would something like this be of use?
from fastapi import FastAPI, APIRouter
app = FastAPI()
people_router = APIRouter(prefix='/people')
account_router = APIRouter(prefix='/{person_id}/accounts')
@people_router.get('/{person_id}')
def get_person_id(person_id: int) -> dict[str, int]:
return {'person_id': person_id}
@account_router.get('/{account_id}')
def get_account_id(person_id: int, account_id: int) -> dict[str, int]:
return {'person_id': person_id, 'account_id': account_id}
people_router.include_router(account_router)
app.include_router(people_router, prefix='/api')