<commit_before>'use strict';

var options =
{
    baseUrl: 'https://mailosaur.com/api/',
    smtpHost: 'mailosaur.io',
    apiKey: ''
}

function Mailbox(mailbox) {
    function http(opts, done) {
        var request = require('request'),
            route = opts.route.replace(':mailbox', mailbox),
            params = {
                url: options.baseUrl + route,
                method: (opts.method || 'GET'),
                auth: {
                    user: options.apiKey,
                    pass: ''
                },
                json: true
            };
        if (opts.binary) {
            params.encoding = null;
        }
        if (done) {
            request(params, function (err, headers, body) {
                if (!err) {
                    if (headers.statusCode != 200 && headers.statusCode != 204) {
                        throw new Error(headers.statusCode + ': ' + body);
                    }
                }
                done(err, body);
            });
        }
        else {
            var Promise = require("bluebird");

            return new Promise(function (resolve, reject) {
                request(params, function (err, headers, body) {
                    if (!err) {
                        if (headers.statusCode != 200 && headers.statusCode != 204) {
                            var err = new Error(headers.statusCode + ': ' + body);
                            reject(err);
                            throw err;
                        }
                    }
                    resolve(body);
                });
            });
        }
    }

    return {
        getEmails: function (searchPattern, cb) {
            var route = 'mailboxes/:mailbox/emails';
            if (typeof(searchPattern) === "string") {
                route += '/?search=' + searchPattern;
            }
            else {
                cb = searchPattern;
            }
            return http({
                route: route
            }, cb);
        },

        getEmailsByRecipient: function (recipient, cb) {
            return http({
                route: 'mailboxes/:mailbox/emails/?recipient=' + recipient
            }, cb);
        },

        getEmail: function (email, cb) {
            return http({
                route: 'emails/' + email
            }, cb);
        },

        deleteAllEmail: function (cb) {
            return http({
                route: 'mailboxes/:mailbox/empty',
                method: 'POST'
            }, cb);
        },

        deleteEmail: function (email, cb) {
            return http({
                route: 'emails/' + email + '/delete',
                method: 'POST'
            }, cb);
        },

        getAttachment: function (attachmentId, cb) {
            return http({
                route: 'attachments/' + attachmentId,
                method: 'GET',
                binary: true
            }, cb);
        },

        getRawEmail: function (rawId, cb) {
            return http({
                route: 'raw/' + rawId,
                method: 'GET',
                binary: true
            }, cb);
        },

        generateEmailAddress: function () {
            var random = Math.random().toString(36);
            return random + '.' + mailbox + '@' + options.smtpHost;
        }
    };
};

module.exports = function mailosaur(apiKey) {
    options.apiKey = apiKey;

    options.baseUrl = process.env.MAILOSAUR_BASE_URL || options.baseUrl;
    options.smtpHost = process.env.MAILOSAUR_SMTP_HOST || options.smtpHost;

    return {
        Mailbox: Mailbox
    }
};<commit_msg>Add logging when using alternative base url<commit_after>'use strict';

var options =
{
    baseUrl: 'https://mailosaur.com/api/',
    smtpHost: 'mailosaur.io',
    apiKey: ''
}

function Mailbox(mailbox) {
    function http(opts, done) {
        var request = require('request'),
            route = opts.route.replace(':mailbox', mailbox),
            params = {
                url: options.baseUrl + route,
                method: (opts.method || 'GET'),
                auth: {
                    user: options.apiKey,
                    pass: ''
                },
                json: true
            };
        if (opts.binary) {
            params.encoding = null;
        }
        if (done) {
            request(params, function (err, headers, body) {
                if (!err) {
                    if (headers.statusCode != 200 && headers.statusCode != 204) {
                        throw new Error(headers.statusCode + ': ' + body);
                    }
                }
                done(err, body);
            });
        }
        else {
            var Promise = require("bluebird");

            return new Promise(function (resolve, reject) {
                request(params, function (err, headers, body) {
                    if (!err) {
                        if (headers.statusCode != 200 && headers.statusCode != 204) {
                            var err = new Error(headers.statusCode + ': ' + body);
                            reject(err);
                            throw err;
                        }
                    }
                    resolve(body);
                });
            });
        }
    }

    return {
        getEmails: function (searchPattern, cb) {
            var route = 'mailboxes/:mailbox/emails';
            if (typeof(searchPattern) === "string") {
                route += '/?search=' + searchPattern;
            }
            else {
                cb = searchPattern;
            }
            return http({
                route: route
            }, cb);
        },

        getEmailsByRecipient: function (recipient, cb) {
            return http({
                route: 'mailboxes/:mailbox/emails/?recipient=' + recipient
            }, cb);
        },

        getEmail: function (email, cb) {
            return http({
                route: 'emails/' + email
            }, cb);
        },

        deleteAllEmail: function (cb) {
            return http({
                route: 'mailboxes/:mailbox/empty',
                method: 'POST'
            }, cb);
        },

        deleteEmail: function (email, cb) {
            return http({
                route: 'emails/' + email + '/delete',
                method: 'POST'
            }, cb);
        },

        getAttachment: function (attachmentId, cb) {
            return http({
                route: 'attachments/' + attachmentId,
                method: 'GET',
                binary: true
            }, cb);
        },

        getRawEmail: function (rawId, cb) {
            return http({
                route: 'raw/' + rawId,
                method: 'GET',
                binary: true
            }, cb);
        },

        generateEmailAddress: function () {
            var random = Math.random().toString(36);
            return random + '.' + mailbox + '@' + options.smtpHost;
        }
    };
};

module.exports = function mailosaur(apiKey) {
    options.apiKey = apiKey;

    options.baseUrl = process.env.MAILOSAUR_BASE_URL || options.baseUrl;
    options.smtpHost = process.env.MAILOSAUR_SMTP_HOST || options.smtpHost;

    if (process.env.MAILOSAUR_BASE_URL) {
        console.log('Using', options.baseUrl, options.smtpHost);
    }

    return {
        Mailbox: Mailbox
    }
};
{'fraction_non_alphanumeric': 0.1197930846719303, 'fraction_numerical': 0.0038115981486523277, 'mean_word_length': 1.174955595026643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '5348136', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1853, 'n_words': 417}