<commit_before>'use strict';

var Chalk = require('chalk');
var co = require('co');
var Prompt = require('co-prompt');
var Request = require('superagent');
var url = require('url');

module.exports = class ShopwareStoreClient {

    /**
     * @constructor
     * @param {String} username
     * @param {String} password - optional
     */
    constructor(username, password) {
        this.baseURL = 'https://api.shopware.com/';
        this.username = username;
        this.password = password;
    }

    /**
     * @param {String} path
     * @param {Object} queryParams - optional
     * @return {Promise}
     */
    GET(path, queryParams) {
        return this.getAccessToken().then(token => {
            return Request
                .get(url.resolve(this.baseURL, path))
                .query(queryParams || {})
                .set('X-Shopware-Token', token);
        }).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @param {Object} params
     * @param {String} filePath - optional
     * @return {Promise}
     */
    POST(path, params, filePath) {
        return this.getAccessToken().then(token => {
            var req = Request
                .post(url.resolve(this.baseURL, path))
                .send(params)
                .set('X-Shopware-Token', token);
            if (filePath) {
                req = req.attach('file', filePath);
            }
            return req;
        }).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @param {Object} params
     * @return {Promise}
     */
    PUT(path, params) {
        return this.getAccessToken().then(token => {
            return Request
                .put(url.resolve(this.baseURL, path))
                .send(params)
                .set('X-Shopware-Token', token);
        }).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @return {Promise}
     */
    DELETE(path) {
        return this.getAccessToken().then(token => {
            return Request
                .delete(url.resolve(this.baseURL, path))
                .set('X-Shopware-Token', token);
        }).catch(this.errorHandler());
    }

    /**
     * @return {Promise}
     */
    getAccessToken() {
        if (this.token) {
            return Promise.resolve(this.token);
        }

        // Login to get an access token
        return this.login().then(client => {
            return this.token;
        });
    }

    /**
     * @return {Promise}
     */
    login() {
        return this.getPassword().then(password => {
            // Exchange password for access token
            console.log('Logging in...');
            return Request
                .post(url.resolve(this.baseURL, 'accesstokens'))
                .send({
                    shopwareId: this.username,
                    password: password
                });
        }).then(res => {
            console.log(Chalk.green.bold('Login successful! \u{1F513}'))
            // Save userId and access token
            this.userId = res.body.userId;
            this.token = res.body.token;

            return this;
        }).catch(this.errorHandler());
    }

    /**
     * @return {Promise}
     */
    getPassword() {
        if (this.password) {
            // Use given password
            return Promise.resolve(this.password);
        }

        // Prompt for password
        return co.wrap(function *() {
            return yield Prompt.password('Password: ');
        })();
    }

    /**
     * @return {Promise}
     */
    errorHandler() {
        return (err) => {
            if (err.response && err.response.unauthorized) {
                throw new Error(`Login failed: ${err.message}`);
            }

            throw err;
        };
    }

}<commit_msg>Show spinner on CLI while waiting for requests sent to the Shopware Community Store<commit_after>'use strict';

var Chalk = require('chalk');
var co = require('co');
var Prompt = require('co-prompt');
var Request = require('superagent');
var url = require('url');
var Spinner = require('cli-spinner').Spinner;

module.exports = class ShopwareStoreClient {

    /**
     * @constructor
     * @param {String} username
     * @param {String} password - optional
     */
    constructor(username, password) {
        this.baseURL = 'https://api.shopware.com/';
        this.username = username;
        this.password = password;
        // Prepare the loading spinner
        this.spinner = new Spinner();
        this.spinner.setSpinnerString(11);
    }

    /**
     * @param {String} path
     * @param {Object} queryParams - optional
     * @return {Promise}
     */
    GET(path, queryParams) {
        return this.getAccessToken().then(token => {
            this.startCLISpinner();
            return Request
                .get(url.resolve(this.baseURL, path))
                .query(queryParams || {})
                .set('X-Shopware-Token', token);
        }).then(this.successHandler()).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @param {Object} params
     * @param {String} filePath - optional
     * @return {Promise}
     */
    POST(path, params, filePath) {
        return this.getAccessToken().then(token => {
            this.startCLISpinner();
            var req = Request
                .post(url.resolve(this.baseURL, path))
                .send(params)
                .set('X-Shopware-Token', token);
            if (filePath) {
                req = req.attach('file', filePath);
            }
            return req;
        }).then(this.successHandler()).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @param {Object} params
     * @return {Promise}
     */
    PUT(path, params) {
        return this.getAccessToken().then(token => {
            this.startCLISpinner();
            return Request
                .put(url.resolve(this.baseURL, path))
                .send(params)
                .set('X-Shopware-Token', token);
        }).then(this.successHandler()).catch(this.errorHandler());
    }

    /**
     * @param {String} path
     * @return {Promise}
     */
    DELETE(path) {
        return this.getAccessToken().then(token => {
            this.startCLISpinner();
            return Request
                .delete(url.resolve(this.baseURL, path))
                .set('X-Shopware-Token', token);
        }).then(this.successHandler()).catch(this.errorHandler());
    }

    /**
     * @return {Promise}
     */
    getAccessToken() {
        if (this.token) {
            return Promise.resolve(this.token);
        }

        // Login to get an access token
        return this.login().then(client => {
            return this.token;
        });
    }

    /**
     * @return {Promise}
     */
    login() {
        return this.getPassword().then(password => {
            // Exchange password for access token
            console.log('Logging in...');
            this.startCLISpinner();
            return Request
                .post(url.resolve(this.baseURL, 'accesstokens'))
                .send({
                    shopwareId: this.username,
                    password: password
                });
        }).then(this.successHandler()).then(res => {
            this.stopCLISpinner();
            console.log(Chalk.green.bold('Login successful! \u{1F513}'))
            // Save userId and access token
            this.userId = res.body.userId;
            this.token = res.body.token;

            return this;
        }).catch(this.errorHandler());
    }

    /**
     * @return {Promise}
     */
    getPassword() {
        if (this.password) {
            // Use given password
            return Promise.resolve(this.password);
        }

        // Prompt for password
        return co.wrap(function *() {
            return yield Prompt.password('Password: ');
        })();
    }

    /**
     * @return {Promise}
     */
    successHandler() {
        return (res) => {
            this.stopCLISpinner();
            return res;
        };
    }

    /**
     * @return {Promise}
     */
    errorHandler() {
        return (err) => {
            if (err.response && err.response.unauthorized) {
                throw new Error(`Login failed: ${err.message}`);
            }

            throw err;
        };
    }

    startCLISpinner() {
        this.spinner.start();
    }

    stopCLISpinner() {
        this.spinner.stop(true);
    }

}
{'fraction_non_alphanumeric': 0.14327830188679244, 'fraction_numerical': 0.0011792452830188679, 'mean_word_length': 1.5991418939626112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '4011933', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2339, 'n_words': 466}