-
Notifications
You must be signed in to change notification settings - Fork 1
/
ampapi.js
79 lines (69 loc) · 2.48 KB
/
ampapi.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
exports.AMPAPI = function (baseUri) {
const request = require('request');
var self = this;
this.baseUri = baseUri;
this.sessionId = "";
this.dataSource = "";
this.API = { Core: { GetAPISpec: [] } };
this.initAsync = async function (stage2) {
this.dataSource = (baseUri.endsWith("/") ?
baseUri.substring(0, baseUri.lastIndexOf("/")) :
baseUri)
+ "/API";
for (const module of Object.keys(this.API)) {
var methods = this.API[module];
this[module] = {};
for (const method of Object.keys(methods)) {
this[module][method + "Async"] = function (params) {
var args = Array.prototype.slice.call(arguments, 0);
return self.APICall(module, method, args);
};
}
}
if (stage2) {
return true;
}
else {
var result = await this.Core.GetAPISpecAsync();
if (result != null) {
this.API = result;
return await this.initAsync(true);
}
else {
return false;
}
}
}
this.APICall = function (module, methodName, args) {
var data = {};
var methodParams = this.API[module][methodName].Parameters;
var methodParamsLength = methodParams != null ? methodParams.length : 0;
for (var a = 0; a < methodParamsLength; a++) {
var argName = methodParams[a].Name;
var val = args[a];
data[argName] = val;
}
var URI = `${this.dataSource}/${module}/${methodName}`;
data.SESSIONID = this.sessionId;
return new Promise(function (resolve, reject) {
request.post({
headers: { "Accept": "application/vnd.cubecoders-ampapi", "User-Agent": "CCL/AMPAPI-NodeJS" },
url: URI,
body: JSON.stringify(data),
}, function (error, res, body) {
if (error) {
reject(error, res);
}
else if (!error) {
var result = JSON.parse(body);
if (result != null && Object.keys(result).length === 1 && result.result != undefined) {
resolve(result.result);
}
else {
resolve(result);
}
}
});
});
}
};