This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
126 lines (118 loc) · 4.01 KB
/
client.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
123
124
125
126
const axios = require("axios").default;
const errors = require("./errors");
const pkg = require("../package.json");
const { Dashboard } = require("./dashboard.js");
const { PiggyBox } = require("./piggybox.js");
class Client {
/**
* The EarnApp client
* @example const client = new Client();
*/
constructor() {
/**
* The client API URL
* @type {string}
*/
this.CLIENT_API_URL = "https://client.earnapp.com";
/**
* The EarnApp dashboard client
* @type {Dashboard}
*/
this.dashboard = new Dashboard();
/**
* The PiggyBox dashboard client
* @type {PiggyBox}
*/
this.piggybox = new PiggyBox();
}
/**
* Make request against the API
* @private
* @param {string} method HTTP method
* @param {string} path API endpoint
* @param {Object} [reqOptions] request options
* @private
* @returns {Promise<Object>} promise
*/
#request(method, path, reqOptions = {}) {
let options = {
method,
url: `${this.CLIENT_API_URL}/${path}`,
headers: {
"User-Agent": `earnapp.js ${pkg.version} (https://github.com/LockBlock-dev/earnapp.js)`,
"Content-Type": "application/json",
},
...reqOptions,
};
return axios(options)
.then((response) => {
if (typeof response.data === "object") {
return response.data;
} else {
try {
return { status: response.data.toLowerCase() };
} catch (err) {
throw new errors.ParseError(
response.data,
response.status,
options.method,
options.url
);
}
}
})
.catch((error) => {
throw error.type === "ParseError"
? error
: new errors.APIError(
error,
error.response,
error.response.status,
options.method,
options.url
);
});
}
/**
* Register a new device on EarnApp database.
* Use client.linkDevice to link it to your account.
* @param {string} uuid device uuid
* @param {string} version app version
* @param {string} arch device arch
* @param {string} appid app id
* @example client.registerDevice("sdk-win-7744606f9f7b42d5b99d11e80f70886c", "1.295.874", "x64", "win_earnapp.com");
* @example client.registerDevice("sdk-node-6fd29568de6f481887ccf0ddea29dcca", "1.293.301", "x64", "node_earnapp.com");
* @returns {Promise<Object>}
*/
registerDevice(uuid, version, arch, appid) {
return this.#request("POST", "install_device", {
data: {
uuid: uuid,
version: version,
arch: arch || process.arch,
appid: appid,
},
});
}
/**
* Get a device stats on EarnApp database.
* @param {string} uuid device uuid
* @param {string} version app version
* @param {string} arch device arch
* @param {string} appid app id
* @example client.deviceStats("sdk-win-7744606f9f7b42d5b99d11e80f70886c", "1.295.874", "x64", "win_earnapp.com");
* @example client.deviceStats("sdk-node-6fd29568de6f481887ccf0ddea29dcca", "1.293.301", "x64", "node_earnapp.com");
* @returns {Promise<Object>}
*/
deviceStats(uuid, version, arch, appid) {
return this.#request("POST", "app_config_win.json", {
params: {
uuid: uuid,
version: version,
arch: arch || process.arch,
appid: appid,
},
});
}
}
module.exports = { Client };