-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67e6646
commit 9ee0c1b
Showing
8 changed files
with
1,304 additions
and
1,280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
node_modules | ||
config.js | ||
node_modules | ||
config.js | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Owen Jenkins | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
MIT License | ||
|
||
Copyright (c) 2019 Owen Jenkins | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
# Railator | ||
National Rail Darwin API translator server | ||
|
||
## What & Why? | ||
This is a Node.JS HTTP server that serves as a translator between National Rail's Darwin API and JSON. Darwin is a SOAP XML API, | ||
which is... difficult, to say the least, to work with in JavaScript; this project aims to solve that problem by sitting in between | ||
client applications and Darwin to translate into JSON. | ||
|
||
## Install | ||
``` | ||
git clone [email protected]:ArtOfCode-/railator | ||
cd railator | ||
cp config.sample.js config.js | ||
``` | ||
|
||
You'll need to edit the values in `config.js`: pick the port you want the server to run on, and provide a valid access token for LDBSVWS, | ||
which you can obtain [here](http://openldbsv.nationalrail.co.uk/) (for now). Then: | ||
|
||
``` | ||
npm install | ||
npm start | ||
# Railator | ||
National Rail Darwin API translator server | ||
|
||
## What & Why? | ||
This is a Node.JS HTTP server that serves as a translator between National Rail's Darwin API and JSON. Darwin is a SOAP XML API, | ||
which is... difficult, to say the least, to work with in JavaScript; this project aims to solve that problem by sitting in between | ||
client applications and Darwin to translate into JSON. | ||
|
||
## Install | ||
``` | ||
git clone [email protected]:ArtOfCode-/railator | ||
cd railator | ||
cp config.sample.js config.js | ||
``` | ||
|
||
You'll need to edit the values in `config.js`: pick the port you want the server to run on, and provide a valid access token for LDBSVWS, | ||
which you can obtain [here](http://openldbsv.nationalrail.co.uk/) (for now). Then: | ||
|
||
``` | ||
npm install | ||
npm start | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,139 @@ | ||
const xmlbuilder = require('xmlbuilder'); | ||
const fetch = require('node-fetch'); | ||
const xmlParser = require('fast-xml-parser'); | ||
|
||
const methods = [ | ||
"GetArrBoardWithDetails", "GetArrDepBoardWithDetails", "GetArrivalDepartureBoardByCRS", "GetArrivalDepartureBoardByTIPLOC", | ||
"GetArrivalBoardByCRS", "GetArrivalBoardByTIPLOC", "GetDepartureBoardByCRS", "GetDepartureBoardByTIPLOC", "GetDepBoardWithDetails", | ||
"GetDisruptionList", "GetFastestDepartures", "GetFastestDeparturesWithDetails", "GetHistoricDepartureBoard", "GetHistoricServiceDetails", | ||
"GetHistoricTimeLine", "GetNextDepartures", "GetNextDeparturesWithDetails", "GetServiceDetailsByRID", "QueryHistoricServices", "QueryServices" | ||
]; | ||
|
||
const referenceMethods = ["GetReasonCode", "GetReasonCodeList", "GetSourceInstanceNames", "GetStationList", "GetTOCList"]; | ||
|
||
const apiBaseUrl = 'https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ldbsv12.asmx'; | ||
const apiActionBase = 'http://thalesgroup.com/RTTI/2012-01-13/ldbsv'; | ||
const referenceBaseUrl = 'https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ldbsvref.asmx'; | ||
const refActionBase = 'http://thalesgroup.com/RTTI/2015-05-14/ldbsv_ref'; | ||
|
||
const isObject = obj => obj === Object(obj); | ||
|
||
module.exports = class NationalRailClient { | ||
static denamespacify(obj) { | ||
if (obj instanceof Array) { | ||
const result = []; | ||
obj.forEach(x => { | ||
if (isObject(x) || x instanceof Array) { | ||
if (x['attr'] && x['attr']['xmlns']) { | ||
delete x['attr']['xmlns']; | ||
if (Object.keys(x['attr']).length === 0) { | ||
delete x['attr']; | ||
} | ||
} | ||
|
||
result.push(NationalRailClient.denamespacify(x)); | ||
} | ||
else { | ||
result.push(x); | ||
} | ||
}); | ||
return result; | ||
} | ||
else { | ||
const result = {}; | ||
Object.keys(obj).forEach(k => { | ||
const splat = k.split(':'); | ||
const newName = splat[splat.length - 1]; | ||
if (isObject(obj[k]) || obj[k] instanceof Array) { | ||
result[newName] = NationalRailClient.denamespacify(obj[k]); | ||
|
||
if (result[newName]['attr'] && result[newName]['attr']['xmlns']) { | ||
delete result[newName]['attr']['xmlns']; | ||
if (Object.keys(result[newName]['attr']).length === 0) { | ||
delete result[newName]['attr']; | ||
} | ||
} | ||
} | ||
else { | ||
result[newName] = obj[k]; | ||
} | ||
}); | ||
return result; | ||
} | ||
} | ||
|
||
constructor (token) { | ||
this._token = token; | ||
|
||
methods.forEach(m => { | ||
const chars = m.split(''); | ||
const methodName = chars[0].toLowerCase() + chars.slice(1, chars.length).join(''); | ||
this[methodName] = this.createRequester(m); | ||
}); | ||
|
||
referenceMethods.forEach(m => { | ||
const chars = m.split(''); | ||
const methodName = chars[0].toLowerCase() + chars.slice(1, chars.length).join(''); | ||
this[methodName] = this.createRequester(m, true); | ||
}); | ||
} | ||
|
||
createSoapRequest(body) { | ||
return xmlbuilder.create({ | ||
'soap:Envelope': { | ||
'@xmlns:soap': 'http://www.w3.org/2003/05/soap-envelope', | ||
'@xmlns:typ': 'http://thalesgroup.com/RTTI/2013-11-28/Token/types', | ||
'@xmlns:ldb': 'http://thalesgroup.com/RTTI/2017-10-01/ldbsv/', | ||
'soap:Header': { | ||
'typ:AccessToken': { | ||
'typ:TokenValue': this._token | ||
} | ||
}, | ||
'soap:Body': body | ||
} | ||
}).end({ pretty: true }); | ||
} | ||
|
||
createRequester(method, isReference = false) { | ||
const requestType = `ldb:${method}Request`; | ||
return async params => { | ||
const xmlParams = {}; | ||
Object.keys(params).forEach(p => { | ||
xmlParams[`ldb:${p}`] = params[p]; | ||
}); | ||
|
||
const body = this.createSoapRequest({ | ||
[requestType]: xmlParams | ||
}); | ||
const response = await fetch(isReference ? referenceBaseUrl : apiBaseUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': `application/soap+xml;charset=UTF-8;action=${isReference ? refActionBase : apiActionBase}/${method}`, | ||
'Accept-Encoding': 'gzip, deflate' | ||
}, | ||
body | ||
}); | ||
|
||
const raw = await response.text(); | ||
const data = xmlParser.parse(raw, { textNodeName: 'content', attributeNamePrefix: '', attrNodeName: 'attr', ignoreAttributes: false }); | ||
const responseType = `${method}Response`; | ||
const bodyData = data['soap:Envelope']['soap:Body'][responseType]; | ||
return NationalRailClient.denamespacify(bodyData[Object.keys(bodyData).filter(x => x !== 'attr')[0]]); | ||
}; | ||
} | ||
const xmlbuilder = require('xmlbuilder'); | ||
const fetch = require('node-fetch'); | ||
const xmlParser = require('fast-xml-parser'); | ||
|
||
const methods = [ | ||
"GetArrBoardWithDetails", "GetArrDepBoardWithDetails", "GetArrivalDepartureBoardByCRS", "GetArrivalDepartureBoardByTIPLOC", | ||
"GetArrivalBoardByCRS", "GetArrivalBoardByTIPLOC", "GetDepartureBoardByCRS", "GetDepartureBoardByTIPLOC", "GetDepBoardWithDetails", | ||
"GetDisruptionList", "GetFastestDepartures", "GetFastestDeparturesWithDetails", "GetHistoricDepartureBoard", "GetHistoricServiceDetails", | ||
"GetHistoricTimeLine", "GetNextDepartures", "GetNextDeparturesWithDetails", "GetServiceDetailsByRID", "QueryHistoricServices", "QueryServices" | ||
]; | ||
|
||
const referenceMethods = ["GetReasonCode", "GetReasonCodeList", "GetSourceInstanceNames", "GetStationList", "GetTOCList"]; | ||
|
||
const apiBaseUrl = 'https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ldbsv12.asmx'; | ||
const apiActionBase = 'http://thalesgroup.com/RTTI/2012-01-13/ldbsv'; | ||
const referenceBaseUrl = 'https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ldbsvref.asmx'; | ||
const refActionBase = 'http://thalesgroup.com/RTTI/2015-05-14/ldbsv_ref'; | ||
|
||
const isObject = obj => obj === Object(obj); | ||
|
||
class SoapException extends Error { | ||
constructor (message) { | ||
super(); | ||
this.message = message; | ||
} | ||
} | ||
|
||
module.exports = class NationalRailClient { | ||
static denamespacify(obj) { | ||
if (obj instanceof Array) { | ||
const result = []; | ||
obj.forEach(x => { | ||
if (isObject(x) || x instanceof Array) { | ||
if (x['attr'] && x['attr']['xmlns']) { | ||
delete x['attr']['xmlns']; | ||
if (Object.keys(x['attr']).length === 0) { | ||
delete x['attr']; | ||
} | ||
} | ||
|
||
result.push(NationalRailClient.denamespacify(x)); | ||
} | ||
else { | ||
result.push(x); | ||
} | ||
}); | ||
return result; | ||
} | ||
else { | ||
const result = {}; | ||
Object.keys(obj).forEach(k => { | ||
const splat = k.split(':'); | ||
const newName = splat[splat.length - 1]; | ||
if (isObject(obj[k]) || obj[k] instanceof Array) { | ||
result[newName] = NationalRailClient.denamespacify(obj[k]); | ||
|
||
if (result[newName]['attr'] && result[newName]['attr']['xmlns']) { | ||
delete result[newName]['attr']['xmlns']; | ||
if (Object.keys(result[newName]['attr']).length === 0) { | ||
delete result[newName]['attr']; | ||
} | ||
} | ||
} | ||
else { | ||
result[newName] = obj[k]; | ||
} | ||
}); | ||
return result; | ||
} | ||
} | ||
|
||
constructor (token) { | ||
this._token = token; | ||
|
||
methods.forEach(m => { | ||
const chars = m.split(''); | ||
const methodName = chars[0].toLowerCase() + chars.slice(1, chars.length).join(''); | ||
this[methodName] = this.createRequester(m); | ||
}); | ||
|
||
referenceMethods.forEach(m => { | ||
const chars = m.split(''); | ||
const methodName = chars[0].toLowerCase() + chars.slice(1, chars.length).join(''); | ||
this[methodName] = this.createRequester(m, true); | ||
}); | ||
} | ||
|
||
createSoapRequest(body) { | ||
return xmlbuilder.create({ | ||
'soap:Envelope': { | ||
'@xmlns:soap': 'http://www.w3.org/2003/05/soap-envelope', | ||
'@xmlns:typ': 'http://thalesgroup.com/RTTI/2013-11-28/Token/types', | ||
'@xmlns:ldb': 'http://thalesgroup.com/RTTI/2017-10-01/ldbsv/', | ||
'soap:Header': { | ||
'typ:AccessToken': { | ||
'typ:TokenValue': this._token | ||
} | ||
}, | ||
'soap:Body': body | ||
} | ||
}).end({ pretty: true }); | ||
} | ||
|
||
createRequester(method, isReference = false) { | ||
const requestType = `ldb:${method}Request`; | ||
return async params => { | ||
const xmlParams = {}; | ||
Object.keys(params).forEach(p => { | ||
xmlParams[`ldb:${p}`] = params[p]; | ||
}); | ||
|
||
const body = this.createSoapRequest({ | ||
[requestType]: xmlParams | ||
}); | ||
const response = await fetch(isReference ? referenceBaseUrl : apiBaseUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': `application/soap+xml;charset=UTF-8;action=${isReference ? refActionBase : apiActionBase}/${method}`, | ||
'Accept-Encoding': 'gzip, deflate' | ||
}, | ||
body | ||
}); | ||
|
||
const raw = await response.text(); | ||
const data = xmlParser.parse(raw, { textNodeName: 'content', attributeNamePrefix: '', attrNodeName: 'attr', ignoreAttributes: false }); | ||
const responseType = `${method}Response`; | ||
const soapBody = data['soap:Envelope']['soap:Body']; | ||
|
||
if (soapBody['soap:Fault']) { | ||
throw new SoapException(soapBody['soap:Fault']['soap:Reason']['soap:Text'].content); | ||
} | ||
else { | ||
const bodyData = soapBody[responseType] ? soapBody[responseType] : {}; | ||
const dataKey = Object.keys(bodyData).filter(x => x !== 'attr')[0]; | ||
return !dataKey ? {} : NationalRailClient.denamespacify(bodyData[dataKey]); | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
port: 3000, | ||
token: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' | ||
module.exports = { | ||
port: 3000, | ||
token: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' | ||
} |
Oops, something went wrong.