lib/common/http-tool.js
function HttpTool(){
        this.settings = {};
        this.urlObject = {};
        this.dataToWrite = '';
        // ********** Helper Functions/Members **********
        
        var validMethods = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH'];
        
        /**
        * Make sure that settings has at least property of url and method.
        * @return {boolean} whether settings is valid.
        */ 
        this.isSettingValid = function(settings){
            if (_.isEmpty(settings)) {
                return false;
            }
            if ( ! (settings.hasOwnProperty('url') && settings.hasOwnProperty('method'))) {
                return false;
            }
            if (_.isEmpty(settings.url) || _.isEmpty(settings.method)) {
                return false;
            }
            if (_.indexOf(validMethods, settings.method) === -1) {
                return false;    
            }

            return true;
        };

        /**
        * Parse and convert setting into a urlObject that suitable for 
        * http/https module in NodeJs.
        * @return {object} - the object that suitable for Node http/https module.
        */
        this.setupUrlOptions = function(settings) {
                
            var urlTool = require('url');
            var urlObject = {};

            // Parse the string into url options
            if (typeof (settings.url) === 'string') {
                urlObject = urlTool.parse(settings.url);
            }
            else {
                urlObject = settings.url;
            }
            // set the REST options
            urlObject.method = settings.method;
        
            // set up the REST headers
            if (! _.isEmpty(settings.headers)){
                urlObject.headers = settings.headers;
            }
            else {
                urlObject.headers = {};
            }
            
            urlObject.headers['Content-Length'] = 0;
            if (settings.hasOwnProperty('data')){
                switch (typeof settings.data) {
                    case 'object':
                        this.dataToWrite = JSON.stringify(settings.data);
                        urlObject.headers['Content-Type'] = 'application/json';
                        break;
                    case 'string':
                        this.dataToWrite = settings.data;
                        break;
                    default:
                        throw new TypeError("Data field can only be object or string," + 
                                            " but got " + (typeof settings.data));
                                        }
                urlObject.headers['Content-Length'] = Buffer.byteLength(this.dataToWrite);
            }
            
            if (! _.isEmpty(settings.credential)){
                if (!_.isEmpty(settings.credential.password) &&
                    _.isEmpty(settings.credential.username)) {
                    throw new Error('Please provide username and password '+
                                    'for basic authentication.');
                }
                else {
                    urlObject.auth = settings.credential.username +':'+
                    settings.credential.password;
                }
            }
            
            // set the protolcol paramter
            if (urlObject.protocol.substr(-1) !== ':') {
                urlObject.protocol = urlObject.protocol + ':';
            }

            urlObject.rejectUnauthorized = settings.verifySSL || false;
            urlObject.recvTimeoutMs = settings.recvTimeoutMs;

            return urlObject;
        };
    }
function HttpTool(){
        this.settings = {};
        this.urlObject = {};
        this.dataToWrite = '';
        // ********** Helper Functions/Members **********
        
        var validMethods = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH'];
        
        /**
        * Make sure that settings has at least property of url and method.
        * @return {boolean} whether settings is valid.
        */ 
        this.isSettingValid = function(settings){
            if (_.isEmpty(settings)) {
                return false;
            }
            if ( ! (settings.hasOwnProperty('url') && settings.hasOwnProperty('method'))) {
                return false;
            }
            if (_.isEmpty(settings.url) || _.isEmpty(settings.method)) {
                return false;
            }
            if (_.indexOf(validMethods, settings.method) === -1) {
                return false;    
            }

            return true;
        };

        /**
        * Parse and convert setting into a urlObject that suitable for 
        * http/https module in NodeJs.
        * @return {object} - the object that suitable for Node http/https module.
        */
        this.setupUrlOptions = function(settings) {
                
            var urlTool = require('url');
            var urlObject = {};

            // Parse the string into url options
            if (typeof (settings.url) === 'string') {
                urlObject = urlTool.parse(settings.url);
            }
            else {
                urlObject = settings.url;
            }
            // set the REST options
            urlObject.method = settings.method;
        
            // set up the REST headers
            if (! _.isEmpty(settings.headers)){
                urlObject.headers = settings.headers;
            }
            else {
                urlObject.headers = {};
            }
            
            urlObject.headers['Content-Length'] = 0;
            if (settings.hasOwnProperty('data')){
                switch (typeof settings.data) {
                    case 'object':
                        this.dataToWrite = JSON.stringify(settings.data);
                        urlObject.headers['Content-Type'] = 'application/json';
                        break;
                    case 'string':
                        this.dataToWrite = settings.data;
                        break;
                    default:
                        throw new TypeError("Data field can only be object or string," + 
                                            " but got " + (typeof settings.data));
                                        }
                urlObject.headers['Content-Length'] = Buffer.byteLength(this.dataToWrite);
            }
            
            if (! _.isEmpty(settings.credential)){
                if (!_.isEmpty(settings.credential.password) &&
                    _.isEmpty(settings.credential.username)) {
                    throw new Error('Please provide username and password '+
                                    'for basic authentication.');
                }
                else {
                    urlObject.auth = settings.credential.username +':'+
                    settings.credential.password;
                }
            }
            
            // set the protolcol paramter
            if (urlObject.protocol.substr(-1) !== ':') {
                urlObject.protocol = urlObject.protocol + ':';
            }

            urlObject.rejectUnauthorized = settings.verifySSL || false;
            urlObject.recvTimeoutMs = settings.recvTimeoutMs;

            return urlObject;
        };
    }
function HttpTool ( ) { this . settings = { } ; this . urlObject = { } ; this . dataToWrite = '' ; // ********** Helper Functions/Members ********** var validMethods = [ 'GET' , 'PUT' , 'POST' , 'DELETE' , 'PATCH' ] ; /**
        * Make sure that settings has at least property of url and method.
        * @return {boolean} whether settings is valid.
        */ this . isSettingValid = function ( settings ) { if ( _ . isEmpty ( settings ) ) { return false ; } if ( ! ( settings . hasOwnProperty ( 'url' ) && settings . hasOwnProperty ( 'method' ) ) ) { return false ; } if ( _ . isEmpty ( settings . url ) || _ . isEmpty ( settings . method ) ) { return false ; } if ( _ . indexOf ( validMethods , settings . method ) === - 1 ) { return false ; } return true ; } ; /**
        * Parse and convert setting into a urlObject that suitable for 
        * http/https module in NodeJs.
        * @return {object} - the object that suitable for Node http/https module.
        */ this . setupUrlOptions = function ( settings ) { var urlTool = require ( 'url' ) ; var urlObject = { } ; // Parse the string into url options if ( typeof ( settings . url ) === 'string' ) { urlObject = urlTool . parse ( settings . url ) ; } else { urlObject = settings . url ; } // set the REST options urlObject . method = settings . method ; // set up the REST headers if ( ! _ . isEmpty ( settings . headers ) ) { urlObject . headers = settings . headers ; } else { urlObject . headers = { } ; } urlObject . headers [ 'Content-Length' ] = 0 ; if ( settings . hasOwnProperty ( 'data' ) ) { switch ( typeof settings . data ) { case 'object' : this . dataToWrite = JSON . stringify ( settings . data ) ; urlObject . headers [ 'Content-Type' ] = 'application/json' ; break ; case 'string' : this . dataToWrite = settings . data ; break ; default : throw new TypeError ( "Data field can only be object or string," + " but got " + ( typeof settings . data ) ) ; } urlObject . headers [ 'Content-Length' ] = Buffer . byteLength ( this . dataToWrite ) ; } if ( ! _ . isEmpty ( settings . credential ) ) { if ( ! _ . isEmpty ( settings . credential . password ) && _ . isEmpty ( settings . credential . username ) ) { throw new Error ( 'Please provide username and password ' + 'for basic authentication.' ) ; } else { urlObject . auth = settings . credential . username + ':' + settings . credential . password ; } } // set the protolcol paramter if ( urlObject . protocol . substr ( - 1 ) !== ':' ) { urlObject . protocol = urlObject . protocol + ':' ; } urlObject . rejectUnauthorized = settings . verifySSL || false ; urlObject . recvTimeoutMs = settings . recvTimeoutMs ; return urlObject ; } ; }
Tool class that does HTTP methods
@constructor
Tool class that does HTTP methods