forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
61 lines (55 loc) · 2.02 KB
/
Copy pathapi.js
File metadata and controls
61 lines (55 loc) · 2.02 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
// Here we put all the code that handles the interaction with the API
// We will use two different API calls ["weather", "forecast"].
// This function takes the parameters city and country and tries to fetch the weather data.
const fetchWeatherReport = (reportType, city, country) => {
// Here we set the parameters for our API request.
const params = {
// we are grabbing the apiKey from our weatherApp object
APPID: weatherApp.apiKey,
q: `${city},${country}`,
units: weatherApp.units
};
// We convert our parameters into a string
const queryString = buildQueryString(params);
const apiCollection = ["weather","forecast"].includes(reportType) ? reportType : false;
if (!apiCollection) {
console.log("Incorrect report type");
return Promise.reject("Incorrect report type");
}
// We put together the apiUrl, the apiCollection and the queryString to form the complete fetch url
const url = `${weatherApp.apiUrl}${apiCollection}?${queryString}`;
// We return the fetch call. By doing this fetchWeather will first return a promise and later the data.
return fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
return data;
})
.catch((error) => {
console.error("Fetching data failed", error);
return false;
});
};
const fetchWeatherData = async (city, country) => {
// We run both fetch calls concurrently and wait for them to both complete
const [weatherData, forecastData] = await Promise.all([
fetchWeatherReport("weather", city, country),
fetchWeatherReport("forecast", city, country)
]);
if (!weatherData || !forecastData) {
console.error("Failed fetching data");
return false;
}
// once completed we return an object consisting of city, country, current, forecast
return {
city: city,
country: country,
timezone: weatherData.timezone,
current: weatherData,
forecast: forecastData
}
};