<commit_before>var _ = require('underscore');
var util = require('util');
var request = require('request');
var querystring = require('querystring');

// These methods require POST instead of GET
var resourcesRequiringPost = [
    'blacklists/add',
    'blacklists/remove',
    'categories/create',
    'exports/exportForum',
    'forums/addModerator',
    'forums/create',
    'forums/removeModerator',
    'posts/approve',
    'posts/create',
    'posts/highlight',
    'posts/remove',
    'posts/report',
    'posts/restore',
    'posts/spam',
    'posts/unhighlight',
    'posts/update',
    'posts/vote',
    'reactions/remove',
    'reactions/restore',
    'threads/close',
    'threads/create',
    'threads/open',
    'threads/remove',
    'threads/restore',
    'threads/subscribe',
    'threads/unsubscribe',
    'threads/update',
    'threads/vote',
    'users/checkUsername',
    'users/follow',
    'users/unfollow',
    'whitelists/add',
    'whitelists/remove'
];

function Disqus( conf ) {
    this.access_token = conf.access_token;
    this.api_key = conf.api_key;
    this.api_secret = conf.api_secret;
}

Disqus.prototype = {
    API_ROOT : 'https://disqus.com/api/3.0/%s.json',

    _getHttpVerb : function(resource) {
        return resourcesRequiringPost.indexOf(resource) !== -1 ? 'post' : 'get';
    },

    _handleResponse : function(error, response, body, callback) {
        if (response.statusCode === 200) {
            return body;
        } else {
            return {
                error : {
                    body : body,
                    error : error,
                    response : response
                }
            };
        }
    },

    getRequestUrl : function(resource, params) {
        params = params || {};

        resource = resource.toLowerCase();

        // Always extend with the authentication stuff
        _.extend(params, {
            api_secret : this.api_secret,
            api_key : this.api_key,
            access_token : this.access_token
        });

        var url = util.format(this.API_ROOT, resource);
        url += '?' + querystring.stringify( params );

        return url;
    },

    request : function(resource, params, callback ) {
        params = params || {};

        var method = this._getHttpVerb(resource);

        if (method === 'get') {
            url = this.getRequestUrl(resource, params);

            request.get(url, function() {
                callback( this._handleResponse.apply(this, arguments) );
            }.bind(this));
        }

        if (method === 'post') {
            url = this.getRequestUrl(resource);

            request.post({
                url : url,
                body : querystring.stringify( params )
            }, function() {
                callback( this._handleResponse.apply(this, arguments) );
            }.bind(this));
        }
    }
};

module.exports = Disqus;<commit_msg>version 2<commit_after>var request = require('request')
var API_ROOT =  'https://disqus.com/api/3.0/%s.json'

var resourcesRequiringPost = ['blacklists/add', 'blacklists/remove', 'categories/create', 'exports/exportForum', 'forums/addModerator', 'forums/create', 'forums/removeModerator', 'posts/approve', 'posts/create', 'posts/highlight', 'posts/remove', 'posts/report', 'posts/restore', 'posts/spam', 'posts/unhighlight', 'posts/update', 'posts/vote', 'reactions/remove', 'reactions/restore', 'threads/close', 'threads/create', 'threads/open', 'threads/remove', 'threads/restore', 'threads/subscribe', 'threads/unsubscribe', 'threads/update', 'threads/vote', 'users/checkUsername', 'users/follow', 'users/unfollow', 'whitelists/add', 'whitelists/remove']

module.exports = class Disqus {
  constructor(conf) {
    this.conf = conf
  }
  
  getHttpVerb(resource) {
    return resourcesRequiringPost.indexOf(resource) !== -1 ? 'post' : 'get';
  }
  
  request(resource, qs = {}, callback) {
    var method = this.getHttpVerb(resource)
    var url = API_ROOT.replace('%s', resource)
    Object.assign(qs, this.conf)
    
    return request[method]({ url, qs }, (e, res, body) => {
      callback(res.statusCode === 200 ? body : { error: { body, e, res } })
    })
  }
}
{'fraction_non_alphanumeric': 0.15298329355608592, 'fraction_numerical': 0.0031026252983293555, 'mean_word_length': 2.515939597315436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1611481', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1262, 'n_words': 238}