<commit_before>var errors  = require('./errors');

var request = require('request');

var url     = require('url'),
    util    = require('util');

module.exports = function (protocol, host, token, email, password) {
    var getUrl = function (object) {
        return url.format({
            protocol: protocol,
            hostname: host,
            pathname: 'v2/' + object.endpoint,
            query:    object.page ? { page: object.page } : null
        });
    };

    var getAuthUrl = function (callback) {
        request({
            url: getUrl({ endpoint: 'info' }),
            json: true
        }, function (err, resp, body) {
            if (err) {
                return callback(err);
            }

            if (resp.statusCode !== 200) {
                return callback(resp.statusCode, body);
            }

            callback(null, body.authorization_endpoint + '/oauth/token');
        });
    };

    var login = function (callback) {
        getAuthUrl(function (err, url) {
            if (err) {
                return callback(err);
            }

            request({
                url:    url,
                method: 'POST',
                headers: {
                    'content-type': 'application/x-www-form-urlencoded;' +
                                    'charset=utf-8',
                    accept:         'application/json;charset=utf-8',
                    authorization:  'Basic Y2Y6'
                },
                body: util.format('grant_type=password&username=%s&password=%s',
                          email, password)
            }, function (err, resp, body) {
                if (err) {
                    return callback(err);
                }

                try {
                    body = JSON.parse(body);
                } catch (err) {
                    return callback(err);
                }

                token = body.access_token;
                callback();
            });
        });
    };

    var isSuccess = function (code) {
        return (code - (code % 200)) === 200;
    };

    var makeRequest = function (object, callback) {
        object.url = getUrl(object);
        delete object.endpoint;
        object.json = object.json || true;
        console.log('makeRequest', object);

        if (object.json) {
            if ([ 'object', 'boolean' ].none(typeof object.json)) {
                object.body = object.json;
                object.json = undefined;
            }
        }

        return request(object, function (err, resp, body) {
            if (err) {
                return callback(err);
            }

            if (isSuccess(resp.statusCode)) {
                return callback(null, body);
            }

            return callback(errors.get(resp));
        });
    };

    return function (object, callback) {
        if (token) {
            object.headers = {
                Authorization: 'bearer ' + token
            };<commit_msg>Pass query to request rather than from formed url<commit_after>var errors  = require('./errors');

var request = require('request');

var url     = require('url'),
    util    = require('util');

module.exports = function (protocol, host, token, email, password) {
    var getUrl = function (object) {
        return url.format({
            protocol: protocol,
            hostname: host,
            pathname: 'v2/' + object.endpoint
        });
    };

    var getAuthUrl = function (callback) {
        request({
            url: getUrl({ endpoint: 'info' }),
            json: true
        }, function (err, resp, body) {
            if (err) {
                return callback(err);
            }

            if (resp.statusCode !== 200) {
                return callback(resp.statusCode, body);
            }

            callback(null, body.authorization_endpoint + '/oauth/token');
        });
    };

    var login = function (callback) {
        getAuthUrl(function (err, url) {
            if (err) {
                return callback(err);
            }

            request({
                url:    url,
                method: 'POST',
                headers: {
                    'content-type': 'application/x-www-form-urlencoded;' +
                                    'charset=utf-8',
                    accept:         'application/json;charset=utf-8',
                    authorization:  'Basic Y2Y6'
                },
                body: util.format('grant_type=password&username=%s&password=%s',
                          email, password)
            }, function (err, resp, body) {
                if (err) {
                    return callback(err);
                }

                try {
                    body = JSON.parse(body);
                } catch (err) {
                    return callback(err);
                }

                token = body.access_token;
                callback();
            });
        });
    };

    var isSuccess = function (code) {
        return (code - (code % 200)) === 200;
    };

    var makeRequest = function (object, callback) {
        object.url = getUrl(object);
        delete object.endpoint;
        object.json = object.json || true;
        console.log('makeRequest', object);

        if (object.json) {
            if ([ 'object', 'boolean' ].none(typeof object.json)) {
                object.body = object.json;
                object.json = undefined;
            }
        }

        object.query = object.page ? { page: object.page } : null;

        return request(object, function (err, resp, body) {
            if (err) {
                return callback(err);
            }

            if (isSuccess(resp.statusCode)) {
                return callback(null, body);
            }

            return callback(errors.get(resp));
        });
    };

    return function (object, callback) {
        if (token) {
            object.headers = {
                Authorization: 'bearer ' + token
            };
{'fraction_non_alphanumeric': 0.12081099195710455, 'fraction_numerical': 0.004691689008042895, 'mean_word_length': 1.2618416066691929, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2587709', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1479, 'n_words': 352}