mandatory User-Agent param 💥

This commit is contained in:
Jannis R 2018-07-19 21:50:20 +02:00 committed by Jannis Redmann
parent b5b2cfb38f
commit 5d9d738152
3 changed files with 16 additions and 12 deletions

View file

@ -12,7 +12,7 @@ const _request = require('./lib/request')
const isNonEmptyString = str => 'string' === typeof str && str.length > 0 const isNonEmptyString = str => 'string' === typeof str && str.length > 0
const createClient = (profile, request = _request) => { const createClient = (profile, userAgent, request = _request) => {
profile = Object.assign({}, defaultProfile, profile) profile = Object.assign({}, defaultProfile, profile)
if (!profile.parseProducts) { if (!profile.parseProducts) {
profile.parseProducts = createParseBitmask(profile) profile.parseProducts = createParseBitmask(profile)
@ -22,6 +22,10 @@ const createClient = (profile, request = _request) => {
} }
validateProfile(profile) validateProfile(profile)
if ('string' !== typeof userAgent) {
throw new Error('userAgent must be a string');
}
const _stationBoard = (station, type, parser, opt = {}) => { const _stationBoard = (station, type, parser, opt = {}) => {
if (isObj(station)) station = profile.formatStation(station.id) if (isObj(station)) station = profile.formatStation(station.id)
else if ('string' === typeof station) station = profile.formatStation(station) else if ('string' === typeof station) station = profile.formatStation(station)
@ -45,7 +49,7 @@ const createClient = (profile, request = _request) => {
const products = profile.formatProductsFilter(opt.products || {}) const products = profile.formatProductsFilter(opt.products || {})
const dir = opt.direction ? profile.formatStation(opt.direction) : null const dir = opt.direction ? profile.formatStation(opt.direction) : null
return request(profile, opt, { return request(profile, userAgent, opt, {
meth: 'StationBoard', meth: 'StationBoard',
req: { req: {
type, type,
@ -181,7 +185,7 @@ const createClient = (profile, request = _request) => {
} }
if (profile.journeysNumF) query.numF = opt.results if (profile.journeysNumF) query.numF = opt.results
return request(profile, opt, { return request(profile, userAgent, opt, {
cfg: {polyEnc: 'GPA'}, cfg: {polyEnc: 'GPA'},
meth: 'TripSearch', meth: 'TripSearch',
req: profile.transformJourneysQuery(query, opt) req: profile.transformJourneysQuery(query, opt)
@ -234,7 +238,7 @@ const createClient = (profile, request = _request) => {
}, opt) }, opt)
const f = profile.formatLocationFilter(opt.stations, opt.addresses, opt.poi) const f = profile.formatLocationFilter(opt.stations, opt.addresses, opt.poi)
return request(profile, opt, { return request(profile, userAgent, opt, {
cfg: {polyEnc: 'GPA'}, cfg: {polyEnc: 'GPA'},
meth: 'LocMatch', meth: 'LocMatch',
req: {input: { req: {input: {
@ -261,7 +265,7 @@ const createClient = (profile, request = _request) => {
opt = Object.assign({ opt = Object.assign({
stationLines: false // parse & expose lines of the station? stationLines: false // parse & expose lines of the station?
}, opt) }, opt)
return request(profile, opt, { return request(profile, userAgent, opt, {
meth: 'LocDetails', meth: 'LocDetails',
req: { req: {
locL: [station] locL: [station]
@ -295,7 +299,7 @@ const createClient = (profile, request = _request) => {
stationLines: false // parse & expose lines of the station? stationLines: false // parse & expose lines of the station?
}, opt) }, opt)
return request(profile, opt, { return request(profile, userAgent, opt, {
cfg: {polyEnc: 'GPA'}, cfg: {polyEnc: 'GPA'},
meth: 'LocGeoPos', meth: 'LocGeoPos',
req: { req: {
@ -334,7 +338,7 @@ const createClient = (profile, request = _request) => {
opt.when = new Date(opt.when || Date.now()) opt.when = new Date(opt.when || Date.now())
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
return request(profile, opt, { return request(profile, userAgent, opt, {
cfg: {polyEnc: 'GPA'}, cfg: {polyEnc: 'GPA'},
meth: 'JourneyDetails', meth: 'JourneyDetails',
req: { req: {
@ -384,7 +388,7 @@ const createClient = (profile, request = _request) => {
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
const durationPerStep = opt.duration / Math.max(opt.frames, 1) * 1000 const durationPerStep = opt.duration / Math.max(opt.frames, 1) * 1000
return request(profile, opt, { return request(profile, userAgent, opt, {
meth: 'JourneyGeoPos', meth: 'JourneyGeoPos',
req: { req: {
maxJny: opt.results, maxJny: opt.results,

View file

@ -11,7 +11,7 @@ const {fetch} = require('fetch-ponyfill')({Promise})
const md5 = input => createHash('md5').update(input).digest() const md5 = input => createHash('md5').update(input).digest()
const request = (profile, opt, data) => { const request = (profile, userAgent, opt, data) => {
const body = profile.transformReqBody({ const body = profile.transformReqBody({
lang: opt.language || 'en', lang: opt.language || 'en',
svcReqL: [data] svcReqL: [data]
@ -24,7 +24,7 @@ const request = (profile, opt, data) => {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate', 'Accept-Encoding': 'gzip, deflate',
'Accept': 'application/json', 'Accept': 'application/json',
'user-agent': 'https://github.com/public-transport/hafas-client' 'user-agent': userAgent
}, },
query: {} query: {}
}) })

View file

@ -5,9 +5,9 @@ const throttle = require('p-throttle')
const request = require('./lib/request') const request = require('./lib/request')
const createClient = require('.') const createClient = require('.')
const createThrottledClient = (profile, limit = 5, interval = 1000) => { const createThrottledClient = (profile, userAgent, limit = 5, interval = 1000) => {
const throttledRequest = throttle(request, limit, interval) const throttledRequest = throttle(request, limit, interval)
return createClient(profile, throttledRequest) return createClient(profile, userAgent, throttledRequest)
} }
module.exports = createThrottledClient module.exports = createThrottledClient