<commit_before>validator = require('validator').validators;
request = require('request');
request.defaults({ 
  timeout: 5000
});

// For self-signed certificates, set to "1" for production
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

module.exports = {
  Gateway: (function(){
    function Client(opts){
      this.user = this.validate(opts, 'user', 'notNull');
      this.secret = this.validate(opts, 'secret', 'notNull');

      this.api = opts.api || 'https://localhost:4000';
      this.request = request;
    }

    Client.prototype.validate = function(opts, value, predicate) {
      function throwError() {
        throw new Error('params: '+value+' fails '+predicate+' test.')
      }

      validator['isBoolean'] = function(val) {
        return (val == true)   || (val == false) ||
               (val == 'true') || (val == 'false')
      }

      if (!!value) {  
        if (predicate == 'present') { return true }
      } else {
        throwError() 
      }
      try {
        if (validator[predicate](opts[value])) {
          return value
        } else { 
          throwError()
        }
      } catch(e) {
        throwError()
      }
    }

    Client.prototype.auth = function() {
      return auth = { 
        'user': this.user, 
        'pass': this.<PASSWORD>, 
        'sendImmediately': true 
      };
    };

    Client.prototype.createUser = function(opts, fn){
      var url = this.api + '/api/v1/users';
      request.post({ url: url, form: opts , json:true, auth: this.auth(), rejectUnauthorized: false }, function(err,resp,body){
        if (!body.user) {
          err = body;
        }
        fn(err, body.user);
      })
    }

    Client.prototype.getUser = function(opts, fn){
      var url = this.api + '/api/v1/users';
      request.get({ url: url, form: opts , json: true, auth: this.auth(), rejectUnauthorized: false }, function(err,resp,body){
        fn(err, body.user);
      })
    }

    Client.prototype.getExternalAccounts = function(opts, fn){
      var url = this.api + '/api/v1/external_accounts';
      request.get({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body.externalAccounts);
      })
    }

    Client.prototype.createExternalAccount = function(opts, fn){
      var url = this.api + '/api/v1/external_accounts';
      request.post({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body.external_account);
      })
    }

    Client.prototype.createDeposit = function(opts, fn) {
      var url = this.api + '/api/v1/deposits';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.createWithdrawal = function(opts, fn) {
      var url = this.api + '/api/v1/withdrawals';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.getPendingWithdrawals = function(opts, fn) {
      var url = this.api + '/api/v1/withdrawals/pending';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.clearWithdrawal = function(opts, fn) {
      this.validate(opts, 'withdrawal_id', 'isNumeric');
      var url = this.api + '/api/v1/withdrawals/' + opts.withdrawal_id + '/clear';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.getExternalTransactions = function(opts, fn){
      var url = this.api + '/api/v1/external_transactions';
      request.get({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body);
      })
    }

    Client.prototype.getSettings = function(fn) {
      var url = this.api + '/api/v1/settings';
      request.get({ url: url, json: true, auth: this.auth() }, function(err, resp, body) {
          fn(err, body);
      });
    }
   
    Client.prototype.getBalances = function(opts, fn) {
      var url = this.api + '/api/v1/balances';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.getRippleAddresses = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_addresses';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.createRippleAddress = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_addresses';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.createRippleTransaction = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }
  
    Client.prototype.getRippleTransactions = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }

    Client.prototype.getPendingRippleTransactions = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions/pending';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body);
      });
    }
  
    return {
      Client: Client
    }
  })()
}
<commit_msg>[FEATURE] Return resource object in all http client calls.<commit_after>validator = require('validator').validators;
request = require('request');
request.defaults({ 
  timeout: 5000
});

// For self-signed certificates, set to "1" for production
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

module.exports = {
  Gateway: (function(){
    function Client(opts){
      this.user = this.validate(opts, 'user', 'notNull');
      this.secret = this.validate(opts, 'secret', 'notNull');

      this.api = opts.api || 'https://localhost:4000';
      this.request = request;
    }

    Client.prototype.validate = function(opts, value, predicate) {
      function throwError() {
        throw new Error('params: '+value+' fails '+predicate+' test.')
      }

      validator['isBoolean'] = function(val) {
        return (val == true)   || (val == false) ||
               (val == 'true') || (val == 'false')
      }

      if (!!value) {  
        if (predicate == 'present') { return true }
      } else {
        throwError() 
      }
      try {
        if (validator[predicate](opts[value])) {
          return value
        } else { 
          throwError()
        }
      } catch(e) {
        throwError()
      }
    }

    Client.prototype.auth = function() {
      return auth = { 
        'user': this.user, 
        'pass': <PASSWORD>, 
        'sendImmediately': true 
      };
    };

    Client.prototype.createUser = function(opts, fn){
      var url = this.api + '/api/v1/users';
      request.post({ url: url, form: opts , json:true, auth: this.auth(), rejectUnauthorized: false }, function(err,resp,body){
        if (!body.user) {
          err = body;
        }
        fn(err, body.user);
      })
    }

    Client.prototype.getUser = function(opts, fn){
      var url = this.api + '/api/v1/users';
      request.get({ url: url, form: opts , json: true, auth: this.auth(), rejectUnauthorized: false }, function(err,resp,body){
        fn(err, body.user);
      })
    }

    Client.prototype.getExternalAccounts = function(opts, fn){
      var url = this.api + '/api/v1/external_accounts';
      request.get({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body.external_accounts);
      })
    }

    Client.prototype.createExternalAccount = function(opts, fn){
      var url = this.api + '/api/v1/external_accounts';
      request.post({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body.external_account);
      })
    }

    Client.prototype.createDeposit = function(opts, fn) {
      var url = this.api + '/api/v1/deposits';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.deposit);
      });
    }

    Client.prototype.createWithdrawal = function(opts, fn) {
      var url = this.api + '/api/v1/withdrawals';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.withdrawal);
      });
    }

    Client.prototype.getPendingWithdrawals = function(opts, fn) {
      var url = this.api + '/api/v1/withdrawals/pending';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.withdrawals);
      });
    }

    Client.prototype.clearWithdrawal = function(opts, fn) {
      this.validate(opts, 'withdrawal_id', 'isNumeric');
      var url = this.api + '/api/v1/withdrawals/' + opts.withdrawal_id + '/clear';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.withdrawal);
      });
    }

    Client.prototype.getExternalTransactions = function(opts, fn){
      var url = this.api + '/api/v1/external_transactions';
      request.get({ url: url, form: opts , json: true, auth: this.auth() }, function(err,resp,body){
        fn(err, body.external_transactions);
      })
    }

    Client.prototype.getSettings = function(fn) {
      var url = this.api + '/api/v1/settings';
      request.get({ url: url, json: true, auth: this.auth() }, function(err, resp, body) {
          fn(err, body.settings);
      });
    }
   
    Client.prototype.getBalances = function(opts, fn) {
      var url = this.api + '/api/v1/balances';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.balances);
      });
    }

    Client.prototype.getRippleAddresses = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_addresses';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.ripple_addresses);
      });
    }

    Client.prototype.createRippleAddress = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_addresses';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.ripple_address);
      });
    }

    Client.prototype.sendPayment = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions';
      request.post({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.payment);
      });
    }
  
    Client.prototype.getPayments = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.payments);
      });
    }

    Client.prototype.getPendingPayments = function(opts, fn) {
      var url = this.api + '/api/v1/ripple_transactions/pending';
      request.get({ url: url, form: opts, json: true, auth: this.auth() }, function(err, resp, body) {
        fn(err, body.payments);
      });
    }
  
    return {
      Client: Client
    }
  })()
}

{'fraction_non_alphanumeric': 0.1646252676659529, 'fraction_numerical': 0.004453961456102783, 'mean_word_length': 2.511578947368421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4779596', 'n_tokens_mistral': 3831, 'n_tokens_neox': 3748, 'n_words': 858}