Skip to content

Commit 204e452

Browse files
committed
rest.exe: export createClient
1 parent f9ddc70 commit 204e452

File tree

4 files changed

+400
-357
lines changed

4 files changed

+400
-357
lines changed

lib/randomize-user-agent.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const {randomBytes} = require('crypto')
4+
5+
const id = randomBytes(6).toString('hex')
6+
const randomizeUserAgent = (userAgent) => {
7+
const i = Math.round(Math.random() * userAgent.length)
8+
return userAgent.slice(0, i) + id + userAgent.slice(i)
9+
}
10+
11+
module.exports = randomizeUserAgent

lib/request.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@
33
const DEV = process.env.NODE_ENV === 'dev'
44
const DEBUG = process.env.DEBUG === 'hafas-client'
55

6-
const {randomBytes} = require('crypto')
76
const createHash = require('create-hash')
87
const captureStackTrace = DEV ? require('capture-stack-trace') : () => {}
98
const {stringify} = require('qs')
109
const Promise = require('pinkie-promise')
1110
const {fetch} = require('fetch-ponyfill')({Promise})
11+
const randomizeUserAgent = require('./randomize-user-agent')
1212
const {byErrorCode} = require('./errors')
1313

14-
const id = randomBytes(6).toString('hex')
15-
const randomizeUserAgent = (userAgent) => {
16-
const i = Math.round(Math.random() * userAgent.length)
17-
return userAgent.slice(0, i) + id + userAgent.slice(i)
18-
}
19-
2014
const md5 = input => createHash('md5').update(input).digest()
2115

2216
const addErrorInfo = (err, errorCode, errorText) => {

rest-exe-example.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
const createClient = require('./rest-exe')
4+
5+
const TOKEN = process.env.TOKEN
6+
if (!TOKEN) throw new Error('missing TOKEN env var')
7+
8+
const profile = {
9+
endpoint: 'https://dbrest-rt.hafas.de/openapi/1.23/'
10+
}
11+
12+
const {
13+
locations, nearby,
14+
departures, arrivals,
15+
journey, trip, tripHistory, tripAlternatives,
16+
radar,
17+
remarks, dataInfo
18+
} = createClient(profile, TOKEN, 'hafas-client example')
19+
20+
const berlinOstkreuz = '8011162'
21+
const berlinSchönefeld = '8010109'
22+
const berlinWestkreuz = '8089047'
23+
const berlinMesseSüd = '8089328'
24+
const berlinYorckstrS1 = '8089051'
25+
const somewhereInBerlin = {type: 'location', address: 'foo', latitude: 52.51072, longitude: 13.37793}
26+
27+
// journeys(berlinOstkreuz, '8000261', {
28+
// results: 3,
29+
// stopovers: true,
30+
// remarks: true
31+
// })
32+
// .then(journeys => journeys.flatMap(j => [...j.legs, '---']))
33+
// .then(([journey]) => journey.legs.map(l => l.tripId).find(tripId => !!tripId))
34+
// .then(trip)
35+
// .then(tripHistory)
36+
// tripAlternatives()
37+
38+
locations('leopoldplatz ber')
39+
// nearby({type: 'location', latitude: 52.4751309, longitude: 13.3656537})
40+
// departures(berlinYorckstrS1, {
41+
// remarks: true
42+
// })
43+
// .then(deps => deps.map(d => [d.line && d.line.name, d.stop && d.stop.name]))
44+
// arrivals(berlinYorckstrS1, {
45+
// remarks: true
46+
// })
47+
// radar({
48+
// north: 52.52411,
49+
// west: 13.41002,
50+
// south: 52.51942,
51+
// east: 13.41709
52+
// })
53+
// remarks()
54+
// dataInfo()
55+
56+
.then(data => console.log(require('util').inspect(data, {depth: null, colors: true})))
57+
.catch((err) => {
58+
console.error(err)
59+
process.exit(1)
60+
})

0 commit comments

Comments
 (0)