Comparing version 0.3.3 to 0.4.0
633
lib/plivo.js
@@ -5,28 +5,20 @@ //Get required modules | ||
var Request = require('request'); | ||
var qs = require('querystring'); | ||
var xmlBuilder = require('xmlbuilder'); | ||
var doc = xmlBuilder.create(); | ||
var plivoError = require('./plivoError'); | ||
var Response = require('./plivoResponse'); | ||
var plivo = {}; | ||
//var doc = xmlBuilder.create(''); | ||
plivo.options = {}; | ||
plivo.options.host = 'api.plivo.com'; | ||
plivo.options.version = 'v1'; | ||
plivo.options.authId = ''; | ||
plivo.options.authToken = ''; | ||
var plivo = function() { | ||
this.options = {}; | ||
this.options.host = 'api.plivo.com'; | ||
this.options.version = 'v1'; | ||
this.options.authId = ''; | ||
this.options.authToken = ''; | ||
this.UserAgent = 'NodePlivo'; | ||
this.plivoError = plivoError; | ||
}; | ||
var UserAgent = 'NodePlivo'; | ||
// Generic Plivo Error | ||
function PlivoError(msg) { | ||
Error.call(this); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.message = (msg || '') + '\n'; | ||
this.name = 'PlivoError'; | ||
} | ||
PlivoError.prototype = Error.prototype; | ||
// Main request function | ||
var request = function (action, method, params, callback, optional) { | ||
plivo.prototype.request = function (action, method, params, callback, optional) { | ||
if (optional) { | ||
@@ -42,9 +34,10 @@ if (typeof params != 'object') { | ||
if (!callback) { | ||
var callback = function() {}; | ||
var callback = function () {}; | ||
} | ||
var err = null; | ||
var path = 'https://' + plivo.options.host + '/' + plivo.options.version + '/Account/' + plivo.options.authId + '/' + action; | ||
var path = 'https://' + this.options.host + '/' + this.options.version + '/Account/' + this.options.authId + '/' + action; | ||
var auth = 'Basic ' + new Buffer(plivo.options.authId + ':' + plivo.options.authToken) | ||
var auth = 'Basic ' + new Buffer(this.options.authId + ':' + this.options.authToken) | ||
.toString('base64'); | ||
@@ -54,3 +47,3 @@ | ||
'Authorization': auth, | ||
'User-Agent': UserAgent, | ||
'User-Agent': this.UserAgent, | ||
'Content-Type': 'application/json' | ||
@@ -66,12 +59,9 @@ }; | ||
if (method == 'POST') { | ||
var body = JSON.stringify(params); | ||
request_options.json = true; | ||
request_options.body = body; | ||
request_options.json = params; | ||
Request.post(request_options, function (error, response, body) { | ||
if (error || !response) { | ||
return callback(500, body) | ||
return callback(500, body) | ||
} | ||
if (response.statusCode != 201) { | ||
err = new PlivoError(error); | ||
err = new plivoError(error); | ||
} | ||
@@ -91,6 +81,3 @@ callback(response.statusCode, body); | ||
} else if (method == 'PUT') { | ||
var body = JSON.stringify(params); | ||
request_options.json = true; | ||
request_options.body = body, | ||
request_options.json = params; | ||
Request.put(request_options, function (error, response, body) { | ||
@@ -102,7 +89,6 @@ callback(response.statusCode, body); | ||
// Exposing generic request functionality as well. | ||
plivo.request = request; | ||
// For verifying the plivo server signature | ||
plivo.create_signature = function (url, params) { | ||
plivo.prototype.create_signature = function (url, params) { | ||
var toSign = url; | ||
@@ -115,3 +101,3 @@ | ||
var signature = crypto | ||
.createHmac('sha1', plivo.options.authToken) | ||
.createHmac('sha1', this.options.authToken) | ||
.update(new Buffer(toSign, 'utf-8')) | ||
@@ -124,3 +110,3 @@ .digest('base64'); | ||
// Express middleware for verifying signature | ||
plivo.middleware = function(options) { | ||
plivo.prototype.middleware = function(options) { | ||
return function (req, res, next) { | ||
@@ -152,17 +138,17 @@ if (process.env.NODE_ENV === 'test') return next() | ||
plivo.make_call = function (params, callback) { | ||
plivo.prototype.make_call = function (params, callback) { | ||
var action = 'Call/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_cdrs = function (params, callback) { | ||
plivo.prototype.get_cdrs = function (params, callback) { | ||
var action = 'Call/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
plivo.get_cdr = function (params, callback) { | ||
plivo.prototype.get_cdr = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/'; | ||
@@ -172,6 +158,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_live_calls = function (params, callback) { | ||
plivo.prototype.get_live_calls = function (params, callback) { | ||
var action = 'Call/'; | ||
@@ -181,6 +167,6 @@ var method = 'GET'; | ||
params.status = 'live'; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
plivo.get_live_call = function (params, callback) { | ||
plivo.prototype.get_live_call = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/'; | ||
@@ -191,6 +177,6 @@ delete params.call_uuid; | ||
params.status = 'live'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.transfer_call = function (params, callback) { | ||
plivo.prototype.transfer_call = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/'; | ||
@@ -200,6 +186,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.hangup_all_calls = function (callback) { | ||
plivo.prototype.hangup_all_calls = function (callback) { | ||
var action = 'Call/'; | ||
@@ -209,6 +195,6 @@ var method = 'DELETE'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.hangup_call = function (params, callback) { | ||
plivo.prototype.hangup_call = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/'; | ||
@@ -218,6 +204,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.record = function (params, callback) { | ||
plivo.prototype.record = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Record/'; | ||
@@ -227,6 +213,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.record_stop = function (params, callback) { | ||
plivo.prototype.record_stop = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Record/'; | ||
@@ -236,6 +222,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.play = function (params, callback) { | ||
plivo.prototype.play = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Play/'; | ||
@@ -245,6 +231,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.play_stop = function (params, callback) { | ||
plivo.prototype.play_stop = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Play/'; | ||
@@ -254,6 +240,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.speak = function (params, callback) { | ||
plivo.prototype.speak = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Speak/'; | ||
@@ -263,6 +249,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.speak_stop = function (params, callback) { | ||
plivo.prototype.speak_stop = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/Speak/'; | ||
@@ -272,6 +258,6 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.send_digits = function (params, callback) { | ||
plivo.prototype.send_digits = function (params, callback) { | ||
var action = 'Call/' + params['call_uuid'] + '/DTMF/'; | ||
@@ -281,3 +267,3 @@ delete params.call_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -287,3 +273,3 @@ | ||
plivo.hangup_request = function (params, callback) { | ||
plivo.prototype.hangup_request = function (params, callback) { | ||
var action = 'Request/' + params['request_uuid'] + '/'; | ||
@@ -293,3 +279,3 @@ delete params.request_uuid; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -299,10 +285,10 @@ | ||
plivo.get_live_conferences = function (params, callback) { | ||
plivo.prototype.get_live_conferences = function (params, callback) { | ||
var action = 'Conference/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
plivo.get_live_conference = function (params, callback) { | ||
plivo.prototype.get_live_conference = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/'; | ||
@@ -312,13 +298,13 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.hangup_all_conferences = function (callback) { | ||
plivo.prototype.hangup_all_conferences = function (callback) { | ||
var action = 'Conference/'; | ||
var method = 'DELETE'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.hangup_conference = function (params, callback) { | ||
plivo.prototype.hangup_conference = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/'; | ||
@@ -328,6 +314,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.hangup_conference_member = function (params, callback) { | ||
plivo.prototype.hangup_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/'; | ||
@@ -338,6 +324,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.play_conference_member = function (params, callback) { | ||
plivo.prototype.play_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Play/'; | ||
@@ -348,6 +334,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.stop_play_conference_member = function (params, callback) { | ||
plivo.prototype.stop_play_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Play/'; | ||
@@ -358,6 +344,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.speak_conference_member = function (params, callback) { | ||
plivo.prototype.speak_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Speak/'; | ||
@@ -369,6 +355,6 @@ console.log(action); | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.stop_speak_conference_member = function (params, callback) { | ||
plivo.prototype.stop_speak_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Speak/'; | ||
@@ -379,6 +365,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.deaf_conference_member = function (params, callback) { | ||
plivo.prototype.deaf_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Deaf/'; | ||
@@ -389,6 +375,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.undeaf_conference_member = function (params, callback) { | ||
plivo.prototype.undeaf_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Deaf/'; | ||
@@ -399,6 +385,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.mute_conference_member = function (params, callback) { | ||
plivo.prototype.mute_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Mute/'; | ||
@@ -409,6 +395,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.unmute_conference_member = function (params, callback) { | ||
plivo.prototype.unmute_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Mute/'; | ||
@@ -419,6 +405,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.kick_conference_member = function (params, callback) { | ||
plivo.prototype.kick_conference_member = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Member/' + params['member_id'] + '/Kick/'; | ||
@@ -429,6 +415,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.record_conference = function (params, callback) { | ||
plivo.prototype.record_conference = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Record/'; | ||
@@ -438,6 +424,6 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.stop_record_conference = function (params, callback) { | ||
plivo.prototype.stop_record_conference = function (params, callback) { | ||
var action = 'Conference/' + params['conference_id'] + '/Record/'; | ||
@@ -447,30 +433,29 @@ delete params.conference_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
// Accounts | ||
plivo.get_account = function (params, callback) { | ||
plivo.prototype.get_account = function (params, callback) { | ||
var action = ''; | ||
var method = 'GET'; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
plivo.modify_account = function (params, callback) { | ||
plivo.prototype.modify_account = function (params, callback) { | ||
var action = ''; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_subaccounts = function (params, callback) { | ||
plivo.prototype.get_subaccounts = function (params, callback) { | ||
var action = 'Subaccount/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_subaccount = function (params, callback) { | ||
plivo.prototype.get_subaccount = function (params, callback) { | ||
var action = 'Subaccount/' + params['subauth_id'] + '/'; | ||
@@ -480,13 +465,13 @@ delete params.subauth_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_subaccount = function (params, callback) { | ||
plivo.prototype.create_subaccount = function (params, callback) { | ||
var action = 'Subaccount/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_subaccount = function (params, callback) { | ||
plivo.prototype.modify_subaccount = function (params, callback) { | ||
var action = 'Subaccount/' + params['subauth_id'] + '/'; | ||
@@ -496,6 +481,6 @@ delete params.subauth_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_subaccount = function (params, callback) { | ||
plivo.prototype.delete_subaccount = function (params, callback) { | ||
var action = 'Subaccount/' + params['subauth_id'] + '/'; | ||
@@ -505,3 +490,3 @@ delete params.subauth_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -511,10 +496,10 @@ | ||
plivo.get_applications = function (params, callback) { | ||
plivo.prototype.get_applications = function (params, callback) { | ||
var action = 'Application/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_application = function (params, callback) { | ||
plivo.prototype.get_application = function (params, callback) { | ||
var action = 'Application/' + params['app_id'] + '/'; | ||
@@ -524,13 +509,13 @@ delete params.app_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_application = function (params, callback) { | ||
plivo.prototype.create_application = function (params, callback) { | ||
var action = 'Application/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_application = function (params, callback) { | ||
plivo.prototype.modify_application = function (params, callback) { | ||
var action = 'Application/' + params['app_id'] + '/'; | ||
@@ -540,6 +525,6 @@ delete params.app_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_application = function (params, callback) { | ||
plivo.prototype.delete_application = function (params, callback) { | ||
var action = 'Application/' + params['app_id'] + '/'; | ||
@@ -549,14 +534,14 @@ delete params.app_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
// Recordings | ||
plivo.get_recordings = function (params, callback) { | ||
plivo.prototype.get_recordings = function (params, callback) { | ||
var action = 'Recording/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_recording = function (params, callback) { | ||
plivo.prototype.get_recording = function (params, callback) { | ||
var action = 'Recording/' + params['recording_id'] + '/'; | ||
@@ -566,6 +551,6 @@ delete params.recording_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_recording = function (params, callback) { | ||
plivo.prototype.delete_recording = function (params, callback) { | ||
var action = 'Recording/' + params['recording_id'] + '/'; | ||
@@ -575,3 +560,3 @@ delete params.recording_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -581,10 +566,10 @@ | ||
plivo.get_endpoints = function (params, callback) { | ||
plivo.prototype.get_endpoints = function (params, callback) { | ||
var action = 'Endpoint/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_endpoint = function (params, callback) { | ||
plivo.prototype.get_endpoint = function (params, callback) { | ||
var action = 'Endpoint/' + params['endpoint_id'] + '/'; | ||
@@ -594,13 +579,13 @@ delete params.endpoint_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_endpoint = function (params, callback) { | ||
plivo.prototype.create_endpoint = function (params, callback) { | ||
var action = 'Endpoint/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_endpoint = function (params, callback) { | ||
plivo.prototype.modify_endpoint = function (params, callback) { | ||
var action = 'Endpoint/' + params['endpoint_id'] + '/'; | ||
@@ -610,6 +595,6 @@ delete params.endpoint_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_endpoint = function (params, callback) { | ||
plivo.prototype.delete_endpoint = function (params, callback) { | ||
var action = 'Endpoint/' + params['endpoint_id'] + '/'; | ||
@@ -619,14 +604,14 @@ delete params.endpoint_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
// Numbers | ||
plivo.get_numbers = function (params, callback) { | ||
plivo.prototype.get_numbers = function (params, callback) { | ||
var action = 'Number/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_number_details = function (params, callback) { | ||
plivo.prototype.get_number_details = function (params, callback) { | ||
var action = 'Number/' + params['number'] + '/'; | ||
@@ -636,6 +621,6 @@ delete params.number; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.unrent_number = function (params, callback) { | ||
plivo.prototype.unrent_number = function (params, callback) { | ||
var action = 'Number/' + params['number'] + '/'; | ||
@@ -645,13 +630,13 @@ delete params.number; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_number_group = function (params, callback) { | ||
plivo.prototype.get_number_group = function (params, callback) { | ||
var action = 'AvailableNumberGroup/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_number_group_details = function (params, callback) { | ||
plivo.prototype.get_number_group_details = function (params, callback) { | ||
var action = 'AvailableNumberGroup/' + params['group_id'] + '/'; | ||
@@ -661,6 +646,6 @@ delete params.group_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.rent_from_number_group = function (params, callback) { | ||
plivo.prototype.rent_from_number_group = function (params, callback) { | ||
var action = 'AvailableNumberGroup/' + params['group_id'] + '/'; | ||
@@ -670,6 +655,6 @@ delete params.group_id; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
plivo.edit_number = function (params, callback) { | ||
plivo.prototype.edit_number = function (params, callback) { | ||
var action = 'Number/' + params['number'] + '/'; | ||
@@ -679,10 +664,10 @@ delete params.number; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.link_application_number = function (params, callback) { | ||
plivo.prototype.link_application_number = function (params, callback) { | ||
this.edit_number(params, callback); | ||
}; | ||
plivo.unlink_application_number = function (params, callback) { | ||
plivo.prototype.unlink_application_number = function (params, callback) { | ||
params.app_id = null; | ||
@@ -692,10 +677,10 @@ this.edit_number(params, callback); | ||
plivo.search_phone_numbers = function (params, callback) { | ||
plivo.prototype.search_phone_numbers = function (params, callback) { | ||
var action = 'PhoneNumber/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.buy_phone_number = function (params, callback) { | ||
plivo.prototype.buy_phone_number = function (params, callback) { | ||
var action = 'PhoneNumber/' + params['number'] + '/'; | ||
@@ -705,21 +690,21 @@ delete params.number; | ||
request(action, method, params, callback, true); | ||
this.request(action, method, params, callback, true); | ||
}; | ||
// Message | ||
plivo.send_message = function (params, callback) { | ||
plivo.prototype.send_message = function (params, callback) { | ||
var action = 'Message/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_messages = function (params, callback) { | ||
plivo.prototype.get_messages = function (params, callback) { | ||
var action = 'Message/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_message = function (params, callback) { | ||
plivo.prototype.get_message = function (params, callback) { | ||
var action = 'Message/' + params['record_id'] + '/'; | ||
@@ -729,14 +714,14 @@ delete params.record_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
// Incoming Carriers | ||
plivo.get_incoming_carriers = function (params, callback) { | ||
plivo.prototype.get_incoming_carriers = function (params, callback) { | ||
var action = 'IncomingCarrier/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_incoming_carrier = function (params, callback) { | ||
plivo.prototype.get_incoming_carrier = function (params, callback) { | ||
var action = 'IncomingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -746,13 +731,13 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_incoming_carrier = function (params, callback) { | ||
plivo.prototype.create_incoming_carrier = function (params, callback) { | ||
var action = 'IncomingCarrier/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_incoming_carrier = function (params, callback) { | ||
plivo.prototype.modify_incoming_carrier = function (params, callback) { | ||
var action = 'IncomingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -762,6 +747,6 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_incoming_carrier = function (params, callback) { | ||
plivo.prototype.delete_incoming_carrier = function (params, callback) { | ||
var action = 'IncomingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -771,3 +756,3 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -777,10 +762,10 @@ | ||
// Outgoing Carriers | ||
plivo.get_outgoing_carriers = function (params, callback) { | ||
plivo.prototype.get_outgoing_carriers = function (params, callback) { | ||
var action = 'OutgoingCarrier/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_outgoing_carrier = function (params, callback) { | ||
plivo.prototype.get_outgoing_carrier = function (params, callback) { | ||
var action = 'OutgoingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -790,13 +775,13 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_outgoing_carrier = function (params, callback) { | ||
plivo.prototype.create_outgoing_carrier = function (params, callback) { | ||
var action = 'OutgoingCarrier/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_outgoing_carrier = function (params, callback) { | ||
plivo.prototype.modify_outgoing_carrier = function (params, callback) { | ||
var action = 'OutgoingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -806,6 +791,6 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_outgoing_carrier = function (params, callback) { | ||
plivo.prototype.delete_outgoing_carrier = function (params, callback) { | ||
var action = 'OutgoingCarrier/' + params['carrier_id'] + '/'; | ||
@@ -815,3 +800,3 @@ delete params.carrier_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -821,10 +806,10 @@ | ||
// Outgoing Carrier Routings | ||
plivo.get_outgoing_carrier_routings = function (params, callback) { | ||
plivo.prototype.get_outgoing_carrier_routings = function (params, callback) { | ||
var action = 'OutgoingCarrierRouting/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.get_outgoing_carrier_routing = function (params, callback) { | ||
plivo.prototype.get_outgoing_carrier_routing = function (params, callback) { | ||
var action = 'OutgoingCarrierRouting/' + params['routing_id'] + '/'; | ||
@@ -834,13 +819,13 @@ delete params.routing_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.create_outgoing_carrier_routing = function (params, callback) { | ||
plivo.prototype.create_outgoing_carrier_routing = function (params, callback) { | ||
var action = 'OutgoingCarrierRouting/'; | ||
var method = 'POST'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.modify_outgoing_carrier_routing = function (params, callback) { | ||
plivo.prototype.modify_outgoing_carrier_routing = function (params, callback) { | ||
var action = 'OutgoingCarrierRouting/' + params['routing_id'] + '/'; | ||
@@ -850,6 +835,6 @@ delete params.routing_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
plivo.delete_outgoing_carrier_routing = function (params, callback) { | ||
plivo.prototype.delete_outgoing_carrier_routing = function (params, callback) { | ||
var action = 'OutgoingCarrierRouting/' + params['routing_id'] + '/'; | ||
@@ -859,3 +844,3 @@ delete params.routing_id; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
@@ -865,244 +850,13 @@ | ||
// Pricing | ||
plivo.get_pricing = function (params, callback) { | ||
plivo.prototype.get_pricing = function (params, callback) { | ||
var action = 'Pricing/'; | ||
var method = 'GET'; | ||
request(action, method, params, callback); | ||
this.request(action, method, params, callback); | ||
}; | ||
/** | ||
* XML Response Generation | ||
*/ | ||
// Decalaring a class Response | ||
function Response() { | ||
this.element = 'Response'; | ||
this.nestables = ['Speak', 'Play', 'GetDigits', 'Record', 'Dial', 'Message', | ||
'Redirect', 'Wait', 'Hangup', 'PreAnswer', 'Conference', 'DTMF']; | ||
this.valid_attributes = []; | ||
this.elem = doc.begin(this.element); | ||
}; | ||
Response.prototype = { | ||
init: function (name, body, attributes, parent) { | ||
this.name = name; | ||
this.body = body; | ||
this.elem = ''; | ||
if (this.element != 'Response') { | ||
this.elem.parent = parent; | ||
this.elem = parent.ele(this.name); | ||
} else { | ||
this.elem = this.elem.ele(this.name); | ||
} | ||
if (!attributes) { | ||
var attributes = {}; | ||
} | ||
var keys = Object.keys(attributes); | ||
for (var i = 0; i < keys.length; i++) { | ||
if (this.valid_attributes.indexOf(keys[i]) == -1) { | ||
throw new PlivoError('Not a valid attribute : "' + keys[i] + '" for "' + this.name + '" Element'); | ||
} | ||
this.elem.att(keys[i], attributes[keys[i]]) | ||
} | ||
if (body) { | ||
this.elem.text(body) | ||
} | ||
}, | ||
add: function (new_element, body, attributes) { | ||
if (body === undefined) { | ||
throw new PlivoError('No text set for ' + new_element.element + '.'); | ||
} | ||
if (this.nestables.indexOf(new_element.element) > -1) { | ||
var parent = this.elem; | ||
} else { | ||
throw new PlivoError(new_element.element + ' cannot be nested in ' + this.element + '.'); | ||
} | ||
new_element.init(new_element.element, body, attributes, parent); | ||
return new_element; | ||
}, | ||
addConference: function (body, attributes) { | ||
return this.add(new Conference(Response), body, attributes); | ||
}, | ||
addNumber: function (body, attributes) { | ||
return this.add(new Number(Response), body, attributes); | ||
}, | ||
addUser: function (body) { | ||
return this.add(new User(Response), body, {}); | ||
}, | ||
addDial: function (attributes) { | ||
return this.add(new Dial(Response), '', attributes); | ||
}, | ||
addGetDigits: function (attributes) { | ||
return this.add(new GetDigits(Response), '', attributes); | ||
}, | ||
addHangup: function (attributes) { | ||
return this.add(new Hangup(Response), '', attributes); | ||
}, | ||
addMessage: function (body, attributes) { | ||
return this.add(new Message(Response), body, attributes); | ||
}, | ||
addPlay: function (body, attributes) { | ||
return this.add(new Play(Response), body, attributes); | ||
}, | ||
addPreAnswer: function () { | ||
return this.add(new PreAnswer(Response), '', {}); | ||
}, | ||
addRecord: function (attributes) { | ||
return this.add(new Record(Response),'', attributes); | ||
}, | ||
addRedirect: function (body, attributes) { | ||
return this.add(new Redirect(Response), body, attributes); | ||
}, | ||
addSpeak: function (body, attributes) { | ||
return this.add(new Speak(Response), body, attributes); | ||
}, | ||
addWait: function (attributes) { | ||
return this.add(new Wait(Response), '', attributes); | ||
}, | ||
addDTMF: function (body, attributes) { | ||
return this.add(new DTMF(Response), body, attributes); | ||
}, | ||
toXML: function () { | ||
return this.elem.toString(); | ||
} | ||
} | ||
function Conference(Response) { | ||
this.element = 'Conference'; | ||
this.valid_attributes = ['muted', 'beep', 'startConferenceOnEnter', | ||
'endConferenceOnExit', 'waitSound', 'enterSound', 'exitSound', | ||
'timeLimit', 'hangupOnStar', 'maxMembers', 'record','recordWhenAlone', | ||
'recordFileFormat', 'action', 'method', 'redirect', | ||
'digitsMatch', 'callbackUrl', 'callbackMethod', 'stayAlone', | ||
'floorEvent', 'transcriptionType', 'transcriptionUrl', | ||
'transcriptionMethod', 'relayDTMF']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Conference, Response); | ||
function Number(Response) { | ||
this.element = 'Number'; | ||
this.valid_attributes = ['sendDigits', 'sendOnPreanswer', 'sendDigitsMode']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Number, Response); | ||
function User(Response) { | ||
this.element = 'User'; | ||
this.nestables = []; | ||
this.valid_attributes = ['sendDigits', 'sendOnPreanswer', 'sipHeaders']; | ||
} | ||
util.inherits(User, Response); | ||
function Dial(Response) { | ||
this.element = 'Dial'; | ||
this.valid_attributes = ['action', 'method', 'timeout', 'hangupOnStar', | ||
'timeLimit', 'callerId', 'callerName', 'confirmSound', | ||
'dialMusic', 'confirmKey', 'redirect', 'callbackUrl', | ||
'callbackMethod', 'digitsMatch', 'digitsMatchBLeg', 'sipHeaders']; | ||
this.nestables = ['Number', 'User']; | ||
} | ||
util.inherits(Dial, Response); | ||
function GetDigits(Response) { | ||
this.element = 'GetDigits'; | ||
this.valid_attributes = ['action', 'method', 'timeout', 'digitTimeout', | ||
'finishOnKey', 'numDigits', 'retries', 'invalidDigitsSound', | ||
'validDigits', 'playBeep', 'redirect', 'log']; | ||
this.nestables = ['Speak', 'Play', 'Wait']; | ||
} | ||
util.inherits(GetDigits, Response); | ||
function Hangup(Response) { | ||
this.element = 'Hangup'; | ||
this.valid_attributes = ['schedule', 'reason']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Hangup, Response); | ||
function Message(Response) { | ||
this.element = 'Message'; | ||
this.nestables = []; | ||
this.valid_attributes = ['src', 'dst', 'type', 'callbackUrl', | ||
'callbackMethod']; | ||
} | ||
util.inherits(Message, Response); | ||
function Play(Response) { | ||
this.element = 'Play'; | ||
this.valid_attributes = ['loop']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Play, Response); | ||
function PreAnswer(Response) { | ||
this.element = 'PreAnswer'; | ||
this.valid_attributes = []; | ||
this.nestables = ['Play', 'Speak', 'GetDigits', 'Wait', 'Redirect', | ||
'Message', 'DTMF']; | ||
} | ||
util.inherits(PreAnswer, Response); | ||
function Record(Response) { | ||
this.element = 'Record'; | ||
this.nestables = []; | ||
this.valid_attributes = ['action', 'method', 'timeout', 'finishOnKey', | ||
'maxLength', 'playBeep', 'recordSession', | ||
'startOnDialAnswer', 'redirect', 'fileFormat', | ||
'callbackUrl', 'callbackMethod', 'transcriptionType', | ||
'transcriptionUrl', 'transcriptionMethod']; | ||
} | ||
util.inherits(Record, Response); | ||
function Redirect(Response) { | ||
this.element = 'Redirect'; | ||
this.valid_attributes = ['method']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Redirect, Response); | ||
function Speak(Response) { | ||
this.element = 'Speak'; | ||
this.valid_attributes = ['voice', 'language', 'loop']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Speak, Response); | ||
function Wait(Response) { | ||
this.element = 'Wait'; | ||
this.valid_attributes = ['length', 'silence', 'min_silence', 'minSilence', 'beep']; | ||
this.nestables = []; | ||
} | ||
util.inherits(Wait, Response); | ||
function DTMF(Response) { | ||
this.element = 'DTMF'; | ||
this.nestables = []; | ||
this.valid_attributes = ['digits', 'async']; | ||
} | ||
util.inherits(DTMF, Response); | ||
/** | ||
@@ -1117,12 +871,13 @@ * Module Exports | ||
exports.RestAPI = function (config) { | ||
var plivoObj = new plivo(); | ||
if (!config) { | ||
throw new PlivoError('Auth ID and Auth Token must be provided.'); | ||
throw new plivoError('Auth ID and Auth Token must be provided.'); | ||
} | ||
if (typeof config != 'object') { | ||
throw new PlivoError('Config for RestAPI must be provided as an object.'); | ||
throw new plivoError('Config for RestAPI must be provided as an object.'); | ||
} | ||
if (!config.authId || !config.authToken) { | ||
throw new PlivoError('Auth ID and Auth Token must be provided.'); | ||
throw new plivoError('Auth ID and Auth Token must be provided.'); | ||
} | ||
@@ -1132,6 +887,6 @@ | ||
for (key in config) { | ||
plivo.options[key] = config[key]; | ||
plivoObj.options[key] = config[key]; | ||
} | ||
return plivo; | ||
return plivoObj; | ||
} |
{ | ||
"name": "plivo", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Plivo NodeJS helper library.", | ||
@@ -20,4 +20,4 @@ "main": "lib/plivo.js", | ||
"dependencies": { | ||
"xmlbuilder": "~0.4.2", | ||
"request": "~2.12.0" | ||
"request": "^2.71.0", | ||
"xmlbuilder": "~8.0.0" | ||
}, | ||
@@ -28,4 +28,4 @@ "directories": { | ||
"devDependencies": { | ||
"nock": "~0.14.2", | ||
"mocha": "~1.7.4" | ||
"nock": "~8.0.0", | ||
"mocha": "~2.4.5" | ||
}, | ||
@@ -32,0 +32,0 @@ "scripts": { |
@@ -27,3 +27,4 @@ var assert = require('assert'); | ||
for (key in y) { | ||
if (x[key] !== y[key]) { | ||
var yProp = y[key]; | ||
if (!yProp && x[key] !== yProp.value) { | ||
return false; | ||
@@ -73,3 +74,3 @@ } | ||
// signifies that this is not an XML element but text | ||
assert.equal('', response.elem.children[child_pos].children[0].name); | ||
assert.equal(undefined, response.elem.children[child_pos].children[0].name); | ||
@@ -90,2 +91,3 @@ // check if the text provided was added as provided or not | ||
if (!Utility.areEqual(attributes, response.elem.children[child_pos].attributes)) { | ||
@@ -92,0 +94,0 @@ assert.fail(response.elem.children[child_pos].attributes, |
@@ -54,2 +54,3 @@ var assert = require('assert'); | ||
describe('request()', function() { | ||
@@ -62,12 +63,16 @@ var rest = plivo.RestAPI({ | ||
var endpoint = nock('https://' + rest.options.host + ':443'); | ||
endpoint.get('/v1/Account/0123456789/Call/?') | ||
endpoint.get('/v1/Account/0123456789/Call/') | ||
.reply(200, JSON.stringify({})) | ||
.get('/v1/Account/0123456789/Call/?limit=5') | ||
.reply(200, JSON.stringify({})) | ||
.get('/v1/Account/0123456789/Call/xxxxxxxxxxxxxxxxx/?') | ||
.get('/v1/Account/0123456789/Call/xxxxxxxxxxxxxxxxx/') | ||
.reply(200, JSON.stringify({})) | ||
.get('/v1/Account/0123456789/Call/yyyyyyyyyyyyyyyyy/') | ||
.reply(404, JSON.stringify({})) | ||
.post('/v1/Account/0123456789/Call/', "{\"answer_url\":\"http://test.com\",\"to\":\"1234567890\",\"from\":\"1234567890\"}") | ||
.reply(201, JSON.stringify({})) | ||
.post('/v1/Account/0123456789/Call/', "{\"answer_url\":\"http://test.com\"}") | ||
.reply(401, JSON.stringify({})) | ||
.delete('/v1/Account/0123456789/Call/') | ||
.reply(204, ""); | ||
.reply(204, " "); | ||
@@ -92,2 +97,11 @@ it('should treat params as callback when params are not provided and optional is true.', function(done) { | ||
}); | ||
it('test call not found 404.', function() { | ||
rest.get_cdr({ | ||
call_uuid: 'yyyyyyyyyyyyyyyyy', | ||
}, function(status, response) { | ||
assert.equal(404, status); | ||
assert.equal('object', typeof response); | ||
done(); | ||
});; | ||
}); | ||
@@ -119,2 +133,13 @@ it('should successfully send GET requests with parameters.', function(done) { | ||
it('fail sending POST request. 401 bad request', function(done) { | ||
rest.make_call({ | ||
answer_url: 'http://test.com', | ||
}, function(status, response) { | ||
assert.equal(401, status); | ||
assert.equal('object', typeof response); | ||
done(); | ||
}); | ||
}); | ||
it('should successfully send DELETE requests.', function(done) { | ||
@@ -124,3 +149,3 @@ rest.hangup_all_calls(function(status, response) { | ||
assert.equal('string', typeof response); | ||
assert.equal('', response); | ||
assert.equal(' ', response); | ||
@@ -130,2 +155,16 @@ done(); | ||
}); | ||
it('should keep credentials unique per client. Clients should not share credentials', function () { | ||
var restEndpoint1 = plivo.RestAPI({ | ||
authId: '0123456789', | ||
authToken: '0123456789abc', | ||
}); | ||
var restEndpoint2 = plivo.RestAPI({ | ||
authId: '987654321', | ||
authToken: '0987654321abc', | ||
}); | ||
assert.notEqual(restEndpoint1.options.authId, restEndpoint2.options.authId) | ||
assert.notEqual(restEndpoint1.options.authToken, restEndpoint2.options.authToken) | ||
}); | ||
}); | ||
@@ -132,0 +171,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
53070
10
1118
0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedxmlbuilder@8.0.0(transitive)
- Removedrequest@2.12.0(transitive)
- Removedxmlbuilder@0.4.3(transitive)
Updatedrequest@^2.71.0
Updatedxmlbuilder@~8.0.0