-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
38 lines (36 loc) · 1.43 KB
/
utils.js
File metadata and controls
38 lines (36 loc) · 1.43 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
import request from "postman-request";
import { WEATHERSTACK_SECRET, MAPBOX_SECRET } from "./secrets.js";
export const geocode = (place, callback) => {
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(
place
)}.json?access_token=${MAPBOX_SECRET}&limit=1`;
// If there is an error, then reponse is undefined and vice-versa
request({ url: url, json: true }, (error, response) => {
if (error) {
callback(`Unable to connect to location services.`, undefined);
} else if (response.body.features.length === 0) {
callback(`Unable to find place. Try another search!`, undefined);
} else {
callback(undefined, {
longitude: response.body.features[0].center[0],
latitude: response.body.features[0].center[1],
location: response.body.features[0].place_name,
});
}
});
};
export const forecast = (latitude, longitude, callback) => {
const url = `http://api.weatherstack.com/current?access_key=${WEATHERSTACK_SECRET}&query=${latitude},${longitude}`;
request({ url: url, json: true }, (error, response) => {
if (error) {
callback(`Unable to connect to weather services`, undefined);
} else if (response.body.error) {
callback(`Unable to find location`, undefined);
} else {
callback(undefined, {
temperature: response.body.current.temperature,
feelslike: response.body.current.feelslike,
});
}
});
};