<commit_before>'use strict';

var _ = require('lodash');
var request = require('request');
var Q = require('q');
var url = require('url');
Client.PACKAGE_VERSION = require('../package.json').version;

var resources = require('./resources');
Client.resources = resources;

function Client(options) {
  if (!options) options = {};
  this.spread = options.spread;
  this.accessToken = options.accessToken;
  this.requestLib = options.request || request;
  this.defaultHeaders = _.extend({
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'User-Agent': 'Gitter GitHub Client/' + Client.PACKAGE_VERSION
  }, options.headers);
  this.errorHandler = options.errorHandler;

  this._prepResources();
}

Client.prototype = {
  _prepResources: function() {

    for (var name in resources) {
      this[
        name[0].toLowerCase() + name.substring(1)
      ] = new resources[name](this);
    }

  },

  _request: function(method, path, data, spec, options) {
    var self = this;

    var headers = _.extend({}, this.defaultHeaders, spec.headers, options.headers);

    if (options.accessToken || this.accessToken) {
      headers.Authorization = 'token ' + (options.accessToken || this.accessToken);
    }

    var d = Q.defer();

    var uri = url.format({
      protocol: "https:",
      host: "api.github.com",
      pathname: path,
      query: options.query
    });

    var useSpread = options.spread !== undefined  ? options.spread : this.spread;

    function spreadResponse(body, response) {
      d.resolve([body, response]);
    }

    function noSpreadResponse(body) {
      d.resolve(body);
    }

    var resolve = useSpread ? spreadResponse : noSpreadResponse;

    request({
      method: method,
      uri: uri,
      headers: headers,
      body: data,
      json: !!data,
      encoding: options.encoding === undefined ? 'utf8' : options.encoding
    }, function(err, response, body) {
      if (err) {
        err.respone = response;
        return d.reject(err);
      }

      if (spec.checkOperation) {
        /* 404 means "false" */
        if (response.statusCode === 404) {
          return resolve(false, response);
        }

        /* 2XX means "true" */
        if (response.statusCode < 300) {
          return resolve(true, response);
        }
      }

      if (options.treat404asEmpty && response.statusCode === 404) {
        return resolve(null, response);
      }

      try {
        var parsedBody = self._parseBody(response, body);<commit_msg>GZIP encoding on API calls<commit_after>'use strict';

var _ = require('lodash');
var request = require('request');
var Q = require('q');
var url = require('url');
Client.PACKAGE_VERSION = require('../package.json').version;

var resources = require('./resources');
Client.resources = resources;

function Client(options) {
  if (!options) options = {};
  this.spread = options.spread;
  this.accessToken = options.accessToken;
  this.requestLib = options.request || request;
  this.defaultHeaders = _.extend({
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'User-Agent': 'Gitter GitHub Client/' + Client.PACKAGE_VERSION
  }, options.headers);
  this.errorHandler = options.errorHandler;

  this._prepResources();
}

Client.prototype = {
  _prepResources: function() {

    for (var name in resources) {
      this[
        name[0].toLowerCase() + name.substring(1)
      ] = new resources[name](this);
    }

  },

  _request: function(method, path, data, spec, options) {
    var self = this;

    var headers = _.extend({}, this.defaultHeaders, spec.headers, options.headers);

    if (options.accessToken || this.accessToken) {
      headers.Authorization = 'token ' + (options.accessToken || this.accessToken);
    }

    var d = Q.defer();

    var uri = url.format({
      protocol: "https:",
      host: "api.github.com",
      pathname: path,
      query: options.query
    });

    var useSpread = options.spread !== undefined  ? options.spread : this.spread;

    function spreadResponse(body, response) {
      d.resolve([body, response]);
    }

    function noSpreadResponse(body) {
      d.resolve(body);
    }

    var resolve = useSpread ? spreadResponse : noSpreadResponse;

    request({
      method: method,
      uri: uri,
      headers: headers,
      body: data,
      json: !!data,
      gzip: true,
      encoding: options.encoding === undefined ? 'utf8' : options.encoding
    }, function(err, response, body) {
      if (err) {
        err.respone = response;
        return d.reject(err);
      }

      if (spec.checkOperation) {
        /* 404 means "false" */
        if (response.statusCode === 404) {
          return resolve(false, response);
        }

        /* 2XX means "true" */
        if (response.statusCode < 300) {
          return resolve(true, response);
        }
      }

      if (options.treat404asEmpty && response.statusCode === 404) {
        return resolve(null, response);
      }

      try {
        var parsedBody = self._parseBody(response, body);
{'fraction_non_alphanumeric': 0.14186645531999206, 'fraction_numerical': 0.007529225282345948, 'mean_word_length': 2.6927578639356256, 'pattern_counts': {'":': 0, '<': 5, '<?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': '292202', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1457, 'n_words': 349}