mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
major refactoring
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user