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,17 +1,18 @@
const config = require('config');
const router = require('express').Router();
const sysError = require('./error');
const makeTask = require('../../tasks/make_task');
const RestCallSession = require('../../session/rest-call-session');
const CallInfo = require('../../session/call-info');
const {CallDirection, CallStatus} = require('../../utils/constants');
const parseUrl = require('parse-url');
const SipError = require('drachtio-srf').SipError;
const Srf = require('drachtio-srf');
const sysError = require('./error');
const drachtio = config.get('outdials.drachtio');
const sbcs = config.get('outdials.sbc');
const Mrf = require('drachtio-fsmrf');
const installSrfLocals = require('../../utils/install-srf-locals');
const Requestor = require('./utils/requestor');
let idxDrachtio = 0;
let idxSbc = 0;
let srfs = [];
@@ -57,41 +58,16 @@ function getSrfForOutdial(logger) {
});
}
async function validate(logger, payload) {
const data = Object.assign({}, {
from: payload.from,
to: payload.to,
call_hook: payload.call_hook
});
const u = parseUrl(payload.call_hook.url);
const myPort = u.port ? `:${u.port}` : '';
payload.originalRequest = {
baseUrl: `${u.protocol}://${u.resource}${myPort}`,
method: payload.call_hook.method
};
if (payload.call_hook.username && payload.call_hook.password) {
payload.originalRequest.auth = {
username: payload.call_hook.username,
password: payload.call_hook.password
};
}
return makeTask(logger, {'rest:dial': data});
}
router.post('/', async(req, res) => {
const logger = req.app.locals.logger;
logger.debug({body: req.body}, 'got createCall request');
try {
let uri, cs, to;
const restDial = await validate(logger, req.body);
const restDial = makeTask(logger, {'rest:dial': req.body});
const sbcAddress = sbcs[idxSbc++ % sbcs.length];
const srf = await getSrfForOutdial(logger);
const target = restDial.to;
const opts = {
'callingNumber': restDial.from
};
const opts = { callingNumber: restDial.from };
switch (target.type) {
case 'phone':
@@ -130,8 +106,24 @@ router.post('/', async(req, res) => {
localSdp: ep.local.sdp
});
if (target.auth) opts.auth = this.target.auth;
const application = req.body;
/**
* create our application object -
* not from the database as per an inbound call,
* but from the provided params in the request
*/
const app = req.body;
/**
* attach our requestor and notifier objects
* these will be used for all http requests we make during this call
*/
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: () => {}};
/* now launch the outdial */
try {
const dlg = await srf.createUAC(uri, opts, {
cbRequest: (err, inviteReq) => {
@@ -140,18 +132,18 @@ router.post('/', async(req, res) => {
res.status(500).send('Call Failure');
ep.destroy();
}
/* ok our outbound NVITE is in flight */
/* call is in flight */
const tasks = [restDial];
const callInfo = new CallInfo({
direction: CallDirection.Outbound,
req: inviteReq,
to,
tag: req.body.tag,
tag: app.tag,
accountSid: req.body.account_sid,
applicationSid: req.body.application_sid
applicationSid: app.application_sid
});
cs = new RestCallSession({logger, application, srf, req: inviteReq, ep, tasks, callInfo});
cs = new RestCallSession({logger, application: app, srf, req: inviteReq, ep, tasks, callInfo});
cs.exec(req);
res.status(201).json({sid: cs.callSid});
@@ -191,7 +183,6 @@ router.post('/', async(req, res) => {
} catch (err) {
sysError(logger, res, err);
}
});
module.exports = router;

View File

@@ -31,6 +31,10 @@ function retrieveCallSession(callSid, opts) {
return cs;
}
/**
* update a call
*/
router.post('/:callSid', async(req, res) => {
const logger = req.app.locals.logger;
const callSid = req.params.callSid;
@@ -42,7 +46,7 @@ router.post('/:callSid', async(req, res) => {
return res.sendStatus(404);
}
res.sendStatus(202);
cs.updateCall(req.body);
cs.updateCall(req.body, callSid);
} catch (err) {
sysError(logger, res, err);
}