From 7955b79dfb3116e1ecb9b097080ef6f7b8651b60 Mon Sep 17 00:00:00 2001 From: TA2k Date: Thu, 28 Nov 2024 15:32:31 +0100 Subject: [PATCH] add retries --- main.js | 18 ++++++++++++++++++ package-lock.json | 36 ++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 55 insertions(+) diff --git a/main.js b/main.js index 7dfafde..2c74d99 100644 --- a/main.js +++ b/main.js @@ -14,6 +14,7 @@ const crypto = require('crypto'); const qs = require('qs'); const Json2iob = require('json2iob'); const tough = require('tough-cookie'); +const axiosRetry = require('axios-retry').default; class Bmw extends utils.Adapter { /** * @param {Partial} [options={}] @@ -168,6 +169,7 @@ class Bmw extends utils.Adapter { withCredentials: true, httpsAgent: new HttpsCookieAgent({ cookies: { jar: this.cookieJar } }), }); + axiosRetry(this.requestClient, { retries: 0 }); } /** @@ -1028,6 +1030,22 @@ class Bmw extends utils.Adapter { method: 'post', url: url, headers: headers, + 'axios-retry': { + retries: 3, + // only 403 rate limit + retryCondition: (error) => { + return error.response && error.response.status === 403; + }, + retryDelay: () => { + return 5000; + }, + onRetry: () => { + this.log.warn('Rate Limit exceeded, retry in 5 seconds'); + }, + onMaxRetryTimesExceeded: () => { + this.log.error('3 Retries failed'); + }, + }, }) .then((res) => { this.log.debug(JSON.stringify(res.data)); diff --git a/package-lock.json b/package-lock.json index 29832b1..44d521e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@iobroker/adapter-core": "^3.2.2", "axios": "^1.7.7", + "axios-retry": "^4.5.0", "http-cookie-agent": "^6.0.6", "json-bigint": "^1.0.0", "json2iob": "^2.6.12", @@ -722,6 +723,17 @@ "proxy-from-env": "^1.1.0" } }, + "node_modules/axios-retry": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-4.5.0.tgz", + "integrity": "sha512-aR99oXhpEDGo0UuAlYcn2iGRds30k366Zfa05XWScR9QaQD4JYiP3/1Qt1u7YlefUOK+cn0CcwoL1oefavQUlQ==", + "dependencies": { + "is-retry-allowed": "^2.2.0" + }, + "peerDependencies": { + "axios": "0.x || 1.x" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1861,6 +1873,17 @@ "node": ">=8" } }, + "node_modules/is-retry-allowed": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz", + "integrity": "sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", @@ -3450,6 +3473,14 @@ "proxy-from-env": "^1.1.0" } }, + "axios-retry": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-4.5.0.tgz", + "integrity": "sha512-aR99oXhpEDGo0UuAlYcn2iGRds30k366Zfa05XWScR9QaQD4JYiP3/1Qt1u7YlefUOK+cn0CcwoL1oefavQUlQ==", + "requires": { + "is-retry-allowed": "^2.2.0" + } + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -4235,6 +4266,11 @@ "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", "dev": true }, + "is-retry-allowed": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz", + "integrity": "sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==" + }, "is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", diff --git a/package.json b/package.json index 39b22bb..3d2c0b4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "dependencies": { "@iobroker/adapter-core": "^3.2.2", "axios": "^1.7.7", + "axios-retry": "^4.5.0", "http-cookie-agent": "^6.0.6", "json-bigint": "^1.0.0", "json2iob": "^2.6.12",