@eiannone/tesla-api 1.19.0 → 1.20.0
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 +34 -23
- package/package.json +1 -1
package/TeslaApi.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {request} from 'https';
|
|
2
|
+
import {connect} from 'http2';
|
|
2
3
|
|
|
3
4
|
const BASE_URL = "https://owner-api.teslamotors.com";
|
|
4
5
|
|
|
@@ -140,42 +141,52 @@ class TeslaApi {
|
|
|
140
141
|
|
|
141
142
|
async #oauthCall(params) {
|
|
142
143
|
return new Promise((resolve, reject) => {
|
|
144
|
+
|
|
145
|
+
const session = connect('https://auth.tesla.com', {
|
|
146
|
+
minVersion: 'TLSv1.3', maxVersion: 'TLSv1.3'
|
|
147
|
+
});
|
|
148
|
+
session.on('error', (err) => {
|
|
149
|
+
reject(new ApiError(err.message + " ("+err.code+")", ApiError.NETWORK));
|
|
150
|
+
session.destroy();
|
|
151
|
+
});
|
|
152
|
+
|
|
143
153
|
const postData = JSON.stringify(params);
|
|
144
|
-
const req = request(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
154
|
+
const req = session.request({
|
|
155
|
+
':method': 'POST',
|
|
156
|
+
':path': '/oauth2/v3/token',
|
|
157
|
+
'user-agent': 'TeslaEma',
|
|
158
|
+
'Content-Type': 'application/json',
|
|
159
|
+
'Content-Length': postData.length
|
|
160
|
+
}, {
|
|
161
|
+
timeout: 30000
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
req.on('response', (headers) => {
|
|
165
|
+
const statusCode = headers[':status'];
|
|
166
|
+
if (statusCode > 199 && statusCode < 300) {
|
|
157
167
|
let rawData = '';
|
|
158
|
-
|
|
159
|
-
|
|
168
|
+
req.setEncoding('utf8');
|
|
169
|
+
req.on('data', chunk => { rawData += chunk; });
|
|
170
|
+
req.on('end', () => {
|
|
160
171
|
try {
|
|
161
172
|
resolve(JSON.parse(rawData));
|
|
162
173
|
} catch(err) {
|
|
163
174
|
reject(new ApiError(err));
|
|
164
175
|
}
|
|
176
|
+
session.close();
|
|
165
177
|
});
|
|
166
178
|
} else {
|
|
167
|
-
let errMsg =
|
|
168
|
-
reject(new ApiError(errMsg, this.#decodeStatus(
|
|
179
|
+
let errMsg = headers[':status'] + "";
|
|
180
|
+
reject(new ApiError(errMsg, this.#decodeStatus(statusCode)));
|
|
181
|
+
session.close();
|
|
169
182
|
}
|
|
170
183
|
});
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// - EAI_AGAIN (DNS lookup timeout)
|
|
174
|
-
// - ECONNRESET
|
|
175
|
-
// - ECONNREFUSED
|
|
176
|
-
// - ENOTFOUND
|
|
184
|
+
|
|
185
|
+
req.on('error', (e) => {
|
|
177
186
|
reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
|
|
187
|
+
session.destroy();
|
|
178
188
|
});
|
|
189
|
+
|
|
179
190
|
req.write(postData);
|
|
180
191
|
req.end();
|
|
181
192
|
});
|