All files / Service-Helper/lib/tcp tcp.js

7.84% Statements 4/51
0% Branches 0/18
0% Functions 0/9
7.84% Lines 4/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135    1x 1x 1x   1x                                                                                                                                                                                                                                                                
'use strict';
 
var axios = require('axios'),
    _ = require('lodash'),
    TcpSender = {};
 
module.exports = TcpSender = {
    /**
     *
     * @method restAbstract
     * Updated to use `axios` lib instead
     * @param {*} route - baseUrl
     * @param {*} body - payload Body
     * @param {*} query
     * @param {*} params
     * @param {*} headers
     * @param {*} method
     * @param {*} callback
     *
     */
    restAbstract: (route, body, query, params, headers, method, callback) => {
        var _url = route,
            _body = body,
            _query = query,
            _params = params,
            _headers = headers,
            _method = _.toLower(method);
 
        // TODO: create a new instance of the HTTP client for high throughput/concurrency puroposes
        // USE process.env to differentiate between the 2 toggles.
        axios({
            method: _method,
            url: _url,
            data: _body,
            params: _params,
            query: _query,
            headers: _headers
        })
            .then((res) => {
                if (res.status >= 200 && res.status <= 299) {
                    return callback(null, res.data);
                } else {
                    return callback(res.data, null);
                }
            })
            .catch((err) => {
                if (err.response) { // HTTP ERROR
                    console.error(JSON.stringify({status: err.response.status, message: err.response.data}));
                    return callback({status: err.response.status, message: err.response.data});
                } else { // CLIENT HANDLER ERROR
                    console.error(JSON.stringify({stack: err.stack, message: err.message}));
                    return callback({stack: err.stack, message: err.message});
                }
            });
    },
    restAbstractBasicAuth: (route, body, query, params, headers, method, credentials, callback) => {
        var _url = route,
            _body = body,
            _query = query,
            _params = params,
            _headers = headers,
            _credentials = credentials,
            _method = _.toLower(method);
 
        axios({
            method: _method,
            url: _url,
            data: _body,
            params: _params,
            query: _query,
            headers: _headers,
            auth: {
                username: _credentials.user,
                password: _credentials.password
            }
        })
            .then((res) => {
                if (res.status >= 200 && res.status <= 299) {
                    return callback(null, res.data);
                } else {
                    return callback(res.data, null);
                }
            })
            .catch((err) => {
                if (err.response) { // HTTP ERROR
                    console.error(JSON.stringify({status: err.response.status, message: err.response.data}));
                    return callback({status: err.response.status, message: err.response.data});
                } else { // CLIENT HANDLER ERROR
                    console.error(JSON.stringify({stack: err.stack, message: err.message}));
                    return callback({stack: err.stack, message: err.message});
                }
            });
    },
 
    restRequestFormDataWBasicAuth: (route, body, query, params, headers, method, credentials, callback) => {
        var _url = route,
        _body = body,
        _query = query,
        _params = params,
        _headers = headers,
        _credentials = credentials,
        _method = _.toLower(method);
 
        axios({
            method: _method,
            url: _url,
            data: _body,
            params: _params,
            query: _query,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Authorization": `Basic ${new Buffer([_credentials.user, _credentials.password].join(":")).toString("base64")}`,
                "Cache-Control": "no-cache"
            }
        })
            .then((res) => {
                if (res.status >= 200 && res.status <= 299) {
                    return callback(null, res.data);
                } else {
                    return callback(res.data, null);
                }
            })
            .catch((err) => {
                if (err.response) { // HTTP ERROR
                    console.error(JSON.stringify({status: err.response.status, message: err.response.data}));
                    return callback({status: err.response.status, message: err.response.data});
                } else { // CLIENT HANDLER ERROR
                    console.error(JSON.stringify({stack: err.stack, message: err.message}));
                    return callback({stack: err.stack, message: err.message});
                }
            });
    }
};