是否可以将 Path 参数传递给 FastAPI 依赖函数?
Is it possible to pass Path arguments into FastAPI dependency functions?
是否有 FastAPI“依赖项”来解释 Path 参数。
我有很多形式的功能:
@app.post("/item/{item_id}/process", response_class=ProcessResponse)
async def process_item(item_id: UUID, session: UserSession = Depends(security.user_session)) -> ProcessResponse:
item = await get_item(client_id=session.client_id, item_id=item_id)
await item.process()
一遍又一遍,我需要传入 [多个] 参数来获取所需的项目,然后再对其进行操作。这是非常重复的,并且使代码非常冗长。我真正想做的是将 item
作为参数传递给方法。
理想情况下,我想使 get_item
成为依赖项或以某种方式将其嵌入到路由器中。这将大大减少重复的逻辑和过于冗长的函数参数。问题是客户端在路径中传递了一些关键参数。
是否可以将 Path 参数传递到依赖项中,或者可能在路由器中执行依赖项并传递结果?
FastAPI 依赖函数可以采用普通端点函数可以采用的任何参数。
因此,在普通端点中,您可以像这样定义路径参数:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id):
return {"item_id": item_id}
现在如果你想在依赖中使用那个参数,你可以简单地做:
from fastapi import Depends, FastAPI
app = FastAPI()
async def my_dependency_function(item_id: int):
return {"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(item_id: int, my_dependency: dict = Depends(my_dependency_function):
return my_dependency
参数将简单地传递给依赖函数(如果存在)。您还可以在依赖函数中使用 Path
和 Query
之类的东西来定义它们的来源。
它只会分析请求对象以提取这些值。
这是一个使用 FastAPI 中的 Path
函数的示例:
from fastapi import Depends, FastAPI, Path
app = FastAPI()
async def my_dependency_function(item_id: int = Path(...)):
return {"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(my_dependency: dict = Depends(my_dependency_function):
return my_dependency
至于你关心的将它实现为路由器中的依赖项,你可以在创建路由器时做这样的事情:
items_router = APIRouter(
prefix="/items",
tags=["items"],
dependencies=[Depends(my_dependency_function)],
)
或者您可以在应用程序上 运行 include_router
时执行此操作,例如:
app.include_router(
items_router,
prefix="/items",
dependencies=[Depends(my_dependency_function)],
)
有关依赖关系的更多信息和更多此类示例,请参阅 https://fastapi.tiangolo.com/tutorial/dependencies/
是否有 FastAPI“依赖项”来解释 Path 参数。 我有很多形式的功能:
@app.post("/item/{item_id}/process", response_class=ProcessResponse)
async def process_item(item_id: UUID, session: UserSession = Depends(security.user_session)) -> ProcessResponse:
item = await get_item(client_id=session.client_id, item_id=item_id)
await item.process()
一遍又一遍,我需要传入 [多个] 参数来获取所需的项目,然后再对其进行操作。这是非常重复的,并且使代码非常冗长。我真正想做的是将 item
作为参数传递给方法。
理想情况下,我想使 get_item
成为依赖项或以某种方式将其嵌入到路由器中。这将大大减少重复的逻辑和过于冗长的函数参数。问题是客户端在路径中传递了一些关键参数。
是否可以将 Path 参数传递到依赖项中,或者可能在路由器中执行依赖项并传递结果?
FastAPI 依赖函数可以采用普通端点函数可以采用的任何参数。
因此,在普通端点中,您可以像这样定义路径参数:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id):
return {"item_id": item_id}
现在如果你想在依赖中使用那个参数,你可以简单地做:
from fastapi import Depends, FastAPI
app = FastAPI()
async def my_dependency_function(item_id: int):
return {"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(item_id: int, my_dependency: dict = Depends(my_dependency_function):
return my_dependency
参数将简单地传递给依赖函数(如果存在)。您还可以在依赖函数中使用 Path
和 Query
之类的东西来定义它们的来源。
它只会分析请求对象以提取这些值。
这是一个使用 FastAPI 中的 Path
函数的示例:
from fastapi import Depends, FastAPI, Path
app = FastAPI()
async def my_dependency_function(item_id: int = Path(...)):
return {"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(my_dependency: dict = Depends(my_dependency_function):
return my_dependency
至于你关心的将它实现为路由器中的依赖项,你可以在创建路由器时做这样的事情:
items_router = APIRouter(
prefix="/items",
tags=["items"],
dependencies=[Depends(my_dependency_function)],
)
或者您可以在应用程序上 运行 include_router
时执行此操作,例如:
app.include_router(
items_router,
prefix="/items",
dependencies=[Depends(my_dependency_function)],
)
有关依赖关系的更多信息和更多此类示例,请参阅 https://fastapi.tiangolo.com/tutorial/dependencies/