major revamp of http client functionalit

This commit is contained in:
Dave Horton
2020-02-14 12:45:28 -05:00
parent ff531e6964
commit 446000ee97
35 changed files with 906 additions and 433 deletions

View File

@@ -1,9 +1,9 @@
//const debug = require('debug')('jambonz:feature-server');
const uuidv4 = require('uuid/v4');
const {CallDirection} = require('./utils/constants');
const CallInfo = require('./session/call-info');
const retrieveApp = require('./utils/retrieve-app');
const parseUrl = require('parse-url');
const Requestor = require('./utils/requestor');
const makeTask = require('./tasks/make_task');
const normalizeJamones = require('./utils/normalize-jamones');
module.exports = function(srf, logger) {
const {lookupAppByPhoneNumber, lookupApplicationBySid} = srf.locals.dbHelpers;
@@ -50,12 +50,9 @@ module.exports = function(srf, logger) {
const logger = req.locals.logger;
try {
let app;
if (req.locals.application_sid) {
app = await lookupApplicationBySid(req.locals.application_sid);
}
else {
app = await lookupAppByPhoneNumber(req.locals.calledNumber);
}
if (req.locals.application_sid) app = await lookupApplicationBySid(req.locals.application_sid);
else app = await lookupAppByPhoneNumber(req.locals.calledNumber);
if (!app || !app.call_hook || !app.call_hook.url) {
logger.info(`rejecting call to ${req.locals.calledNumber}: no application or webhook url`);
return res.send(480, {
@@ -65,6 +62,14 @@ module.exports = function(srf, logger) {
});
}
/**
* create a requestor that we will use for all http requests we make during the call.
* also create a notifier for call status events (if not needed, its a no-op).
*/
app.requestor = new Requestor(this.logger, app.call_hook);
if (app.call_status_hook) app.notifier = new Requestor(this.logger, app.call_status_hook);
else app.notifier = {request: () => {}};
req.locals.application = app;
logger.debug(app, `retrieved application for ${req.locals.calledNumber}`);
req.locals.callInfo = new CallInfo({req, app, direction: CallDirection.Inbound});
@@ -81,26 +86,13 @@ module.exports = function(srf, logger) {
async function invokeWebCallback(req, res, next) {
const logger = req.locals.logger;
const app = req.locals.application;
const call_hook = app.call_hook;
const method = call_hook.method.toUpperCase();
let auth;
if (call_hook.username && call_hook.password) {
auth = {username: call_hook.username, password: call_hook.password};
}
try {
const u = parseUrl(call_hook.url);
const myPort = u.port ? `:${u.port}` : '';
app.originalRequest = {
baseUrl: `${u.protocol}://${u.resource}${myPort}`,
auth,
method
};
logger.debug({url: call_hook.url, method}, 'invokeWebCallback');
const obj = Object.assign({}, req.locals.callInfo);
// if the call hook is a POST add the entire SIP message to the payload
if (method === 'POST') obj.sip = req.msg;
app.tasks = await retrieveApp(logger, call_hook.url, method, auth, obj);
/* retrieve the application to execute for this inbound call */
const params = Object.assign(app.call_hook.method === 'POST' ? {sip: req.msg} : {},
req.locals.callInfo);
const json = await app.requestor.request(app.call_hook, params);
app.tasks = normalizeJamones(logger, json).map((tdata) => makeTask(logger, tdata));
if (0 === app.tasks.length) throw new Error('no application provided');
next();
} catch (err) {
logger.info(`Error retrieving or parsing application: ${err.message}`);