-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
56 lines (48 loc) · 1.37 KB
/
weather.js
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
/* cSpell:disable */
var rp = require('request-promise');
function auth (context) {
var header = '';
header = context.headers['authorization'];
if (header === undefined) {
return 'User not authorized!';
}
var tokenDiff = 0;
var token = Buffer.from(context.headers['authorization'], 'base64').toString('ascii');
var splitToken = token.split('-');
var tokenDate = new Date(parseInt(splitToken[0]) * 1000);
var serverDate = new Date();
tokenDiff = (serverDate - tokenDate) / 1000;
if (isNaN(tokenDiff) || tokenDiff > 600) {
return 'Authorization timed out!';
}
return 'authorized';
}
function getWeather (context, callback) {
var lat = context.data.lat;
var lon = context.data.lon;
var geonames = context.secrets.GEONAME;
var conditionsURL = 'http://api.geonames.org/findNearByWeatherJSON?lat=' + lat + '&lng=' + lon + '&username=' + geonames;
var getConditions = {
uri: conditionsURL,
json: true
};
console.log(conditionsURL);
rp(getConditions)
.then(function (response) {
console.log(response);
callback(null, response);
})
.catch(function (err) {
callback(err);
});
}
// lint spacer
module.exports =
function (context, callback) {
var isAuth = auth(context);
console.log(isAuth);
if (isAuth !== 'authorized') {
callback(null, isAuth);
}
getWeather(context, callback);
};