-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.ts
86 lines (79 loc) · 2.05 KB
/
query.ts
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
import { Command, flags } from '@oclif/command';
import axios from 'axios';
import * as inquirer from 'inquirer';
import { v4 as uuid } from 'uuid';
import { QueryRequest } from '../entities/QueryRequest';
import { readConfig } from '../util/configUtil';
export default class Query extends Command {
static description = 'Sends a QUERY request intent';
static flags = {
token: flags.string({
char: 't',
description: 'oauth access token',
env: 'token',
required: true,
}),
uri: flags.string({
char: 'u',
description: 'uri of the service',
env: 'uri',
required: true,
}),
id: flags.string({
char: 'i',
description: 'id to query',
env: 'id',
required: false,
}),
help: flags.help({ char: 'h' }),
};
async run() {
const { flags } = this.parse(Query);
const requestId = uuid();
const deviceId =
flags.id ||
(await this.promptId().catch(() => {
this.error('Please run sync first or provide arguments.');
}));
const queryBody: QueryRequest = {
requestId,
inputs: [
{
intent: 'action.devices.QUERY',
payload: {
devices: [{ id: deviceId }],
},
},
],
};
await axios
.post(flags.uri, queryBody, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${flags.token}`,
},
responseType: 'json',
})
.then(
(response) => {
this.log(JSON.stringify(response.data, null, 2));
},
(error) => {
this.log(`Request ${requestId} failed with:`);
this.log(JSON.stringify(error.response.data, null, 2));
},
);
}
async promptId(): Promise<string> {
const syncedDevices = await readConfig(this.config.configDir);
const responses = await inquirer.prompt([
{
name: 'id',
message: 'select a device id',
type: 'list',
choices: syncedDevices.ids,
},
]);
return responses.id;
}
}