Add rate limiting

This commit is contained in:
Sebastian Rindom
2020-08-23 16:50:54 +02:00
parent a6807edf2a
commit c37f410429
2 changed files with 16 additions and 7 deletions
@@ -36,6 +36,7 @@
},
"dependencies": {
"axios": "^0.19.2",
"axios-rate-limit": "^1.2.1",
"express": "^4.17.1",
"medusa-core-utils": "^1.0.0-alpha.3",
"medusa-interfaces": "^1.0.0-alpha.3",
@@ -1,6 +1,11 @@
import axios from "axios"
import rateLimit from "axios-rate-limit"
import qs from "querystring"
// Brightpearl allows 200 requests per minute
const RATE_LIMIT_REQUESTS = 200
const RATE_LIMIT_INTERVAL = 60 * 1000
class BrightpearlClient {
static createToken(account, data) {
const params = {
@@ -40,13 +45,16 @@ class BrightpearlClient {
}
constructor(options, onRefreshToken) {
this.client_ = axios.create({
baseURL: `https://${options.url}/public-api/${options.account}`,
headers: {
"brightpearl-app-ref": "medusa-dev",
"brightpearl-dev-ref": "sebrindom",
},
})
this.client_ = rateLimit(
axios.create({
baseURL: `https://${options.url}/public-api/${options.account}`,
headers: {
"brightpearl-app-ref": "medusa-dev",
"brightpearl-dev-ref": "sebrindom",
},
}),
{ maxRequests: RATE_LIMIT_REQUESTS, perMilliseconds: RATE_LIMIT_INTERVAL }
)
this.authType_ = options.auth_type
this.token_ = options.access_token