2022-10-24 17:30:05 +02:00
# `hafas-client` documentation
2018-03-14 20:46:10 +01:00
2022-10-24 17:30:05 +02:00
**[API documentation ](api.md )**
2018-03-14 22:52:36 +01:00
2018-12-10 20:27:51 +01:00
## Migrating from an old `hafas-client` version
2019-12-10 20:43:45 +01:00
- [`4` → `5` migration guide ](migrating-to-5.md )
2018-12-10 20:27:51 +01:00
2019-02-08 13:14:19 +01:00
## Throttling requests
There's opt-in support for throttling requests to the endpoint.
```js
2019-02-08 14:59:21 +01:00
const createClient = require('hafas-client')
2019-10-31 18:48:11 +01:00
const withThrottling = require('hafas-client/throttle')
2019-02-08 13:14:19 +01:00
const dbProfile = require('hafas-client/p/db')
// create a throttled HAFAS client with Deutsche Bahn profile
2019-10-31 18:48:11 +01:00
const client = createClient(withThrottling(dbProfile), 'my-awesome-program')
2019-02-08 13:14:19 +01:00
// Berlin Jungfernheide to München Hbf
client.journeys('8011167', '8000261', {results: 1})
.then(console.log)
.catch(console.error)
```
2019-02-08 14:59:21 +01:00
You can pass custom values for the nr of requests (`limit` ) per interval into `withThrottling` :
2019-02-08 13:14:19 +01:00
```js
// 2 requests per second
2019-10-31 18:48:11 +01:00
const throttledDbProfile = withThrottling(dbProfile, 2, 1000)
const client = createClient(throttledDbProfile, 'my-awesome-program')
2019-02-08 13:14:19 +01:00
```
## Retrying failed requests
There's opt-in support for retrying failed requests to the endpoint.
```js
2019-02-08 15:07:35 +01:00
const createClient = require('hafas-client')
2019-10-31 18:48:11 +01:00
const withRetrying = require('hafas-client/retry')
2019-02-08 13:14:19 +01:00
const dbProfile = require('hafas-client/p/db')
// create a client with Deutsche Bahn profile that will retry on HAFAS errors
2019-10-31 18:48:11 +01:00
const client = createClient(withRetrying(dbProfile), 'my-awesome-program')
2019-02-08 13:14:19 +01:00
// Berlin Jungfernheide to München Hbf
client.journeys('8011167', '8000261', {results: 1})
.then(console.log)
.catch(console.error)
```
2019-02-08 15:07:35 +01:00
You can pass custom options into `withRetrying` . They will be passed into [`retry` ](https://github.com/tim-kos/node-retry#tutorial ).
2019-02-08 13:14:19 +01:00
```js
// retry 2 times, after 10 seconds & 30 seconds
2019-10-31 18:48:11 +01:00
const retryingDbProfile = withRetrying(dbProfile, {
2019-02-08 13:14:19 +01:00
retries: 2,
minTimeout: 10 * 1000,
factor: 3
})
2019-10-31 18:48:11 +01:00
const client = createClient(retryingDbProfile, 'my-awesome-program')
2019-02-08 13:14:19 +01:00
```
2022-10-06 14:22:46 +02:00
## Logging requests
You can use `profile.logRequest` and `profile.logResponse` to process the raw [Fetch ](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API ) [`Request` ](https://developer.mozilla.org/en-US/docs/Web/API/Request ) and [`Response` ](https://developer.mozilla.org/en-US/docs/Web/API/Response ), respectively.
As an example, we can implement a custom logger:
```js
const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db')
2022-10-07 01:29:36 +02:00
const logRequest = (ctx, fetchRequest, requestId) => {
2022-10-06 14:22:46 +02:00
// ctx looks just like with the other profile.* hooks:
const {profile, opt} = ctx
2022-10-07 01:29:36 +02:00
console.debug(requestId, fetchRequest.headers, fetchRequest.body + '')
}
const logResponse = (ctx, fetchResponse, body, requestId) => {
console.debug(requestId, fetchResponse.headers, body + '')
2022-10-06 14:22:46 +02:00
}
// create a client with Deutsche Bahn profile that debug-logs
const client = createClient({
...dbProfile,
logRequest,
logResponse,
}, 'my-awesome-program')
```
```js
// logRequest output:
2022-10-07 01:29:36 +02:00
'29d0e3' {
2022-10-06 14:22:46 +02:00
accept: 'application/json',
'accept-encoding': 'gzip, br, deflate',
'content-type': 'application/json',
connection: 'keep-alive',
'user-agent': 'hafas842c51-clie842c51nt debug C842c51LI'
} {"lang":"de","svcReqL":[{"cfg":{"polyEnc":"GPA"},"meth":"LocMatch",…
// logResponse output:
2022-10-07 01:29:36 +02:00
'29d0e3' {
2022-10-06 14:22:46 +02:00
'content-encoding': 'gzip',
'content-length': '1010',
'content-type': 'application/json; charset=utf-8',
date: 'Thu, 06 Oct 2022 12:31:09 GMT',
server: 'Apache',
vary: 'User-Agent'
} {"ver":"1.45","lang":"deu","id":"sb42zgck4mxtxm4s","err":"OK","graph"…
```
The default `profile.logRequest` [`console.error` ](https://nodejs.org/docs/latest-v10.x/api/console.html#console_console_error_data_args )s the request body, if you have set `$DEBUG` to `hafas-client` . Likewise, `profile.logResponse` `console.error` s the response body.
2022-10-24 14:11:47 +02:00
## Using `hafas-client` from another language
If you want to use `hafas-client` to access HAFAS APIs but work with non-Node.js environments, you can use [`hafas-client-rpc` ](https://github.com/derhuerst/hafas-client-rpc ) to create a [JSON-RPC ](https://www.jsonrpc.org ) interface that you can send commands to.
2018-03-14 22:52:36 +01:00
## Writing a profile
Check [the guide ](writing-a-profile.md ).
2019-12-20 18:02:48 +01:00
## General documentation for `mgate.exe` APIs
[`hafas-mgate-api.md` ](hafas-mgate-api.md )