-
Notifications
You must be signed in to change notification settings - Fork 0
/
IFapi.js
56 lines (49 loc) · 1.31 KB
/
IFapi.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
var http = require('http');
var apiKey;
var apiBaseUrl = "http://infinite-flight-public-api.cloudapp.net/v1/";
var apiEndPoints = {
"getSessionsInfo" : "GetSessionsInfo.aspx",
"Flights": "Flights.aspx",
"FlightDetails": "FlightDetails.aspx",
"GetATCFacilities": "GetATCFacilities.aspx",
"UserDetails": "UserDetails.aspx"
};
var getSessionsInfoUrl = "GetSessionsInfo.aspx";
exports.initialize = function(k) {
apiKey = k;
}
exports.showKey = function() {
return apiKey;
}
exports.callAPI = function(endpoint, params, internalparams, callback, err) {
if (apiKey == undefined) {
err("You need to init the api key first!");
return;
}
if (apiEndPoints[endpoint] == undefined) {
err("Api endpoint: " + endpoint + " doesn't exist...");
return;
}
if (params == undefined) {
err("Missing params!");
return;
}
if (internalparams == undefined) {
err("Missing internal params!");
return;
}
http.get(apiBaseUrl + apiEndPoints[endpoint] + "?apikey=" + apiKey + params, function(res) {
var body = '';
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
var parsed = JSON.parse(body);
callback(parsed, internalparams);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
err(e.message);
return;
});
}