ability to fetch data

This commit is contained in:
Jannis R 2016-06-22 01:39:59 +02:00
parent df398966dc
commit 844780d805
2 changed files with 32 additions and 2 deletions

View file

@ -1,3 +1,30 @@
'use strict'
// todo
const got = require('got')
const request = (cfg) => (data) => {
let req = {
json: true,
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate'
},
body: JSON.stringify({
client: cfg.client, ext: cfg.ext
, ver: cfg.version, auth: cfg.auth
, lang: 'en', svcReqL: [data]
})
}
if (cfg.req) req = cfg.req(req)
return got.post(cfg.endpoint, req)
.then((res) => {
if (res.body.err) return new Error(res.body.err)
if (!res.body.svcResL || !res.body.svcResL[0]) return new Error('invalid response')
const data = res.body.svcResL[0]
if (data.err !== 'OK') return new Error(data.errTxt)
return data.res
}).catch(console.error)
}
module.exports = request

View file

@ -10,5 +10,8 @@
"bugs": "https://github.com/derhuerst/hafas-client/issues",
"license": "ISC",
"keywords": ["hafas", "public", "transport", "api"],
"engines" : {"node": ">=4"}
"engines" : {"node": ">=4"},
"dependencies": {
"got": "^6.3"
}
}