@eiannone/tesla-api 1.15.1 → 1.15.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TeslaApi.js +7 -2
- package/package.json +1 -1
package/TeslaApi.js
CHANGED
|
@@ -37,6 +37,7 @@ class TeslaApi {
|
|
|
37
37
|
case 401: return ApiError.UNAUTHORIZED;
|
|
38
38
|
case 404: return ApiError.NO_VEHICLE;
|
|
39
39
|
case 405: return ApiError.IN_SERVICE;
|
|
40
|
+
case 406: return ApiError.NETWORK; // Not Acceptable
|
|
40
41
|
case 408: return ApiError.UNAVAILABLE;
|
|
41
42
|
case 500: return ApiError.SERVER;
|
|
42
43
|
case 502: return ApiError.NETWORK; // Bad gateway
|
|
@@ -79,7 +80,7 @@ class TeslaApi {
|
|
|
79
80
|
if (res.statusCode == 401 && this.refresh_token != null) {
|
|
80
81
|
// Tries to refresh the tokens
|
|
81
82
|
this.refreshToken(this.refresh_token)
|
|
82
|
-
.then(_ => this.#apiCall(path, method))
|
|
83
|
+
.then(_ => this.#apiCall(path, method, params))
|
|
83
84
|
.then(response => resolve(response))
|
|
84
85
|
.catch(error => { reject(error); });
|
|
85
86
|
return;
|
|
@@ -175,7 +176,7 @@ class TeslaApi {
|
|
|
175
176
|
}
|
|
176
177
|
|
|
177
178
|
// https://tesla-api.timdorr.com/api-basics/authentication
|
|
178
|
-
async refreshToken(refresh_token) {
|
|
179
|
+
async refreshToken(refresh_token, retry = 1) {
|
|
179
180
|
try {
|
|
180
181
|
const oauth = await this.#oauthCall({
|
|
181
182
|
grant_type: 'refresh_token',
|
|
@@ -191,6 +192,10 @@ class TeslaApi {
|
|
|
191
192
|
return oauth;
|
|
192
193
|
}
|
|
193
194
|
catch(error) {
|
|
195
|
+
if (retry < 3) {
|
|
196
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
197
|
+
return this.refreshToken(refresh_token, retry + 1);
|
|
198
|
+
}
|
|
194
199
|
if (error instanceof Error) error.message += " - Unable to refresh Token";
|
|
195
200
|
throw error;
|
|
196
201
|
}
|