<commit_before>'use strict';

const qs = require('querystring');
const request = require('request');
const KeyPair = require('storj-lib').KeyPair;

/**
 * Represents a client interface to a given billing server
 * @constructor
 * @param {String} uri - API base URI ('https://billing.storj.io')
 * @param {String} privkey - KeyPair instance for request signing
 */
class BillingClient {
  constructor(baseURI, privkey) {
    this._options = {
      baseURI: baseURI,
      keypair: KeyPair(privkey)
    };
  }

  _request(method, path, params, callback) {
    const opts = {
      baseUrl: this._options.baseURI,
      uri: path,
      method: method
    };

    params.__nonce = Date.now();

    if (['GET', 'DELETE'].indexOf(method) !== -1) {
      opts.qs = params;
      opts.json = true;
    } else {
      opts.json = params;
    }

    this._authenticate(opts);

    const requestPromise = new Promise(function(resolve, reject) {
      request(opts, function(err, res, body) {
        if (err) return reject(err);
        if (res.statusCode > 400) return reject(new Error(body.error || body));
        resolve(body);
      });
    });

    if (callback) requestPromise.then(callback, callback);

    return requestPromise;
  }

  _authenticate(opts) {
    if (this._options.keypair) {
      var payload = ['GET', 'DELETE'].indexOf(opts.method) !== -1 ?
          qs.stringify(opts.qs) :
          JSON.stringify(opts.json);

      var contract = [opts.method, opts.uri, payload].join('\n');

      opts.headers = opts.headers || {};
      opts.headers['x-pubkey'] = this._options.keypair.getPublicKey();
      opts.headers['x-signature'] = this._options.keypair.sign(contract, {
        compact: false
      });
    }

    return opts;
  }

  createDebit(debit) {

    return this._request('POST', '/debits', debit);
  }
}

module.exports = BillingClient;
<commit_msg>Formatting<commit_after>'use strict';

const qs = require('querystring');
const request = require('request');
const KeyPair = require('storj-lib').KeyPair;

/**
 * Represents a client interface to a given billing server
 * @constructor
 * @param {String} uri - API base URI ('https://billing.storj.io')
 * @param {String} privkey - KeyPair instance for request signing
 */
class BillingClient {
  constructor(baseURI, privkey) {
    this._options = {
      baseURI: baseURI,
      keypair: KeyPair(privkey)
    };
  }

  _request(method, path, params, callback) {
    const opts = {
      baseUrl: this._options.baseURI,
      uri: path,
      method: method
    };

    params.__nonce = Date.now();

    if (['GET', 'DELETE'].indexOf(method) !== -1) {
      opts.qs = params;
      opts.json = true;
    } else {
      opts.json = params;
    }

    this._authenticate(opts);

    const requestPromise = new Promise(function(resolve, reject) {
      request(opts, function(err, res, body) {
        if (err) return reject(err);
        if (res.statusCode > 400) return reject(new Error(body.error || body));
        resolve(body);
      });
    });

    if (callback) requestPromise.then(callback, callback);

    return requestPromise;
  }

  _authenticate(opts) {
    if (this._options.keypair) {
      var payload = ['GET', 'DELETE'].indexOf(opts.method) !== -1
        ? qs.stringify(opts.qs)
        : JSON.stringify(opts.json);

      var contract = [opts.method, opts.uri, payload].join('\n');

      opts.headers = opts.headers || {};
      opts.headers['x-pubkey'] = this._options.keypair.getPublicKey();
      opts.headers['x-signature'] = this._options.keypair.sign(contract, {
        compact: false
      });
    }

    return opts;
  }

  createDebit(debit) {
    return this._request('POST', '/debits', debit);
  }
}

module.exports = BillingClient;

{'fraction_non_alphanumeric': 0.15080213903743314, 'fraction_numerical': 0.00267379679144385, 'mean_word_length': 2.809572301425662, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7029455', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1152, 'n_words': 260}