INNER CODE UNIT · Python
fetch_weather_api
datawhalechina/wow-fullstack · tutorial/FastAPI/第6章-FastAPI MCP/main.py:203
def fetch_weather_api(endpoint: str, params: Dict[str, Any]):
# 获取有效的JWT令牌
token = get_valid_token()
headers = {
'Authorization': f'Bearer {token}',
'Accept-Encoding': 'gzip' # 我们请求gzip压缩
}
url = f"https://{WEATHER_API_HOST}/{endpoint}"
try:
response = requests.get(url, headers=headers, params=params)
# 检查响应状态
if response.status_code != 200:
# 如果是401或403,尝试刷新令牌并重试
if response.status_code in [401, 403]: