-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
122 lines (104 loc) · 2.93 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const readline = require('readline');
const axios = require('axios');
const { API_BASE_URL } = require('./constants');
async function postRequest(url, data, accessToken) {
const headers = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers['Authorization'] = `Bearer ${accessToken}`;
}
try {
const response = await axios.post(url, data, { headers });
return response.data;
} catch (error) {
console.log(`Failed to submit POST request to ${url}: ${error}`);
return null;
}
}
async function getRequest(url, accessToken) {
try {
const response = await axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
});
return response.data;
} catch (error) {
console.log(`Failed to submit GET request to ${url}: ${error}`);
return null;
}
}
function getURLParameterValue(url, paramName) {
const urlParams = new URLSearchParams(new URL(url).search);
return urlParams.get(paramName);
}
function base64URLEncode(str) {
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
async function readLine(prompt) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
rl.question(prompt, (answer) => {
rl.close();
resolve(answer);
});
});
}
function getRandomString(length) {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let randomString = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
randomString += charset.charAt(randomIndex);
}
return randomString;
}
async function wakeVehicle(accessToken, vehicleId) {
console.log('Waking vehicle...');
try {
await postRequest(`${API_BASE_URL}/vehicles/${vehicleId}/wake_up`, {}, accessToken);
} catch (error) {
console.log(`Failed to wake vehicle: ${error}`);
}
}
async function getVehicle(accessToken) {
console.log('Looking up vehicle...');
try {
const response = await getRequest(`${API_BASE_URL}/vehicles`, accessToken);
const vehicle = response.response[0];
console.log(`Found vehicle "${vehicle.display_name}" with ID ${vehicle.id_s}`);
return vehicle;
} catch (error) {
return null;
}
}
async function getVehicleData(accessToken, vehicleId) {
console.log('Fetching vehicle data...');
try {
const vehicleDataResponse = await getRequest(`${API_BASE_URL}/vehicles/${vehicleId}/vehicle_data`, accessToken);
return vehicleDataResponse.response;
} catch (error) {
console.log(`Failed to fetch vehicle data: ${error}`);
return null;
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = {
getRandomString,
getRequest,
getURLParameterValue,
base64URLEncode,
postRequest,
readLine,
wakeVehicle,
getVehicle,
getVehicleData,
sleep
}