Add rate limiting
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user