forked from chendonming/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.js
More file actions
87 lines (83 loc) · 2.04 KB
/
weather.js
File metadata and controls
87 lines (83 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const axios = require("axios");
const vscode = require("vscode");
// Get configuration dynamically
function getConfig() {
const config = vscode.workspace.getConfiguration("weather");
let apiUrl = config.get("apiUrl") || "https://devapi.qweather.com";
// Ensure API URL has protocol
if (
apiUrl &&
!apiUrl.startsWith("http://") &&
!apiUrl.startsWith("https://")
) {
apiUrl = "https://" + apiUrl;
}
return {
key: config.get("key"),
apiUrl: apiUrl,
language: config.get("language") || "en",
};
}
module.exports = {
/**
* Get geographic location
* @param {string} location
*/
getLocation(location) {
const config = getConfig();
return axios({
url: "https://geoapi.heweather.net/v2/city/lookup",
method: "get",
params: {
location,
key: config.key,
},
});
},
/**
* Get current weather
*/
nowWeather(location) {
const config = getConfig();
return axios({
url: `${config.apiUrl}/v7/weather/now`,
method: "get",
params: {
location,
key: config.key,
lang: config.language,
},
});
},
/**
* Get weather forecast
*/
forecast(location) {
const config = getConfig();
return axios({
url: `${config.apiUrl}/v7/weather/3d`,
method: "get",
params: {
location,
key: config.key,
lang: config.language,
},
});
},
/**
* Get life index
*/
getIndices(location) {
const config = getConfig();
return axios({
url: `${config.apiUrl}/v7/indices/1d`,
method: "get",
params: {
location,
key: config.key,
type: "8",
lang: config.language,
},
});
},
};