This project is a working WIP. It does not have all planned features.
The BaseAPIRequester
is not supposed to be used for anything except as a template for an HTTP request class.
This class is not for use as all methods included will throw an error. The BaseAPIRequester
should only be used to extend another class that implements the HTTPThrottler.HTTPThrottled
interface.
class FooAPIRequester extends BaseAPIRequester {
constructor() {
super()
}
public requestIsAllowed() {
return true
}
public performRequest(options) {
return axios(options)
}
}
The APIRequester
is an implementation of HTTPThrottler.HTTPThrottled
that extends BaseAPIRequester
with a token bucket.
This example will create an instance of APIRequster
allowing 10 requests per minute.
const vasttrafikAPIRequester = new vasttrafik.APIRequester(10, 60000)
performRequest
Perform a request using axios
.
apiRequester.performRequest({
url: "https://httpbin.org/get",
method: "GET"
}).then((response) => {
// Consume response
}).catch((error) => {
// Handle errors
})
requestIsAllowed
Check if a HTTP request is allowed or not.
if (apiRequester.requestIsAllowed()) {
// Perform a request
} else {
// Do not perform a request
}
refillTokens
Refill tokens to the token bucket to make sure there are token left to make HTTP requests. This is typically not necessary as this method is called with requestIsAllowed
. The token bucket will refill if the time when the token bucket was refilled plus the amount of milliseconds passed to the constructor has passed.
apiRequester.refillTokens()
The API
is used to perform HTTP requests to Västtrafik's API.
const vasttrafikAPI = new vasttrafik.API("<västtrafik API access token>", vasttrafikAPIRequester)
getDepartures
Get JSON from Västtrafik's journey planner API version v2 for the /departureBoard
endpoint. Returns the parsed response as an object according to the type vasttrafik.Stop
.
vasttrafikAPI.getDepartures("9022014001960001", new Date(), 60, false).then((stop) => {
// Consume stop
}).catch((error) => {
// Handle errors
})
MIT License
Copyright © 2017-2021 Joel Ericsson
Generated using TypeDoc