|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var url = require('url');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
var createId = require('uuid').v4;
|
|
|
|
|
@ -7,6 +8,7 @@ var assert = require('chai').assert;
|
|
|
|
|
var userAgent = require('superagent');
|
|
|
|
|
var Response = require('./response').Response;
|
|
|
|
|
var ResponseError = require('./error').ResponseError;
|
|
|
|
|
var logger = require('debug-logger')('janus:admin');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @class
|
|
|
|
|
@ -15,15 +17,15 @@ class Admin {
|
|
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
|
assert.property(options, 'url', 'Missing option url');
|
|
|
|
|
assert(validator.isURL(options.url), 'Invalid url');
|
|
|
|
|
assert(validator.isURL(options.url), 'Invalid url', options.url);
|
|
|
|
|
assert.property(options, 'secret');
|
|
|
|
|
this.url = options.url;
|
|
|
|
|
this.url = url.parse(options.url);
|
|
|
|
|
this.secret = options.secret;
|
|
|
|
|
this.userAgent = options.userAgent || userAgent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeUrl(path) {
|
|
|
|
|
return this.url + path;
|
|
|
|
|
return this.url.format() + path.replace(/^\//g, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request(req, options) {
|
|
|
|
|
@ -31,10 +33,12 @@ class Admin {
|
|
|
|
|
var path = _.get(options, 'path', '/admin');
|
|
|
|
|
req.admin_secret = this.secret;
|
|
|
|
|
req.transaction = createId();
|
|
|
|
|
logger.debug('Request', req);
|
|
|
|
|
this.userAgent.post(this.makeUrl(path)).type('json').send(req).end((err, res)=>{
|
|
|
|
|
if(_.isObject(err)) {
|
|
|
|
|
return reject(err);
|
|
|
|
|
}
|
|
|
|
|
logger.debug('Response', res.body);
|
|
|
|
|
var response = new Response(req, res.body);
|
|
|
|
|
if(response.isSuccess()) {
|
|
|
|
|
resolve(response);
|
|
|
|
|
|