mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
parent
eed345e3fc
commit
3e6d6d9917
11 changed files with 177 additions and 2 deletions
|
@ -11,6 +11,7 @@
|
|||
- [`nearby(location, [opt])`](nearby.md) – show stations & POIs around
|
||||
- [`radar(north, west, south, east, [opt])`](radar.md) – find all vehicles currently in a certain area
|
||||
- [`reachableFrom(address, [opt])`](reachable-from.md) – get all stations reachable from an address within `n` minutes
|
||||
- [`serverInfo([opt])`](server-info.md) – fetch meta information from HAFAS
|
||||
|
||||
## Migrating from an old `hafas-client` version
|
||||
|
||||
|
|
33
docs/server-info.md
Normal file
33
docs/server-info.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
# `serverInfo([opt])`
|
||||
|
||||
**Fetches meta information from the HAFAS endpoint.**
|
||||
|
||||
With `opt`, you can override the default options, which look like this:
|
||||
|
||||
```js
|
||||
{
|
||||
language: 'en', // depends on the profile
|
||||
}
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
As an example, we're going to use the [SVV profile](../p/svv):
|
||||
|
||||
```js
|
||||
const createClient = require('hafas-client')
|
||||
const svvProfile = require('hafas-client/p/svv')
|
||||
|
||||
const client = createClient(svvProfile, 'my-awesome-program')
|
||||
|
||||
console.log(await client.serverInfo())
|
||||
```
|
||||
|
||||
```js
|
||||
{
|
||||
timetableStart: '20200517',
|
||||
timetableEnd: '20201212',
|
||||
serverTime: '2020-07-19T21:32:12+02:00',
|
||||
realtimeDataUpdatedAt: 1595187102,
|
||||
}
|
||||
```
|
29
index.js
29
index.js
|
@ -468,7 +468,34 @@ const createClient = (profile, userAgent, opt = {}) => {
|
|||
})
|
||||
}
|
||||
|
||||
const client = {departures, arrivals, journeys, locations, stop, nearby}
|
||||
const serverInfo = async (opt = {}) => {
|
||||
const {res, common} = await profile.request({profile, opt}, userAgent, {
|
||||
meth: 'ServerInfo',
|
||||
req: {}
|
||||
})
|
||||
|
||||
const ctx = {profile, opt, common, res}
|
||||
return {
|
||||
timetableStart: res.fpB || null,
|
||||
timetableEnd: res.fpE || null,
|
||||
serverTime: res.sD && res.sT
|
||||
? profile.parseDateTime(ctx, res.sD, res.sT)
|
||||
: null,
|
||||
realtimeDataUpdatedAt: res.planrtTS
|
||||
? parseInt(res.planrtTS)
|
||||
: null,
|
||||
}
|
||||
}
|
||||
|
||||
const client = {
|
||||
departures,
|
||||
arrivals,
|
||||
journeys,
|
||||
locations,
|
||||
stop,
|
||||
nearby,
|
||||
serverInfo,
|
||||
}
|
||||
if (profile.trip) client.trip = trip
|
||||
if (profile.radar) client.radar = radar
|
||||
if (profile.refreshJourney) client.refreshJourney = refreshJourney
|
||||
|
|
|
@ -26,6 +26,7 @@ const testDeparturesWithoutRelatedStations = require('./lib/departures-without-r
|
|||
const testArrivals = require('./lib/arrivals')
|
||||
const testJourneysWithDetour = require('./lib/journeys-with-detour')
|
||||
const testReachableFrom = require('./lib/reachable-from')
|
||||
const testServerInfo = require('./lib/server-info')
|
||||
|
||||
const stations = []
|
||||
const pStations = new Promise((resolve, reject) => {
|
||||
|
@ -451,3 +452,10 @@ test('reachableFrom', async (t) => {
|
|||
})
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('serverInfo works', async (t) => {
|
||||
await testServerInfo({
|
||||
test: t,
|
||||
fetchServerInfo: client.serverInfo,
|
||||
})
|
||||
})
|
||||
|
|
1
test/e2e/fixtures/08f5be071bd4098b603ebcd9d671b84a
vendored
Normal file
1
test/e2e/fixtures/08f5be071bd4098b603ebcd9d671b84a
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{"ver":"1.20","ext":"VAO.11","lang":"eng","id":"t42agrnmm2g4wm44","err":"OK","cInfo":{"code":"OK","url":"","msg":""},"svcResL":[{"meth":"ServerInfo","err":"OK","res":{"common":{"locL":[],"prodL":[],"polyL":[],"layerL":[{"id":"standard","name":"standard","index":0,"annoCnt":0}],"crdSysL":[{"id":"standard","index":0,"type":"WGS84"}],"opL":[],"remL":[],"txtInstL":[],"icoL":[]},"fpB":"20200517","fpE":"20201212","sD":"20200719","sT":"220453","enc":"UTF-8","planrtTS":"1595189078"}}]}
|
36
test/e2e/fixtures/08f5be071bd4098b603ebcd9d671b84a.headers
vendored
Normal file
36
test/e2e/fixtures/08f5be071bd4098b603ebcd9d671b84a.headers
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"statusCode": 200,
|
||||
"headers": {
|
||||
"date": "Sun, 19 Jul 2020 20:04:53 GMT",
|
||||
"server": "Apache",
|
||||
"content-length": "304",
|
||||
"keep-alive": "timeout=5, max=13",
|
||||
"connection": "Keep-Alive",
|
||||
"content-type": "application/json; charset=utf-8"
|
||||
},
|
||||
"url": "https://fahrplan.salzburg-verkehr.at/bin/mgate.exe",
|
||||
"time": 312,
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": [
|
||||
"application/json"
|
||||
],
|
||||
"Accept-Encoding": [
|
||||
"gzip, br, deflate"
|
||||
],
|
||||
"Accept": [
|
||||
"application/json"
|
||||
],
|
||||
"user-agent": [
|
||||
"public-transport/hafas-client:testdf34c2ec2b3a"
|
||||
],
|
||||
"Content-Length": [
|
||||
"182"
|
||||
],
|
||||
"Connection": [
|
||||
"close"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
1
test/e2e/fixtures/bbc7f8898cbe51f5a85836d62e1abfdc
vendored
Normal file
1
test/e2e/fixtures/bbc7f8898cbe51f5a85836d62e1abfdc
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{"ver":"1.15","ext":"DB.R19.04.a","lang":"eng","id":"pvwu8r74k6wyw644","cInfo":{"code":"OK","url":"","msg":""},"svcResL":[{"meth":"ServerInfo","err":"OK","res":{"common":{"locL":[],"prodL":[],"polyL":[],"layerL":[{"id":"standard","name":"standard","index":0,"annoCnt":0}],"crdSysL":[{"id":"standard","index":0,"type":"WGS84","dim":3}],"opL":[],"remL":[],"icoL":[]},"fpB":"20191215","fpE":"20201212","sD":"20200719","sT":"220419","enc":"ISO-8859-1","planrtTS":"1595188979"}}]}
|
35
test/e2e/fixtures/bbc7f8898cbe51f5a85836d62e1abfdc.headers
vendored
Normal file
35
test/e2e/fixtures/bbc7f8898cbe51f5a85836d62e1abfdc.headers
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"statusCode": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
"date": "Sun, 19 Jul 2020 20:04:19 GMT",
|
||||
"server": "Apache",
|
||||
"content-length": "312",
|
||||
"connection": "Close"
|
||||
},
|
||||
"url": "https://reiseauskunft.bahn.de/bin/mgate.exe?checksum=61043123e067664a035a32152dc11d35",
|
||||
"time": 286,
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": [
|
||||
"application/json"
|
||||
],
|
||||
"Accept-Encoding": [
|
||||
"gzip, br, deflate"
|
||||
],
|
||||
"Accept": [
|
||||
"application/json"
|
||||
],
|
||||
"user-agent": [
|
||||
"public-transpo8bcc44962164rt/hafas-client:test"
|
||||
],
|
||||
"Content-Length": [
|
||||
"233"
|
||||
],
|
||||
"Connection": [
|
||||
"close"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
24
test/e2e/lib/server-info.js
Normal file
24
test/e2e/lib/server-info.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
'use strict'
|
||||
|
||||
const testServerInfo = async (cfg) => {
|
||||
const {
|
||||
test: t,
|
||||
fetchServerInfo,
|
||||
} = cfg
|
||||
|
||||
const info = await fetchServerInfo()
|
||||
t.ok(info, 'invalid info')
|
||||
|
||||
t.equal(typeof info.timetableStart, 'string', 'invalid info.timetableStart')
|
||||
t.ok(info.timetableStart, 'invalid info.timetableStart')
|
||||
t.equal(typeof info.timetableEnd, 'string', 'invalid info.timetableEnd')
|
||||
t.ok(info.timetableEnd, 'invalid info.timetableEnd')
|
||||
|
||||
t.equal(typeof info.serverTime, 'string', 'invalid info.serverTime')
|
||||
t.notOk(Number.isNaN(Date.parse(info.serverTime)), 'invalid info.serverTime')
|
||||
|
||||
t.ok(Number.isInteger(info.realtimeDataUpdatedAt), 'invalid info.realtimeDataUpdatedAt')
|
||||
t.ok(info.realtimeDataUpdatedAt > 0, 'invalid info.realtimeDataUpdatedAt')
|
||||
}
|
||||
|
||||
module.exports = testServerInfo
|
|
@ -10,6 +10,7 @@ const testJourneysStationToStation = require('./lib/journeys-station-to-station'
|
|||
const testRefreshJourney = require('./lib/refresh-journey')
|
||||
const testArrivals = require('./lib/arrivals')
|
||||
const testReachableFrom = require('./lib/reachable-from')
|
||||
const testServerInfo = require('./lib/server-info')
|
||||
|
||||
const when = createWhen(svvProfile.timezone, svvProfile.locale)
|
||||
|
||||
|
@ -94,3 +95,10 @@ test('reachableFrom', async (t) => {
|
|||
})
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('serverInfo works', async (t) => {
|
||||
await testServerInfo({
|
||||
test: t,
|
||||
fetchServerInfo: client.serverInfo,
|
||||
})
|
||||
})
|
||||
|
|
|
@ -38,7 +38,8 @@ const parseArgs = [
|
|||
['radar', 0, parseJsObject],
|
||||
['radar', 1, parseJsObject],
|
||||
['reachableFrom', 0, parseJsObject],
|
||||
['reachableFrom', 1, parseJsObject]
|
||||
['reachableFrom', 1, parseJsObject],
|
||||
['serverInfo', 0, parseJsObject],
|
||||
]
|
||||
|
||||
const argv = mri(process.argv.slice(2))
|
||||
|
|
Loading…
Add table
Reference in a new issue