导入
当前天气状况
学习如何获取当前天气状况数据。
1http://dataservice.accuweather.com/currentconditions/v1/{locationKey}
1http://dataservice.accuweather.com/currentconditions/v1/topcities/{group}
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| API_KEY | 字符串 | 必需 | 这是我们的API密钥。这是一个查询参数。 |
| locationKey | 整数 | 必需 | 这是位置的键。仅用于特定位置端点的路径参数。 |
| group | 整数 | 必需 | 这是城市的数量。允许的值为50、100和150。仅用于热门城市端点的路径参数。 |
| language | 字符串 | 可选 | 返回结果的语言。默认值为 en-us。这是一个查询参数。 |
| details | 布尔值 | 可选 | 设置为 true 以在响应中包含完整详情,否则设置为 false。默认值为 false。仅用于特定位置端点的查询参数。 |
1// 导入库 2import fetch from "node-fetch"; 3 4// 定义API密钥 5const API_KEY = '{{API_KEY}}'; 6 7// 定义路径参数 8const locationKey = 351409; 9const group = 50; 10 11// 定义端点URL 12const urlSpecificLoc = new URL(`http://dataservice.accuweather.com/currentconditions/v1/${locationKey.toString()}`); 13const urlTopCities = new URL(`http://dataservice.accuweather.com/currentconditions/v1/topcities/${group.toString()}`); 14 15// 定义头部参数 16const headerParameters = { 17 contentType: 'application/json', 18}; 19 20// 定义查询参数 21const queryParamsSpecificLoc = new URLSearchParams({ 22 apikey: API_KEY, 23}); 24 25const queryParamsTopCities = new URLSearchParams({ 26 apikey: API_KEY, 27}); 28 29// 设置API调用选项 30const options = { 31 method: 'GET', 32 headers: headerParameters, 33}; 34 35// 函数:发起API调用 36async function getConditions(endpointUrl, queryParameters) { 37 try { 38 endpointUrl.search = queryParameters; 39 const response = await fetch(endpointUrl, options); 40 41 // 自定义函数:打印API响应 42 printResponse(response); 43 } catch (error) { 44 // 自定义函数:打印错误信息 45 printError(error); 46 } 47} 48 49async function getCurrentConditions() { 50 await getConditions(urlSpecificLoc, queryParamsSpecificLoc); 51 await getConditions(urlTopCities, queryParamsTopCities); 52} 53 54// 调用函数发起API调用 55getCurrentConditions();
特定位置端点
| 参数 | 类型 | 描述 |
|---|---|---|
| HasPrecipitation | 布尔值 | 表示是否有降水。 |
| IsDayTime | 布尔值 | 表示是否为白天。 |
| Temperature | 对象 | 温度值,包含华氏度和摄氏度两种单位(Imperial / Metric)。 |
| RealFeelTemperature | 对象 | “体感温度”,单位为华氏度。 |
| Wind | 对象 | 包含风速和风向信息。 |
| RelativeHumidity | 整数 | 湿度值。 |
| UVIndex | 整数 | 紫外线辐射强度指标。该值可能为空。 |
| Visibility | 对象 | 包含能见度信息。 |
| Link | 字符串 | AccuWeather网站上详细天气预报的链接。 |
| MobileLink | 字符串 | AccuWeather移动网站上详细天气预报的链接。 |
热门城市端点
| 参数 | 类型 | 描述 |
|---|---|---|
| Key | 字符串 | 城市的唯一位置键。 |
| LocalizedName | 字符串 | 以请求语言显示的位置名称,默认语言为 en-us。 |
| Country | 对象 | 包含城市所属国家的信息,包括国家代码、本地化名称和英文名称。 |
| Timezone | 对象 | 包含城市所在时区的信息。 |
| Geoposition | 对象 | 包含城市地理坐标信息。 |
| IsDayTime | 布尔值 | 表示是否为白天。 |
| Temperature | 对象 | 提供华氏度和摄氏度的温度值。 |
| HasPrecipitation | 布尔值 | 表示是否有降水。 |
| Link | 字符串 | AccuWeather网站上详细天气预报的链接。 |
| MobileLink | 字符串 | AccuWeather移动网站上详细天气预报的链接。 |