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

View file

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

View file

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