<commit_before>var request = require('request');
var methods = require('./api-methods');

function serializeObjToUri(obj) {
  return Object.keys(obj).map(function(key) {
    return key + '=' + encodeURIComponent(obj[key]);
  }).join('&');
}

function buildBasicAuthHeader(obj) {
  return 'Basic ' + new Buffer(obj.username + ':' + obj.password).toString('base64');
}

var Api = function(config) {
  this._protocol = config.protocol || 'http'; // or https
  this._auth = (config.basicAuth) ? buildBasicAuthHeader(config.basicAuth) : '';
  this._host = config.host || 'localhost';
  this._port = config.port || '12900';
  this._path = config.path || '';
  this._uri = this._protocol + '://' + this._host + ':' + this._port + this._path;
};

Object.keys(methods).forEach(function(mName) {

  var m = methods[mName];

  Api.prototype[mName] = function(parameters, path, callback) {

    if (arguments.length === 1) callback = parameters;
    if (arguments.length === 2) callback = path;

    var computedPath = m.path;
    if (typeof arguments[1] === 'object') {
      computedPath = m.path.replace(/{([^}]*)}/g, function(s, p) {
        return path[p];
      });
    }

    var reqUri = this._uri + computedPath;

    if (m.method === 'GET' && parameters) {
      reqUri = reqUri + '?' + serializeObjToUri(parameters);
    }

    var opts = {
      url: reqUri,
      method: m.method,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: this._auth
      },
      body: (m.method !== 'GET' && parameters) ? parameters : null,
      json: false
    };

    request(opts, function(error, response, body) {
      if (error) {
        return callback([error, body]);
      }

      try {
        /*
        removeStream() will respond an empty body;
        we need a fallback to prevent JSON.parse(body) to fail
         */
        if(body === '')
            body = '{}';
        callback(null, JSON.parse(body));
      }<commit_msg>Simplify graylog uri construction<commit_after>var request = require('request');
var methods = require('./api-methods');

function serializeObjToUri(obj) {
  return Object.keys(obj).map(function(key) {
    return key + '=' + encodeURIComponent(obj[key]);
  }).join('&');
}


var Api = function(config) {
  this._uri = config.uri
};

Object.keys(methods).forEach(function(mName) {

  var m = methods[mName];

  Api.prototype[mName] = function(parameters, path, callback) {

    if (arguments.length === 1) callback = parameters;
    if (arguments.length === 2) callback = path;

    var computedPath = m.path;
    if (typeof arguments[1] === 'object') {
      computedPath = m.path.replace(/{([^}]*)}/g, function(s, p) {
        return path[p];
      });
    }

    var reqUri = this._uri + computedPath;

    if (m.method === 'GET' && parameters) {
      reqUri = reqUri + '?' + serializeObjToUri(parameters);
    }

    var opts = {
      url: reqUri,
      method: m.method,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',

      },
      body: (m.method !== 'GET' && parameters) ? parameters : null,
      json: false
    };
    console.log(opts)
    request(opts, function(error, response, body) {
      if (error) {
        return callback([error, body]);
      }
      try {
        /*
        removeStream() will respond an empty body;
        we need a fallback to prevent JSON.parse(body) to fail
         */
        if(body === '')
            body = '{}';
        callback(null, JSON.parse(body));
      }
{'fraction_non_alphanumeric': 0.16596461668070767, 'fraction_numerical': 0.0036506599269868017, 'mean_word_length': 2.638406537282942, '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': '269682', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1093, 'n_words': 250}