Skip to content

Commit

Permalink
Simplify the CLI request client (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino committed Jun 27, 2019
1 parent 81baae1 commit 8d25416
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -12,6 +12,7 @@
"columnify": "^1.5.4",
"fs-extra": "^7.0.1",
"inquirer": "^6.3.1",
"request": "^2.88.0",
"keytar": "4.9.0",
"shelljs": "^0.8.3",
"tsv": "^0.2.0",
Expand Down
47 changes: 8 additions & 39 deletions src/services/cli-http-client.js
@@ -1,19 +1,16 @@
const os = require('os');
const _ = require('lodash');
const Q = require('q');
const Response = require('twilio/lib/http/response');
const Request = require('twilio/lib/http/request');
const pkg = require('../../package.json');

class CliRequestClient {
constructor(commandName, logger, http) {
this.commandName = commandName;
this.logger = logger;
this.http = http || require('request');
this.http = require('util').promisify(http || require('request'));
}

/**
* Make http request
* Make an HTTP request.
*
* @param {object} opts - The options argument
* @param {string} opts.method - The http method
* @param {string} opts.uri - The request uri
Expand All @@ -26,7 +23,7 @@ class CliRequestClient {
* @param {boolean} [opts.allowRedirects] - Should the client follow redirects
* @param {boolean} [opts.forever] - Set to true to use the forever-agent
*/
request(opts) {
async request(opts) {
opts = opts || {};
if (!opts.method) {
throw new Error('http method is required');
Expand All @@ -36,16 +33,14 @@ class CliRequestClient {
throw new Error('uri is required');
}

const deferred = Q.defer();
const headers = opts.headers || {};

if (!headers.Connection && !headers.connection) {
headers.Connection = 'close';
}

let b64Auth = null;
if (opts.username && opts.password) {
b64Auth = Buffer.from(opts.username + ':' + opts.password).toString('base64');
const b64Auth = Buffer.from(opts.username + ':' + opts.password).toString('base64');
headers.Authorization = 'Basic ' + b64Auth;
}

Expand All @@ -69,15 +64,15 @@ class CliRequestClient {
this.logger.debug('-- BEGIN Twilio API Request --');
this.logger.debug(options.method + ' ' + options.url);

if (!_.isNull(opts.data)) {
if (opts.data) {
options.formData = opts.data;
if (options.formData) {
this.logger.debug('Form data:');
this.logger.debug(options.formData);
}
}

if (!_.isNull(opts.params)) {
if (opts.params) {
options.qs = opts.params;
options.useQuerystring = true;
if (options.qs && Object.keys(options.qs).length > 0) {
Expand All @@ -88,33 +83,7 @@ class CliRequestClient {
this.logger.debug('User-Agent: ' + options.headers['User-Agent']);
this.logger.debug('-- END Twilio API Request --');

const optionsRequest = {
method: options.method,
url: options.url,
auth: b64Auth,
params: options.qs,
data: options.formData,
headers: options.headers
};

const that = this;
this.lastResponse = undefined;
this.lastRequest = new Request(optionsRequest);

this.http(options, (error, response) => {
if (error) {
that.lastResponse = undefined;
deferred.reject(error);
} else {
that.lastResponse = new Response(response.statusCode, response.body);
deferred.resolve({
statusCode: response.statusCode,
body: response.body
});
}
});

return deferred.promise;
return this.http(options);
}
}

Expand Down

0 comments on commit 8d25416

Please sign in to comment.