<commit_before>'use strict';

var request = require('request');
var q = require('q');
var constants = require('./gn-constants');
var GnAuth = require('./gn-auth');

function GnEndpoints(options) {
  options.baseUrl = options.sandbox ? constants.URL.sandbox : constants.URL.production;
  this.options = options;
  this.gnAuth = new GnAuth(options);
  this.accessToken = null;
}

var postParams = function (input, route) {
  var params = {
    url: this.options.baseUrl + route,
    json: true,
    body: input
  };

  if (this.accessToken) {
    params.form.access_token = this.accessToken;
  }

  return params;
};

GnEndpoints.prototype.post = function (object, route) {
  var self = this;
  this.defer = q.defer();

  var getResponse = function (httpResponse, body) {
    return self.options.rawResponse ? httpResponse : body;
  }

  var directReqCallback = function (err, httpResponse, body) {
    var response = getResponse(httpResponse, body);

    if (err) {
      self.defer.reject(err);
    } else if (httpResponse.statusCode !== 200) {
      self.defer.reject(response);
    } else {
      self.defer.resolve(response);
    }
  };

  var withTokenReqCallback = function (err, httpResponse, body) {
    var response = getResponse(httpResponse, body);

    if (err) {
      self.defer.reject(err);
    } else if (httpResponse.statusCode === 401) {
      self.getAccessToken().then(directReq);
    } else if (httpResponse.statusCode !== 200) {
      self.defer.reject(response);
    } else {
      self.defer.resolve(response);
    }
  };

  var directReq = function () {
    request.post(postParams.call(self, object,
      constants.ENDPOINTS[route]), directReqCallback);
  };

  var withTokenReq = function () {
    request.post(postParams.call(self, object,
      constants.ENDPOINTS[route]), withTokenReqCallback);
  };

  if (!this.accessToken) {
    this.getAccessToken().then(directReq);
  } else {
    withTokenReq();
  }

  return this.defer.promise;
};

GnEndpoints.prototype.getAccessToken = function () {
  var self = this;
  return this.gnAuth.getAccessToken().then(function (response) {
    self.accessToken = response.access_token;
    return self.accessToken;
  }).catch(function (err) {
    return err;
  });
};

GnEndpoints.prototype.run = function (endpoint, options) {
  return this.post(options, endpoint);
};

module.exports = GnEndpoints;<commit_msg>Send bearer token in req headers<commit_after>'use strict';

var request = require('request');
var q = require('q');
var constants = require('./gn-constants');
var GnAuth = require('./gn-auth');

function GnEndpoints(options) {
  options.baseUrl = options.sandbox ? constants.URL.sandbox : constants.URL.production;
  this.options = options;
  this.gnAuth = new GnAuth(options);
  this.accessToken = null;
}

var postParams = function (input, route) {
  var params = {
    url: this.options.baseUrl + route,
    json: true,
    body: input
  };

  if (this.accessToken) {
    params.auth = {
      bearer: this.accessToken
    }
  }

  return params;
};

GnEndpoints.prototype.post = function (object, route) {
  var self = this;
  this.defer = q.defer();

  var getResponse = function (httpResponse, body) {
    return self.options.rawResponse ? httpResponse : body;
  }

  var directReqCallback = function (err, httpResponse, body) {
    var response = getResponse(httpResponse, body);

    if (err) {
      self.defer.reject(err);
    } else if (httpResponse.statusCode !== 200) {
      self.defer.reject(response);
    } else {
      self.defer.resolve(response);
    }
  };

  var withTokenReqCallback = function (err, httpResponse, body) {
    var response = getResponse(httpResponse, body);

    if (err) {
      self.defer.reject(err);
    } else if (httpResponse.statusCode === 401) {
      self.getAccessToken().then(directReq);
    } else if (httpResponse.statusCode !== 200) {
      self.defer.reject(response);
    } else {
      self.defer.resolve(response);
    }
  };

  var directReq = function () {
    request.post(postParams.call(self, object,
      constants.ENDPOINTS[route]), directReqCallback);
  };

  var withTokenReq = function () {
    request.post(postParams.call(self, object,
      constants.ENDPOINTS[route]), withTokenReqCallback);
  };

  if (!this.accessToken) {
    this.getAccessToken().then(directReq);
  } else {
    withTokenReq();
  }

  return this.defer.promise;
};

GnEndpoints.prototype.getAccessToken = function () {
  var self = this;
  return this.gnAuth.getAccessToken().then(function (response) {
    self.accessToken = response.access_token;
    return self.accessToken;
  }).catch(function (err) {
    return err;
  });
};

GnEndpoints.prototype.run = function (endpoint, options) {
  return this.post(options, endpoint);
};

module.exports = GnEndpoints;
{'fraction_non_alphanumeric': 0.13808230624608314, 'fraction_numerical': 0.003760183831209526, 'mean_word_length': 3.449814126394052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1505331', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1491, 'n_words': 317}