<commit_before>/*!
 * node-gcm
 * Copyright(c) 2013 <NAME> <<EMAIL>>
 * MIT Licensed
 */
var Constants = require('./constants');
var req = require('request');
var debug = require('debug')('spire-fcm');

function SenderBase(key, options) {
    if (!(this instanceof SenderBase)) {
        return new SenderBase(key, options);
    }

    this.key = key;
    this.options = options;

    // added to support child dependency injection
    this.req = this.req ? this.req : require('request');
}

SenderBase.prototype.sendBaseNoRetry = function(message, recipient, callback) {
    if(!callback) {
        callback = function() {};
    }

    var body = {},
        requestBody,
        post_options,
        post_req,
        timeout;

    if (recipient && recipient.registrationIds) {
        // Registration IDs take precedence
        body[Constants.JSON_REGISTRATION_IDS] = recipient.registrationIds;
    } else if (recipient && recipient.notificationKey && recipient.notificationKey.length) {
        body[Constants.PARAM_TO] = recipient.notificationKey;
    } else {
        return callback('No recipients provided', null);
    }

    if (message.collapseKey !== undefined) {
        body[Constants.PARAM_COLLAPSE_KEY] = message.collapseKey;
    }
    if (message.timeToLive !== undefined) {
        body[Constants.PARAM_TIME_TO_LIVE] = message.timeToLive;
    }
    if (message.dryRun !== undefined) {
        body[Constants.PARAM_DRY_RUN] = message.dryRun;
    }
    if (message.priority !== undefined) {
        body[Constants.PARAM_PRIORITY] = message.priority;
    }
    if (message.contentAvailable !== undefined) {
        body[Constants.PARAM_CONTENT_AVAILABLE] = message.contentAvailable;
    }
    if (message.restrictedPackageName !== undefined) {
        body[Constants.PARAM_RESTRICTED_PACKAGE_NAME] = message.restrictedPackageName;
    }
    if (message.data && Object.keys(message.data)) {
        body[Constants.PARAM_DATA_PAYLOAD_KEY] = message.data;
    }
    if (message.notification && Object.keys(message.notification)) {
        body[Constants.PARAM_NOTIFICATION_PAYLOAD_KEY] = message.notification;
    }

    requestBody = JSON.stringify(body);

    post_options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-length': Buffer.byteLength(requestBody, 'utf8'),
            'Authorization': 'key=' + this.key
        },
        uri: Constants.FCM_SEND_URI,
        body: requestBody
    };

    if (this.options && this.options.proxy) {
        post_options.proxy = this.options.proxy;
    }

    if (this.options && this.options.maxSockets) {
        post_options.maxSockets = this.options.maxSockets;
    }

    timeout = Constants.SOCKET_TIMEOUT;

    if (this.options && this.options.timeout) {
        timeout =  this.options.timeout;
    }

    post_options.timeout = timeout;

    post_req = this.req(post_options, function (err, res, resBody) {

        if (err) {
            return callback(err, null);
        }
        if (!res) {
            return callback('response is null', null);
        }
        if (res.statusCode >= 500) {
            debug('GCM service is unavailable (500)');
            return callback(res.statusCode, resBody);
        } else if (res.statusCode === 401) {
            debug('Unauthorized (401). Check that your API token is correct.');
            return callback(res.statusCode, 'Unauthorized. Check that your API token is correct.');
        } else if (res.statusCode !== 200) {
            debug('Invalid request (' + res.statusCode + '): ' + resBody);
            return callback(res.statusCode, resBody);
        }

        try {
            callback(null, JSON.parse(resBody));
        } catch (e) {
            debug("Error handling GCM response " + e);
            callback("error", e);
        }
    });
};
<commit_msg>Slightly improving error messages for unknown send exceptions<commit_after>/*!
 * node-gcm
 * Copyright(c) 2013 <NAME> <<EMAIL>>
 * MIT Licensed
 */
var Constants = require('./constants');
var req = require('request');
var debug = require('debug')('spire-fcm');

function SenderBase(key, options) {
    if (!(this instanceof SenderBase)) {
        return new SenderBase(key, options);
    }

    this.key = key;
    this.options = options;

    // added to support child dependency injection
    this.req = this.req ? this.req : require('request');
}

SenderBase.prototype.sendBaseNoRetry = function(message, recipient, callback) {
    if(!callback) {
        callback = function() {};
    }

    var body = {},
        requestBody,
        post_options,
        post_req,
        timeout;

    if (recipient && recipient.registrationIds) {
        // Registration IDs take precedence
        body[Constants.JSON_REGISTRATION_IDS] = recipient.registrationIds;
    } else if (recipient && recipient.notificationKey && recipient.notificationKey.length) {
        body[Constants.PARAM_TO] = recipient.notificationKey;
    } else {
        return callback('No recipients provided', null);
    }

    if (message.collapseKey !== undefined) {
        body[Constants.PARAM_COLLAPSE_KEY] = message.collapseKey;
    }
    if (message.timeToLive !== undefined) {
        body[Constants.PARAM_TIME_TO_LIVE] = message.timeToLive;
    }
    if (message.dryRun !== undefined) {
        body[Constants.PARAM_DRY_RUN] = message.dryRun;
    }
    if (message.priority !== undefined) {
        body[Constants.PARAM_PRIORITY] = message.priority;
    }
    if (message.contentAvailable !== undefined) {
        body[Constants.PARAM_CONTENT_AVAILABLE] = message.contentAvailable;
    }
    if (message.restrictedPackageName !== undefined) {
        body[Constants.PARAM_RESTRICTED_PACKAGE_NAME] = message.restrictedPackageName;
    }
    if (message.data && Object.keys(message.data)) {
        body[Constants.PARAM_DATA_PAYLOAD_KEY] = message.data;
    }
    if (message.notification && Object.keys(message.notification)) {
        body[Constants.PARAM_NOTIFICATION_PAYLOAD_KEY] = message.notification;
    }

    requestBody = JSON.stringify(body);

    post_options = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-length': Buffer.byteLength(requestBody, 'utf8'),
            'Authorization': 'key=' + this.key
        },
        uri: Constants.FCM_SEND_URI,
        body: requestBody
    };

    if (this.options && this.options.proxy) {
        post_options.proxy = this.options.proxy;
    }

    if (this.options && this.options.maxSockets) {
        post_options.maxSockets = this.options.maxSockets;
    }

    timeout = Constants.SOCKET_TIMEOUT;

    if (this.options && this.options.timeout) {
        timeout =  this.options.timeout;
    }

    post_options.timeout = timeout;

    post_req = this.req(post_options, function (err, res, resBody) {

        if (err) {
            return callback(err, null);
        }
        if (!res) {
            return callback('response is null', null);
        }
        if (res.statusCode >= 500) {
            debug('GCM service is unavailable (500)');
            return callback(res.statusCode, resBody);
        } else if (res.statusCode === 401) {
            debug('Unauthorized (401). Check that your API token is correct.');
            return callback(res.statusCode, 'Unauthorized. Check that your API token is correct.');
        } else if (res.statusCode !== 200) {
            debug('Invalid request (' + res.statusCode + '): ' + resBody);
            return callback(res.statusCode, resBody);
        }

        try {
            callback(null, JSON.parse(resBody));
        } catch (e) {
            debug("Error handling GCM response " + e);
            callback(e);
        }
    });
};

{'fraction_non_alphanumeric': 0.1258534071879428, 'fraction_numerical': 0.005152647172484864, 'mean_word_length': 2.537129840546697, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2712431', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2204, 'n_words': 464}