<commit_before>'use strict';

const {trimEnd, forEach, camelCase} = require('lodash');

const got  = require('got');
const uuid = require('uuid');

const RunningAction = require('./RunningAction');

/**
 * Shipment HTTP server client wrapper
 */
class ShipmentClient {

    constructor({endpoint}) {
        this.endpoint  = endpoint;
        this.verifyKey = uuid.v1();
    }

    /**
     * Invoke the given action on the shipment server
     *
     * @param action
     * @param args
     *
     * @returns {RunningAction}
     */
    call(action, args = {}) {

        // Format the URL as endpoint/action
        let url = `${trimEnd(this.endpoint, '/')}/${action}`;

        // Instantiate the stream
        let stream = got.stream(url, {
            method:  'POST',
            headers: {'Content-Type': 'application/json'},
        });

        // Send the body
        const payload = this.createPayload(args);

        stream.write(JSON.stringify(payload));
        stream.end();

        return new RunningAction(this, action, args, stream);
    }

    /**
     * Create a payload for a POST request to an action using the given arguments
     * @param {Object} args
     * @returns {Object}
     */
    createPayload(args) {
        return {args, verifyKey: this.verifyKey};
    }

    /**
     * Create a client instance with "magic methods" for every available action
     *
     * @param endpoint
     * @param {String} [name] Optional name that will be verified against app name provided by the remote
     * @param {Boolean} [requireEncrypted = false]
     * @param {Boolean} [magic = true] Create "magic methods" for the available actions
     *
     * @returns {Promise.<ShipmentClient>}
     */
    static create(endpoint, {
        name,
        magic = true
    } = {}) {

        // Call GET / on the endpoint to ensure connectivity and retrieve the available endpoints
        return got(endpoint, {json: true}).then(response => {
            const body = response.body;

            // If a name was given for the client, check if the given name matches
            if (name && name !== body.app.name)
                throw new Error(`App name "${body.app.name}" does not match expected name "${name}"`);

            const client = new ShipmentClient({endpoint});

            if (magic) forEach(body.app.actions, (info, action) => {
                client[camelCase(action)] = args => client.call(action, args);
            });

            return client;
        });
    }
}

module.exports = ShipmentClient;<commit_msg>update user-agent<commit_after>'use strict';

const {trimEnd, forEach, camelCase} = require('lodash');

const got  = require('got');
const uuid = require('uuid');

const pkg = require('./../package.json');

const userAgent = `shipment-client/${pkg.version}`;

const RunningAction = require('./RunningAction');

/**
 * Shipment HTTP server client wrapper
 */
class ShipmentClient {

    constructor({endpoint}) {
        this.endpoint  = endpoint;
        this.verifyKey = uuid.v1();
    }

    /**
     * Invoke the given action on the shipment server
     *
     * @param action
     * @param args
     *
     * @returns {RunningAction}
     */
    call(action, args = {}) {

        // Format the URL as endpoint/action
        let url = `${trimEnd(this.endpoint, '/')}/${action}`;

        // Instantiate the stream
        let stream = got.stream(url, {
            method:  'POST',
            headers: {
                'user-agent':   userAgent,
                'Content-Type': 'application/json'
            },
        });

        // Send the body
        const payload = this.createPayload(args);

        stream.write(JSON.stringify(payload));
        stream.end();

        return new RunningAction(this, action, args, stream);
    }

    /**
     * Create a payload for a POST request to an action using the given arguments
     * @param {Object} args
     * @returns {Object}
     */
    createPayload(args) {
        return {args, verifyKey: this.verifyKey};
    }

    /**
     * Create a client instance with "magic methods" for every available action
     *
     * @param endpoint
     * @param {String} [name] Optional name that will be verified against app name provided by the remote
     * @param {Boolean} [requireEncrypted = false]
     * @param {Boolean} [magic = true] Create "magic methods" for the available actions
     *
     * @returns {Promise.<ShipmentClient>}
     */
    static async create(endpoint, {
        name,
        magic = true
    } = {}) {

        const response = await got(endpoint, {
            json:    true,
            headers: {
                'user-agent': userAgent,
            }
        });

        const body = response.body;

        // If a name was given for the client, check if the given name matches
        if (name && name !== body.app.name)
            throw new Error(`App name "${body.app.name}" does not match expected name "${name}"`);

        const client = new ShipmentClient({endpoint});

        if (magic) forEach(body.app.actions, (info, action) => {
            client[camelCase(action)] = args => client.call(action, args);
        });

        return client;
    }
}

module.exports = ShipmentClient;
{'fraction_non_alphanumeric': 0.12605042016806722, 'fraction_numerical': 0.0003819709702062643, 'mean_word_length': 2.1415716856628673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '5485210', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1407, 'n_words': 439}