major refactoring

This commit is contained in:
Dave Horton
2020-01-25 11:47:33 -05:00
parent 621ea8c0f5
commit 4a1ea4e091
25 changed files with 947 additions and 933 deletions

View File

@@ -1,36 +1,45 @@
const request = require('request');
require('request-debug')(request);
//require('request-debug')(request);
const makeTask = require('../tasks/make_task');
const normalizeJamones = require('./normalize-jamones');
const debug = require('debug')('jambonz:feature-server');
function hooks(logger, callAttributes) {
debug(`notifiers: callAttributes ${JSON.stringify(callAttributes)}`);
function actionHook(url, method, opts) {
debug(`notifiers: opts ${JSON.stringify(opts)}`);
function actionHook(url, method, auth, opts, expectResponse = false) {
const params = Object.assign({}, callAttributes, opts);
const obj = {
url,
method,
json: true,
qs: 'GET' === method ? params : callAttributes,
body: 'POST' === method ? opts : null
};
logger.debug(`${method} ${url} sending ${JSON.stringify(obj)}`);
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');
return new Promise((resolve, reject) => {
request(obj, (err, response, body) => {
if (err) {
this.logger.info(`TaskDial:_actionHook error ${method} ${url}: ${err.message}`);
logger.info(`actionHook error ${method} ${url}: ${err.message}`);
return reject(err);
}
if (body) {
this.logger.debug(body, `TaskDial:_actionHook response ${method} ${url}`);
logger.debug(body, `actionHook response ${method} ${url}`);
if (expectResponse) {
const tasks = normalizeJamones(logger, body).map((tdata) => makeTask(logger, tdata));
return resolve(tasks);
}
}
resolve(body);
});
});
}
function notifyHook(url, method, auth, opts) {
return actionHook(url, method, auth, opts, false);
}
return {
actionHook
actionHook,
notifyHook
};
}