Add rate limiting
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.19.2",
|
"axios": "^0.19.2",
|
||||||
|
"axios-rate-limit": "^1.2.1",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"medusa-core-utils": "^1.0.0-alpha.3",
|
"medusa-core-utils": "^1.0.0-alpha.3",
|
||||||
"medusa-interfaces": "^1.0.0-alpha.3",
|
"medusa-interfaces": "^1.0.0-alpha.3",
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
import axios from "axios"
|
import axios from "axios"
|
||||||
|
import rateLimit from "axios-rate-limit"
|
||||||
import qs from "querystring"
|
import qs from "querystring"
|
||||||
|
|
||||||
|
// Brightpearl allows 200 requests per minute
|
||||||
|
const RATE_LIMIT_REQUESTS = 200
|
||||||
|
const RATE_LIMIT_INTERVAL = 60 * 1000
|
||||||
|
|
||||||
class BrightpearlClient {
|
class BrightpearlClient {
|
||||||
static createToken(account, data) {
|
static createToken(account, data) {
|
||||||
const params = {
|
const params = {
|
||||||
@@ -40,13 +45,16 @@ class BrightpearlClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor(options, onRefreshToken) {
|
constructor(options, onRefreshToken) {
|
||||||
this.client_ = axios.create({
|
this.client_ = rateLimit(
|
||||||
|
axios.create({
|
||||||
baseURL: `https://${options.url}/public-api/${options.account}`,
|
baseURL: `https://${options.url}/public-api/${options.account}`,
|
||||||
headers: {
|
headers: {
|
||||||
"brightpearl-app-ref": "medusa-dev",
|
"brightpearl-app-ref": "medusa-dev",
|
||||||
"brightpearl-dev-ref": "sebrindom",
|
"brightpearl-dev-ref": "sebrindom",
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
{ maxRequests: RATE_LIMIT_REQUESTS, perMilliseconds: RATE_LIMIT_INTERVAL }
|
||||||
|
)
|
||||||
|
|
||||||
this.authType_ = options.auth_type
|
this.authType_ = options.auth_type
|
||||||
this.token_ = options.access_token
|
this.token_ = options.access_token
|
||||||
|
|||||||
Reference in New Issue
Block a user