add tag task and varioius cleanup

This commit is contained in:
Dave Horton
2020-01-29 15:27:20 -05:00
parent bed4fa1f42
commit 92acd50595
17 changed files with 278 additions and 111 deletions

View File

@@ -1,23 +1,36 @@
const request = require('request');
require('request-debug')(request);
const retrieveApp = require('./retrieve-app');
function hooks(logger, callAttributes) {
function actionHook(url, method, auth, opts, expectResponse = true) {
const params = Object.assign({}, callAttributes, opts);
let basicauth, qs, body;
if (auth && typeof auth === 'object' && Object.keys(auth) === 2) basicauth = auth;
if ('GET' === method.toUpperCase()) qs = params;
else body = params;
const obj = {url, method, auth: basicauth, json: expectResponse || !!body, qs, body};
logger.debug({opts: obj}, 'actionHook');
function hooks(logger, callInfo) {
function actionHook(hook, obj, expectResponse = true) {
const method = hook.method.toUpperCase();
const auth = (hook.username && hook.password) ?
{username: hook.username, password: hook.password} :
null;
const data = Object.assign({}, obj, callInfo);
if ('GET' === method) {
// remove customer data - only for POSTs since it might be quite complex
delete data.customerData;
}
const opts = {
url: hook.url,
method,
json: 'POST' === method || expectResponse
};
if (auth) obj.auth = auth;
if ('POST' === method) obj.body = data;
else obj.qs = data;
return new Promise((resolve, reject) => {
request(obj, (err, response, body) => {
request(opts, (err, response, body) => {
if (err) {
logger.info(`actionHook error ${method} ${url}: ${err.message}`);
logger.info(`actionHook error ${method} ${hook.url}: ${err.message}`);
return reject(err);
}
if (body && expectResponse) {
logger.debug(body, `actionHook response ${method} ${url}`);
logger.debug(body, `actionHook response ${method} ${hook.url}`);
return resolve(retrieveApp(logger, body));
}
resolve(body);
@@ -25,7 +38,7 @@ function hooks(logger, callAttributes) {
});
}
function notifyHook(url, method, auth, opts) {
function notifyHook(url, method, auth, opts = {}) {
return actionHook(url, method, auth, opts, false);
}