<commit_before>var request = require('request'),
  xtend = require('xtend'),
  parseLinks = require('parse-links'),
  url = require('url');

var api = module.exports;
var Connection = require('./Connection');

api.getRule = function(accessToken, ruleName, done) {
 request.get({
  url: this.apiUrl + '/rules/' + ruleName,
  qs: { access_token: accessToken },
  headers: {"Accept": "application/xml" },
 }, function(err,r,body) {
    if( err ) return done(err);
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    return done(null, body);
 });
}

api.getRules = function(accessToken, done) {
 request.get({
  url: this.apiUrl + '/rules',
  qs: { access_token: accessToken },
  headers: {"Accept": "application/xml" },
 }, function(err,r,body) {
    if( err ) return done(err);
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    return done(null, body);
 });
}

api.updateRule = function(accessToken, rule, done) {
request.put({
    url:  this.apiUrl + '/rules/' + rule.name,
    qs:   { access_token: accessToken },
    json: rule
  }, function (err, r, body) {
    if (err) { return done(err); }
    if(r.statusCode === 400) {
      return done(new Error(body.detail || body));
    }
    done(null, body);
  });
}

api.deleteRule = function(accessToken, ruleName, done) {
request.del({
    url: this.apiUrl + '/rules/' + ruleName,
    qs: { access_token: accessToken }
  }, function (err, r, body) {
    if (err) return done(err);
    if (r.statusCode === 404) return done(null, null);
    if (r.statusCode.toString().substr(0, 1) !== '2') return done(new Error(body));
    done();
  });
}

api.createRule = function(accessToken, rule, done) {
request.post({
          url:  this.apiUrl + '/rules',
          qs:   { access_token: accessToken },
          json: rule
        },
        function (err, resp, body) {
          if (err) return done(err);
          done(null, body);
        }.bind(this));
}

api.getConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { access_token: accessToken },
    headers: {"Accept": "application/xml"},
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }

    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getSocialConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { access_token: accessToken, only_socials: true },
    headers: {"Accept": "application/xml"}
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getEnterpriseConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { 
      access_token: accessToken, 
      only_enterprise: true 
    },
    headers: {"Accept": "application/xml"}
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getSocialUsers = function (accessToken, options, done) {
  if(typeof options === 'function'){
    done = options;
    options = {};
  }
  options = xtend({social: true}, options);
  return api.getUsers.bind(this)(accessToken, options, done);
};


api.getEnterpriseUsers = function (accessToken, options, done) {
  if(typeof options === 'function'){
    done = options;
    options = {};
  }
  options = xtend({social: false}, options);
  return api.getUsers.bind(this)(accessToken, options, done);
};

api.getUsers = function(accessToken, options, done) {
  var nextPageLink, req, connection;

  if(typeof options === 'function'){<commit_msg>add getUser<commit_after>var request = require('request'),
  xtend = require('xtend'),
  parseLinks = require('parse-links'),
  url = require('url');

var api = module.exports;
var Connection = require('./Connection');

api.getRule = function(accessToken, ruleName, done) {
 request.get({
  url: this.apiUrl + '/rules/' + ruleName,
  qs: { access_token: accessToken },
  headers: {"Accept": "application/xml" },
 }, function(err,r,body) {
    if( err ) return done(err);
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    return done(null, body);
 });
}

api.getRules = function(accessToken, done) {
 request.get({
  url: this.apiUrl + '/rules',
  qs: { access_token: accessToken },
  headers: {"Accept": "application/xml" },
 }, function(err,r,body) {
    if( err ) return done(err);
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    return done(null, body);
 });
}

api.updateRule = function(accessToken, rule, done) {
request.put({
    url:  this.apiUrl + '/rules/' + rule.name,
    qs:   { access_token: accessToken },
    json: rule
  }, function (err, r, body) {
    if (err) { return done(err); }
    if(r.statusCode === 400) {
      return done(new Error(body.detail || body));
    }
    done(null, body);
  });
}

api.deleteRule = function(accessToken, ruleName, done) {
request.del({
    url: this.apiUrl + '/rules/' + ruleName,
    qs: { access_token: accessToken }
  }, function (err, r, body) {
    if (err) return done(err);
    if (r.statusCode === 404) return done(null, null);
    if (r.statusCode.toString().substr(0, 1) !== '2') return done(new Error(body));
    done();
  });
}

api.createRule = function(accessToken, rule, done) {
request.post({
          url:  this.apiUrl + '/rules',
          qs:   { access_token: accessToken },
          json: rule
        },
        function (err, resp, body) {
          if (err) return done(err);
          done(null, body);
        }.bind(this));
}

api.getConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { access_token: accessToken },
    headers: {"Accept": "application/xml"},
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }

    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getSocialConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { access_token: accessToken, only_socials: true },
    headers: {"Accept": "application/xml"}
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getEnterpriseConnections = function(accessToken, done) {
  request.get({
    url: this.apiUrl + '/connections',
    qs: { 
      access_token: accessToken, 
      only_enterprise: true 
    },
    headers: {"Accept": "application/xml"}
  }, function (err, r, body) {
    if (err) { return done(err); }
    
    if (r.statusCode.toString().substr(0, 1) !== '2'){
      return done(new Error(body));
    }
    try {
      var result = JSON.parse(body).map(function(c) {
        return new Connection(this, accessToken, c);
      });
      done(null, result);
    } catch(e) {
      done(e);
    }
  }.bind(this));
};

api.getSocialUsers = function (accessToken, options, done) {
  if(typeof options === 'function'){
    done = options;
    options = {};
  }
  options = xtend({social: true}, options);
  return api.getUsers.bind(this)(accessToken, options, done);
};


api.getEnterpriseUsers = function (accessToken, options, done) {
  if(typeof options === 'function'){
    done = options;
    options = {};
  }
  options = xtend({social: false}, options);
  return api.getUsers.bind(this)(accessToken, options, done);
};

api.getUser = function(accessToken, userId, cb) {
  var options = {
    url: this.apiUrl + "/users/" + userId,
    qs: {access_token: accessToken},
  };

  request.get(options, function(err, res, data) {
    if (err) {
      return cb(err);
    }

    if (res.statusCode.toString().substr(0, 1) !== "2") {
      return cb(new Error(data));
    }

    try {
      data = JSON.parse(data);
    } catch (e) {
      return cb(e);
    }

    return cb(null, data);
  });
};

api.getUsers = function(accessToken, options, done) {
  var nextPageLink, req, connection;

  if(typeof options === 'function'){
{'fraction_non_alphanumeric': 0.17766116941529236, 'fraction_numerical': 0.0054615549368173056, 'mean_word_length': 2.95385266723116, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '5721231', 'n_tokens_mistral': 3181, 'n_tokens_neox': 3106, 'n_words': 718}