-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
44 lines (36 loc) · 1.29 KB
/
events.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
var http = require('https');
var getEventsByCenter = function (centreId, callback) {
requestEvents(process.env["WF_EVENTS_API_ENDPOINT"] + "?api_key=" + process.env["WF_API_KEY"], "¢re_id=" + centreId + "&fields=name%2Cdescription%2Clocations&per_page=3&status=active", function(err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
var requestEvents = function (endpoint, queryString, callback) {
http.get(endpoint + queryString, function (res) {
var responseString = '';
console.log('Status Code: ' + res.statusCode);
if (res.statusCode != 200) {
callback(new Error("Non 200 Response"));
}
res.on('data', function (data) {
responseString += data;
});
res.on('end', function () {
var responseObject = JSON.parse(responseString);
if (responseObject.error) {
console.log("NOAA error: " + responseObject.error.message);
callback(new Error(responseObject.error.message));
} else {
callback(null, responseObject);
}
});
}).on('error', function (e) {
console.log("Communications error: " + e.message);
callback(new Error(e.message));
});
}
module.exports = {
getEventsByCenter : getEventsByCenter
}