本帖最后由 realpc 于 2023-12-16 16:46 编辑
[Python] 纯文本查看 复制代码 from fastapi import FastAPI
import requests
app = FastAPI()
def get_weather_forecast(city: str):
# 定义api_key
api_key = "your_api_key"
# 定义base_url
base_url = "http://api.openweathermap.org/data/2.5/forecast?q={}&appid={}"
# 拼接url
complete_url = base_url.format(city, api_key)
# 发送请求
response = requests.get(complete_url)
# 获取响应数据
data = response.json()
# 返回数据
return data["list"]
@app.get("/weather/{city}")
async def weather_forecast(city: str):
# 调用get_weather_forecast函数
forecast = get_weather_forecast(city)
# 返回数据
return {"city": city, "forecast": forecast}
@app.get("/api")
async def api():
# 定义对象数据
object_data = {
"提示": "彩色代码:初检通过,黑色:初检错误,**:初检有多发性错误。",
"数值型": 123.45,
"文本型": "一段文本",
"逻辑型": True,
"空": None,
"数组": ["数组成员1", 2015],
"未知类型": "127.0.0.1"
}
# 定义错误示范数据
error_demo_data = {
"一般错误": "文本不用引号"
}
# 定义结果
result = {
"对象": object_data,
"错误示范": error_demo_data
}
# 返回结果
return resultfrom fastapi import FastAPI
|