Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 70 additions & 69 deletions ampapi.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
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";
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 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);
};
}
}
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 = {};
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;
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;
for (var a = 0; a < methodParamsLength; a++) {
var argName = methodParams[a].Name;

var val = args[a];
var val = args[a];

data[argName] = val;
}
data[argName] = val;
}

var URI = `${this.dataSource}/${module}/${methodName}`;
data.SESSIONID = this.sessionId;
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);
}
}
});
});
}
};
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) {
let result;
try {
result = JSON.parse(body);
} catch (e) {
// Patch: If not valid JSON, reject cleanly instead of crashing the app.
return reject(new Error(`AMPAPI: Invalid JSON response from ${URI}: ${body.slice(0, 100)}`));
}
if (result != null && Object.keys(result).length === 1 && result.result != undefined) {
resolve(result.result);
} else {
resolve(result);
}
}
}
);
});
};
};