@eiannone/tesla-api 1.15.0 → 1.15.3
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 +16 -60
- 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;
|
|
@@ -129,49 +130,7 @@ class TeslaApi {
|
|
|
129
130
|
return this.#apiCall(vid + "/command/" + command, "POST", params);
|
|
130
131
|
}
|
|
131
132
|
|
|
132
|
-
async #oauthCall(params
|
|
133
|
-
return new Promise((resolve, reject) => {
|
|
134
|
-
const postData = JSON.stringify(params);
|
|
135
|
-
const req = request(BASE_URL + '/oauth/token', {
|
|
136
|
-
headers: {
|
|
137
|
-
'user-agent': "TeslaEma",
|
|
138
|
-
'Authorization': "Bearer " + bearer_token,
|
|
139
|
-
'Content-Type': 'application/json',
|
|
140
|
-
'Content-Length': postData.length
|
|
141
|
-
},
|
|
142
|
-
timeout: 30000,
|
|
143
|
-
method: 'POST'
|
|
144
|
-
}, res => {
|
|
145
|
-
if (res.statusCode > 199 && res.statusCode < 300) {
|
|
146
|
-
res.setEncoding('utf8');
|
|
147
|
-
let rawData = '';
|
|
148
|
-
res.on('data', chunk => { rawData += chunk; });
|
|
149
|
-
res.on('end', () => {
|
|
150
|
-
try {
|
|
151
|
-
resolve(JSON.parse(rawData));
|
|
152
|
-
} catch(err) {
|
|
153
|
-
reject(new ApiError(err));
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
} else {
|
|
157
|
-
let errMsg = res.statusMessage + " ("+res.statusCode+")";
|
|
158
|
-
reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
req.on('error', e => {
|
|
162
|
-
// Error code examples:
|
|
163
|
-
// - EAI_AGAIN (DNS lookup timeout)
|
|
164
|
-
// - ECONNRESET
|
|
165
|
-
// - ECONNREFUSED
|
|
166
|
-
// - ENOTFOUND
|
|
167
|
-
reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
|
|
168
|
-
});
|
|
169
|
-
req.write(postData);
|
|
170
|
-
req.end();
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async #oauthCall2(params) {
|
|
133
|
+
async #oauthCall(params) {
|
|
175
134
|
return new Promise((resolve, reject) => {
|
|
176
135
|
const postData = JSON.stringify(params);
|
|
177
136
|
const req = request('https://auth.tesla.com/oauth2/v3/token', {
|
|
@@ -217,29 +176,26 @@ class TeslaApi {
|
|
|
217
176
|
}
|
|
218
177
|
|
|
219
178
|
// https://tesla-api.timdorr.com/api-basics/authentication
|
|
220
|
-
async refreshToken(refresh_token) {
|
|
221
|
-
const payLoad = {
|
|
222
|
-
grant_type: 'refresh_token',
|
|
223
|
-
client_id: 'ownerapi',
|
|
224
|
-
refresh_token,
|
|
225
|
-
scope: 'openid email offline_access'
|
|
226
|
-
};
|
|
179
|
+
async refreshToken(refresh_token, retry = 1) {
|
|
227
180
|
try {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
this.token = resp.access_token;
|
|
181
|
+
const oauth = await this.#oauthCall({
|
|
182
|
+
grant_type: 'refresh_token',
|
|
183
|
+
client_id: 'ownerapi',
|
|
184
|
+
refresh_token,
|
|
185
|
+
scope: 'openid email offline_access'
|
|
186
|
+
});
|
|
187
|
+
this.refresh_token = oauth.refresh_token;
|
|
188
|
+
this.token = oauth.access_token;
|
|
237
189
|
if (typeof this.cb_refreshToken == 'function') {
|
|
238
190
|
this.cb_refreshToken(this.token, this.refresh_token);
|
|
239
191
|
}
|
|
240
192
|
return oauth;
|
|
241
193
|
}
|
|
242
194
|
catch(error) {
|
|
195
|
+
if (retry < 3) {
|
|
196
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
197
|
+
return this.refreshToken(refresh_token. retry + 1);
|
|
198
|
+
}
|
|
243
199
|
if (error instanceof Error) error.message += " - Unable to refresh Token";
|
|
244
200
|
throw error;
|
|
245
201
|
}
|