var https = require('https');
var http = require('http');

class HttpClient {
  constructor(options) {
    this.port = 443 || options.port;
    this.https = options.https || https;
    this.http =  options.http || http;
    this.headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    };
    this.logger = options.logger;

    if(options.userAgent) {
      this.headers['User-Agent'] = options.userAgent;
    }
  }

  request(endpoint, method, callback) {
    if (typeof method == 'function') {
        callback = method;
        endpoint.method = endpoint.method || 'GET';
    }
    else if (typeof method !== 'undefined') {
      	endpoint.method = method;
    }

    if (endpoint.method == 'POST' || endpoint.method == 'DELETE') {
        // TODO: verify the following fix is required
        // Fix broken due ot 411 Content-Length error now sent by Nexmo API
        // PL 2016-Sept-6 - commented out Content-Length 0
        // headers['Content-Length'] = 0;
    }
    var options = {
        host: endpoint.host?endpoint.host:'rest.nexmo.com',
        port: this.port,
        path: endpoint.path,
        method: endpoint.method,
        headers: this.headers
    };

    // Allow existing headers to be overridden
    // Allow new headers to be added
    if(endpoint.headers) {
      Object.keys(endpoint.headers).forEach(function(key) {
        options.headers[key] = endpoint.headers[key];
      });
    }

    this.logger.info('Request:', options, '\nBody:', endpoint.body);
    var request;

    if (options.port == 443) {
        request = this.https.request(options);
    } else {
        request = http.request(options);
    }

    request.end(endpoint.body);

    var responseReturn = '';
    request.on('response', (response) => {
        response.setEncoding('utf8');
        response.on('data', (chunk) => {
            responseReturn += chunk;
        });
        response.on('end', () => {
            this.logger.info('response ended:', response.statusCode);
            if (callback) {
                var retJson = responseReturn;
                var err = null;

                if(response.statusCode >= 500) {
                  return callback({
                    message: 'Server Error: '+response.statusCode
                  }, null);
                }

                if (method !== 'DELETE') {

                  try {
	                    retJson = JSON.parse(responseReturn);
	                } catch (parsererr) {
	                    // ignore parser error for now and send raw response to client
	                    this.logger.error(parsererr);
	                    this.logger.error('could not convert API response to JSON, above error is ignored and raw API response is returned to client');
						          this.logger.error('Raw Error message from API ');
						          this.logger.error(responseReturn);
	                    err = parsererr;
                      return callback({
                        message: "The API response could not be parsed.",
                        parseError: parsererr
                      }, null);
	                }
                }

                if(response.statusCode < 200 || response.statusCode > 299) {
                  return callback(retJson, null);
                }

                callback(null, retJson);
            }
        })
        response.on('close', (e) => {
            this.logger.error('problem with API request detailed stacktrace below ');
            this.logger.error(e);
            callback(e);
        });
    });
    request.on('error', (e) => {
        this.logger.error('problem with API request detailed stacktrace below ');
        this.logger.error(e);
        callback(e);
    });

  }
}

export default HttpClient;

@@ -1744,22 +1744,20 @@
 response
-Return
+Data
  = '';%0A 
@@ -1897,22 +1897,20 @@
 response
-Return
+Data
  += chun
@@ -2083,30 +2083,26 @@
 r re
-tJson = responseReturn
+sponseValue = null
 ;%0A  
@@ -2122,16 +2122,23 @@
  var err
+orValue
  = null;
@@ -2139,16 +2139,40 @@
  null;%0A%0A
+                try %7B%0A  
         
@@ -2173,32 +2173,33 @@
               if
+ 
 (response.status
@@ -2235,45 +2235,24 @@
     
-return callback(%7B%0A                   
+  errorValue = %7B
  mes
@@ -2293,16 +2293,18 @@
 atusCode
+ %7D
 %0A       
@@ -2319,35 +2319,137 @@
    %7D
-, null);%0A                %7D%0A
+ else if (response.statusCode %3E= 400 %7C%7C response.statusCode %3C 200) %7B%0A                    errorValue = JSON.parse(responseReturn);
 %0A   
@@ -2457,24 +2457,33 @@
             
+  %7D else 
 if (method !
@@ -2497,17 +2497,16 @@
 ETE') %7B%0A
-%0A
         
@@ -2519,42 +2519,23 @@
     
-try %7B%0A%09                    retJson
+  responseValue
  = J
@@ -2553,33 +2553,34 @@
 esponseReturn);%0A
-%09
+  
                 
@@ -2585,29 +2585,15 @@
   %7D 
-catch (parsererr)
+else
  %7B%0A
-%09
     
@@ -2612,74 +2612,98 @@
     
-// ignore parser error for now and send raw response to client%0A%09  
+responseValue = responseReturn;%0A                  %7D%0A                %7D catch (parsererr) %7B%0A
     
@@ -2746,19 +2746,16 @@
 ererr);%0A
-%09  
         
@@ -2888,30 +2888,28 @@
 o client');%0A
-%09%09%09%09%09%09
+    
           th
@@ -2898,32 +2898,36 @@
 ;%0A              
+    
 this.logger.erro
@@ -2964,14 +2964,16 @@
 ');%0A
-%09%09%09%09%09%09
+        
     
@@ -3013,18 +3013,17 @@
 eturn);%0A
-%09 
+%0A
         
@@ -3036,70 +3036,23 @@
     
- 
 err
- = parsererr;%0A                      return callback(%7B%0A    
+orValue = %7B%0A
     
@@ -3117,20 +3117,16 @@
 rsed.%22,%0A
-    
         
@@ -3181,40 +3181,10 @@
     
-    %7D, null);%0A%09                
 %7D
+;
 %0A   
@@ -3219,176 +3219,42 @@
     
-if(response.statusCode %3C 200 %7C%7C response.statusCode %3E 299) %7B%0A                  return callback(retJson, null);%0A                %7D%0A%0A                callback(null, retJson
+callback(errorValue, responseValue
 );%0A 

Rewrite HttpClient response logic to a simple if/else statement