I am new to FastAPI and Python. I need to get all the routes on my root path and show it to the user. However, I could not find a way to get all the paths recursively. The API is versioned with the help of VersionedFastAPI
and the current code does not give the path inside version; it just returns generic ones.
FastAPI backend:
app = FastAPI()
router = APIRouter(
tags=["utilities"]
)
@router.get("/")
def read_root(request: Request):
url_list = [
route.path
for route in request.app.routes
]
return { "endpoints": set(url_list) }
@app.get('/foo')
@version(1)
def foo():
return "foo V1"
@app.get('/foo')
@version(2)
def foo():
return "foo V2"
app = VersionedFastAPI(app, enable_latest=True, version_format='{major}', prefix_format='/v{major}')
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
app.include_router(router)
Code for getting the path list found under \
route
url_list = [
route.path
for route in request.app.routes
]
return { "endpoints": set(url_list) }
This returns only:
["/v1/openapi.json","/v2/docs","/openapi.json","/v2/openapi.json","/v2","/","/redoc","/v1","/docs","/docs/oauth2-redirect","/v1/docs","/latest"]
However /foo
end point is missing. Any clue on this will help.