如何在 FastAPI 上调用另一条路径?
How do I call another path on FastAPI?
我正在开发一个 API 的 return 本地文件夹中的一些文件,以模拟我们为开发人员环境提供的系统。
系统的大部分工作是通过放置识别此人的代码并 returning 他的文件来工作的。但是,这个系统的一个路径有一个独特的行为:它使用 POST 方法(请求正文包含 Id 代码),我正在努力让它工作。
这是我当前的代码:
import json
from pathlib import Path
import yaml
from fastapi import FastAPI
from pydantic.main import BaseModel
app = FastAPI()
class RequestModel(BaseModel):
assetId: str
@app.get("/{group}/{service}/{assetId}")
async def return_json(group: str, service: str, assetId: str):
with open("application-dev.yml", "r") as config_file:
output_dir = yaml.load(config_file)['path']
path = Path(output_dir + f"{group}/{service}/")
file = [f for f in path.iterdir() if f.stem == assetId][0]
if file.exists():
with file.open() as target_file:
return json.load(target_file)
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
return return_json("DataService", "ServiceProtocol", request.msisdn)
我想从 API 本身调用另一个 path/function 到 return 所需的值,但我收到此错误:
ValueError: [TypeError("'coroutine' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
你的 return_json
函数 returns 一个 coroutine (注意其定义中的 async
关键字)。请注意,@app.post(...)
需要一个 returns 数据的协程,而不是 returns 另一个协程的协程。最直接的变化是:
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
# unpack return_json into a non-coroutine object before returning it
return await return_json("DataService", "ServiceProtocol", request.msisdn)
@app.post("/DataService/ServiceProtocol")
def return_post_path(request: RequestModel):
# return the coroutine directly because we're inside a normal function
return return_json("DataService", "ServiceProtocol", request.msisdn)
我正在开发一个 API 的 return 本地文件夹中的一些文件,以模拟我们为开发人员环境提供的系统。
系统的大部分工作是通过放置识别此人的代码并 returning 他的文件来工作的。但是,这个系统的一个路径有一个独特的行为:它使用 POST 方法(请求正文包含 Id 代码),我正在努力让它工作。
这是我当前的代码:
import json
from pathlib import Path
import yaml
from fastapi import FastAPI
from pydantic.main import BaseModel
app = FastAPI()
class RequestModel(BaseModel):
assetId: str
@app.get("/{group}/{service}/{assetId}")
async def return_json(group: str, service: str, assetId: str):
with open("application-dev.yml", "r") as config_file:
output_dir = yaml.load(config_file)['path']
path = Path(output_dir + f"{group}/{service}/")
file = [f for f in path.iterdir() if f.stem == assetId][0]
if file.exists():
with file.open() as target_file:
return json.load(target_file)
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
return return_json("DataService", "ServiceProtocol", request.msisdn)
我想从 API 本身调用另一个 path/function 到 return 所需的值,但我收到此错误:
ValueError: [TypeError("'coroutine' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
你的 return_json
函数 returns 一个 coroutine (注意其定义中的 async
关键字)。请注意,@app.post(...)
需要一个 returns 数据的协程,而不是 returns 另一个协程的协程。最直接的变化是:
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
# unpack return_json into a non-coroutine object before returning it
return await return_json("DataService", "ServiceProtocol", request.msisdn)
@app.post("/DataService/ServiceProtocol")
def return_post_path(request: RequestModel):
# return the coroutine directly because we're inside a normal function
return return_json("DataService", "ServiceProtocol", request.msisdn)