I have my FastAPI app define in server.py
app = FastAPI(
debug=True, title="Microservice for APIs",
description="REST APIs",
version="0.0.1",
openapi_url="/v3/api-docs",
middleware=[
Middleware(AuthorizationMiddleware, authorizor=Auth())
])
In __init__.py
, I have routes defined
from fastapi import APIRouter
api_router = APIRouter()
api_router.include_router(impl_controller.router, prefix="/impl/data",
tags=["APIs for Impl Management"])
In impl_controller.py
, I have define routes like this
@router.get('{id}/get_all')
def hello_world():
return {"msg": "Hello World"}
@router.get('{id}/get_last')
def test():
return {"msg": "test"}
In the middleware, I'm trying to get request route and not the URL
def check_for_api_access(self, request: Request):
request_path = request.scope['path']
# route_path = request.scope['endpoint'] # This does not exists
url_list = [
{'path': route.path, 'name': route.name}
for route in request.app.routes
]
Result I'm expecting is: {id}/get_all
for 1st request and {id}/get_last
for 2nd request.
I'm able to get list of all paths in url_list
but I want route path for the specific request
Tried solution provided here: https://github.com/tiangolo/fastapi/issues/486 that also not working for me