-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (92 loc) · 3.13 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
/**
* index.js
* package main entry point
*/
const request = require('request-promise-native'),
parseString = require('xml2js').parseString,
stripNS = require('xml2js').processors.stripPrefix,
methods = require('./NRDPmethods');
/**
* The main class that will allow access to the RDG Knowledgbase Real Time Incident feeds and return the data in a JSON format
* This is a 2 step porcess where you first eed to log in to opbtain a token and then use that token to access the RTF's.
* To create an account for access register here: https://opendata.nationalrail.co.uk/
* For more information on the RTF's - https://wiki.openraildata.com/index.php/KnowledgeBase#RDG_Knowledgebase_APIs
*/
class RDGKnowledgebaseAPI {
/**
* Create an instance of the service by passing in a valid username and password that has been set up
* You will need to register here for an account - https://opendata.nationalrail.co.uk/
* @param {String} username a valid account username
* @param {*} password a valid account password
*/
constructor(username, password) {
this.username = username;
this.password = password;
this.authURL = " https://opendata.nationalrail.co.uk/authenticate";
this.apiURL = " https://opendata.nationalrail.co.uk/api/staticfeeds/4.0";
this.methods = methods;
}
/**
* This function will be available of logon was successful AND the time elpased since last logon was less that 1 hour
* as the tokens issued currently only have 1 hour of validity then you will need to re-login again
* @param {NRDPMethod} method The API method to call
*/
async callAPI(method) {
const body = await request({
simple: false,
method: 'GET',
url: `${this.apiURL}/${method}`,
headers: {
'X-Auth-Token' : this.result.token
}
});
return await this._parseResult(body);
}
/**
* This is the first step that MUST be carried out before accessing ANY of the RTF's
* Logging in generates an access token that is used for all the oter calls - if successful then the token will be valid for 1 hour
* @returns {Boolean} true or false to indicate if logon succeeds
*/
async logon() {
const body = await request({
simple: false,
method: 'POST',
url: this.authURL,
headers: {
'content-type' : "application/x-www-form-urlencoded"
},
form: {
username: this.username,
password: this.password
}
});
const result = JSON.parse(body);
if(result.error != null) {
this.result = null;
this.error = result.error;
return false;
}
this.error = null;
this.result = result;
return true;
}
// Private method to parse result to JSON
_parseResult(body) {
return new Promise((resolve, reject) => {
parseString(body, {
tagNameProcessors: [stripNS],
explicitArray : false,
ignoreAttrs : true
}, function(err, result){
if(!err){
const data = result;
resolve(data);
} else {
reject(err);
}
});
});
}
}
module.exports = RDGKnowledgebaseAPI;