@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.
Files changed (2) hide show
  1. package/TeslaApi.js +34 -23
  2. 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('https://auth.tesla.com/oauth2/v3/token', {
145
- headers: {
146
- 'user-agent': "TeslaEma",
147
- 'Content-Type': 'application/json',
148
- 'Content-Length': postData.length
149
- },
150
- timeout: 30000,
151
- method: 'POST',
152
- minVersion: 'TLSv1.3',
153
- maxVersion: 'TLSv1.3'
154
- }, res => {
155
- if (res.statusCode > 199 && res.statusCode < 300) {
156
- res.setEncoding('utf8');
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
- res.on('data', chunk => { rawData += chunk; });
159
- res.on('end', () => {
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 = res.statusMessage + " ("+res.statusCode+")";
168
- reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
179
+ let errMsg = headers[':status'] + "";
180
+ reject(new ApiError(errMsg, this.#decodeStatus(statusCode)));
181
+ session.close();
169
182
  }
170
183
  });
171
- req.on('error', e => {
172
- // Error code examples:
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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",