Skip to content

Commit a21e5d4

Browse files
committed
Use offical API client
1 parent a21dc59 commit a21e5d4

File tree

5 files changed

+3444
-1034
lines changed

5 files changed

+3444
-1034
lines changed

lib/cloud.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
const OpenAPI = require('@tuyapi/openapi');
1+
const {TuyaContext} = require('@tuya/tuya-connector-nodejs');
2+
const {regionToUrl} = require('./helpers');
23

34
const list = async options => {
4-
const api = new OpenAPI({key: options.apiKey, secret: options.apiSecret, schema: options.schema, region: options.region});
5+
const api = new TuyaContext({
6+
baseUrl: regionToUrl(options.region),
7+
accessKey: options.apiKey,
8+
secretKey: options.apiSecret
9+
});
510

6-
await api.getToken();
11+
const result = await api.request({
12+
method: 'GET',
13+
path: '/v1.0/devices',
14+
query: {
15+
page_no: 0,
16+
page_size: 1000,
17+
schema: options.schema
18+
}
19+
});
720

8-
const {devices} = await api.getDevices();
21+
if (!result.success) {
22+
throw new Error(`${result.code}: ${result.msg}`);
23+
}
24+
25+
const {devices} = result.result;
926

1027
if (options.stringify) {
1128
console.log(JSON.stringify(devices));

lib/helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
regionToUrl: region => `https://openapi.tuya${region}.com`
3+
};

lib/wizard.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const OpenAPI = require('@tuyapi/openapi');
1+
const {TuyaContext} = require('@tuya/tuya-connector-nodejs');
22
const inquirer = require('inquirer');
33
const colors = require('colors');
44
const any = require('promise.any');
55
const AggregateError = require('es-aggregate-error/polyfill')();
6+
const {regionToUrl} = require('./helpers');
67

78
const REGIONS = ['eu', 'us', 'cn', 'in'];
89

@@ -56,28 +57,31 @@ const list = async (conf, options) => {
5657
}
5758

5859
// Get seed device
59-
let userId = null;
60+
let userId;
6061
let foundAPIRegion = savedAPIRegion;
6162

6263
try {
63-
if (savedAPIRegion && useExistingKeys) {
64-
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: savedAPIRegion});
65-
await api.getToken();
66-
const device = await api.getDevice(answers.deviceId);
64+
const {device, region} = await any((savedAPIRegion ? [savedAPIRegion] : REGIONS).map(async region => {
65+
const api = new TuyaContext({
66+
baseUrl: regionToUrl(region),
67+
accessKey: answers.apiKey,
68+
secretKey: answers.apiSecret
69+
});
70+
71+
const result = await api.request({
72+
method: 'GET',
73+
path: `/v1.0/devices/${answers.deviceId}`
74+
});
75+
76+
if (!result.success) {
77+
throw new Error(`${result.code}: ${result.msg}`);
78+
}
6779

68-
userId = device.uid;
69-
} else {
70-
const {device, region} = await any(REGIONS.map(async region => {
71-
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region});
72-
await api.getToken();
73-
const device = await api.getDevice(answers.deviceId);
80+
return {device: result.result, region};
81+
}));
7482

75-
return {device, region};
76-
}));
77-
78-
userId = device.uid;
79-
foundAPIRegion = region;
80-
}
83+
userId = device.uid;
84+
foundAPIRegion = region;
8185
} catch (error) {
8286
if (process.env.DEBUG) {
8387
if (error.constructor === AggregateError) {
@@ -94,13 +98,23 @@ const list = async (conf, options) => {
9498
}
9599

96100
// Get user devices
97-
const api = new OpenAPI({key: answers.apiKey, secret: answers.apiSecret, region: foundAPIRegion});
98-
await api.getToken();
101+
const api = new TuyaContext({
102+
baseUrl: regionToUrl(savedAPIRegion),
103+
accessKey: answers.apiKey,
104+
secretKey: answers.apiSecret
105+
});
106+
107+
const result = await api.request({
108+
method: 'GET',
109+
path: `/v1.0/users/${userId}/devices`
110+
});
99111

100-
const devices = await api.getDevicesByUser(userId);
112+
if (!result.success) {
113+
throw new Error(`${result.code}: ${result.msg}`);
114+
}
101115

102116
const groupedDevices = {};
103-
for (const device of devices) {
117+
for (const device of result.result) {
104118
if (device.node_id) {
105119
if (!groupedDevices[device.local_key] || !groupedDevices[device.local_key].subDevices) {
106120
groupedDevices[device.local_key] = {...groupedDevices[device.local_key], subDevices: []};

0 commit comments

Comments
 (0)