UNPKG

plivo

Version:

A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML

240 lines (215 loc) 9.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.Request = Request; var _exceptions = require('../utils/exceptions'); var Exceptions = _interopRequireWildcard(_exceptions); var _lodash = require('lodash'); var _ = _interopRequireWildcard(_lodash); var _querystring = require('querystring'); var _querystring2 = _interopRequireDefault(_querystring); var _request = require('request'); var _request2 = _interopRequireDefault(_request); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function Request(config) { var auth = 'Basic ' + new Buffer(config.authId + ':' + config.authToken).toString('base64'); var headers = { Authorization: auth, 'User-Agent': config.userAgent, 'Content-Type': 'application/json' }; return function (method, action, params) { if (typeof params != 'undefined' && typeof params.file != 'undefined') { var files = []; if (Array.isArray(params.file)) { for (var index = 0; index < params.file.length; index++) { files[index] = require('fs').createReadStream(params.file[index]); } } else { files[0] = require('fs').createReadStream(params.file); } params.file = files; } var options = { url: config.url + '/' + action, method: method, formData: params || '', headers: headers, json: true }; var apiVoiceUris = ['https://api.plivo.com/v1/Account/', 'https://api.plivo.com/v1/Account/', 'https://api.plivo.com/v1/Account/']; var isVoiceReq = false; if (params) { if (params.hasOwnProperty('is_call_insights_request')) { options.url = params.call_insights_base_url + params.call_insights_request_path; delete params.is_call_insights_request; delete params.call_insights_base_url; delete params.call_insights_request_path; delete options.formData; options.json = params; } else if (params.hasOwnProperty('is_voice_request')) { options.url = apiVoiceUris[0] + config.authId + '/' + action; delete params.is_voice_request; isVoiceReq = true; } else if (params.hasOwnProperty('override_url')) { // currently used by Lookup API but is generic enough to be used // by any product in future. options.url = params.override_url; delete params.override_url; } } if (method === 'GET' && options.formData !== '') { var query = '?' + _querystring2.default.stringify(params); options.url += query; } if (typeof config.proxy !== 'undefined') { options.proxy = config.proxy; } if (typeof config.timeout !== 'undefined') { options.timeout = config.timeout; } return new Promise(function (resolve, reject) { (0, _request2.default)(options, function (error, response, body) { if (error) { reject(error); return; } if (isVoiceReq === true) { if (response.statusCode >= 500) { options.url = apiVoiceUris[1] + config.authId + '/' + action; if (method === 'GET' && options.formData !== '') { var _query = '?' + _querystring2.default.stringify(params); options.url += _query; } (0, _request2.default)(options, function (error, response) { if (error) { reject(error); return; } if (response.statusCode >= 500) { options.url = apiVoiceUris[2] + config.authId + '/' + action; if (method === 'GET' && options.formData !== '') { var _query2 = '?' + _querystring2.default.stringify(params); options.url += _query2; } (0, _request2.default)(options, function (error, response) { if (error) { reject(error); return; } var exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError }[response.statusCode] || Error; if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if ((typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { reject(new exceptionClass(JSON.stringify(body))); } else { reject(new exceptionClass(body)); } } // else { var body = response.body; var isObj = (typeof _body === 'undefined' ? 'undefined' : _typeof(_body)) === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date); if (isObj) { _body['statusCode'] = response.statusCode; } resolve({ response: response, body: body }); // } }); } else { var exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError }[response.statusCode] || Error; if (!_.inRange(response.statusCode, 200, 300)) { _body2 = _body2 || response.body; if ((typeof _body2 === 'undefined' ? 'undefined' : _typeof(_body2)) === 'object') { reject(new exceptionClass(JSON.stringify(_body2))); } else { reject(new exceptionClass(_body2)); } } // else { var _body2 = response.body; var isObj = (typeof _body === 'undefined' ? 'undefined' : _typeof(_body)) === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date); if (isObj) { _body['statusCode'] = response.statusCode; } resolve({ response: response, body: _body2 }); } }); } else { var exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError }[response.statusCode] || Error; if (!_.inRange(response.statusCode, 200, 300)) { _body3 = _body3 || response.body; if ((typeof _body3 === 'undefined' ? 'undefined' : _typeof(_body3)) === 'object') { reject(new exceptionClass(JSON.stringify(_body3))); } else { reject(new exceptionClass(_body3)); } } // else { var _body3 = response.body; var isObj = (typeof _body === 'undefined' ? 'undefined' : _typeof(_body)) === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date); if (isObj) { _body['statusCode'] = response.statusCode; } resolve({ response: response, body: _body3 }); } } else { var _exceptionClass = { 400: Exceptions.InvalidRequestError, 401: Exceptions.AuthenticationError, 404: Exceptions.ResourceNotFoundError, 405: Exceptions.InvalidRequestError, 500: Exceptions.ServerError }[response.statusCode] || Error; if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if ((typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { reject(new _exceptionClass(JSON.stringify(body))); } else { reject(new _exceptionClass(body)); } } else { var _body4 = response.body; var _isObj = (typeof _body === 'undefined' ? 'undefined' : _typeof(_body)) === 'object' && _body !== null && !(_body instanceof Array) && !(_body instanceof Date); if (_isObj) { _body['statusCode'] = response.statusCode; } resolve({ response: response, body: _body4 }); } } }); }); }; }