INNER CODE UNIT · Python

get_weather_by_city

datawhalechina/wow-fullstack · tutorial/FastAPI/第6章-FastAPI MCP/main.py:287

async def get_weather_by_city(
    query: WeatherQuery
):
    """
    一站式查询城市天气
    
    - 只需提供城市名
    - 自动查询位置ID并获取天气
    """
    try:
        # 先查询城市ID
        location_data = fetch_weather_api("geo/v2/city/lookup", {"location": query.city})
        
        # 检查是否找到城市
        if location_data.get("code") != "200" or not location_data.get("location"):
            raise HTTPException(status_code=404, detail=f"找不到城市: {query.city}")
        
        # 获取第一个匹配城市的ID

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…