in scripts.v2/utils.js [59:123]
async function request(method, url, accessToken, body) {
let requestBody;
const headers = {
"If-Match": "*",
"Content-Type": "application/json",
"Authorization": accessToken
};
if (body) {
if (!body.properties) {
body = {
properties: body
}
}
requestBody = JSON.stringify(body);
headers["Content-Length"] = Buffer.byteLength(requestBody);
}
const options = {
port: 443,
method: method,
headers: headers
};
return new Promise((resolve, reject) => {
const req = https.request(url, options, (resp) => {
let chunks = [];
resp.on('data', (chunk) => {
chunks.push(chunk);
});
resp.on('end', () => {
let data = Buffer.concat(chunks).toString('utf8');
switch (resp.statusCode) {
case 200:
case 201:
data.startsWith("{") ? resolve(JSON.parse(data)) : resolve(data);
break;
case 404:
reject({ code: "NotFound", message: `Resource not found: ${url}` });
break;
case 401:
reject({ code: "Unauthorized", message: `Unauthorized. Make sure you correctly specified management API access token before running the script.` });
break;
case 403:
reject({ code: "Forbidden", message: `Looks like you are not allowed to perform this operation. Please check with your administrator.` });
break;
default:
reject({ code: "UnhandledError", message: `Could not complete request to ${url}. Status: ${resp.statusCode} ${resp.statusMessage}` });
}
});
});
req.on('error', (e) => {
reject(e);
});
if (requestBody) {
req.write(requestBody);
}
req.end();
});
}