我有我的 FastAPI 应用程序定义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())
])
在__init__.py
中,我定义了路线
from fastapi import APIRouter
api_router = APIRouter()
api_router.include_router(impl_controller.router, prefix="/impl/data",
tags=["APIs for Impl Management"])
在impl_controller.py
,我已经定义了这样的路线
@router.get('{id}/get_all')
def hello_world():
return {"msg": "Hello World"}
@router.get('{id}/get_last')
def test():
return {"msg": "test"}
在中间件中,我试图获取请求路由而不是 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
]
我期待的结果是:{id}/get_all
第一个请求和{id}/get_last
第二个请求。
我能够获取所有路径的列表,url_list
但我想要特定路径的路径request
此处提供的尝试解决方案:https ://github.com/tiangolo/fastapi/issues/486也不适用于我