<commit_before>/*  Function to check API key */

var checkApiKey = function(input, callback) {
    var apiKey = input;
    $.ajaxSetup({
        beforeSend: function(xhr) {
            xhr.setRequestHeader('X-AUTH', apiKey);
        }
    });

    // Send request to check API key and also store the Xuid on success 
    $.get('https://xboxapi.com/v2/accountXuid', function (data) {
        callback(data);
    // Return false
    }).fail(function() {
        callback(false);
    });
};

/* API call function */

var apiCall = function (apiKey, endpoint, cache, callback) {
    // Try to get the cached API results out of PouchDB
    retrieveDbData(endpoint, function(data){
        timestampNow = $.now();
        var dataExpiredCondition = (timestampNow - data.timestamp) / 1000,
            dataExpired = (dataExpiredCondition >= cache);

        // If no result or caching value exceeded => call API directly
        // Otherwise return cached API results out of PouchDB
        if (!data || dataExpired) {
            // Display caching status of cachable objects in PouchDB
            if (data && dataExpired) {
                console.log('[INFO] Cache expired for "' + endpoint + ': ' + dataExpiredCondition + ' ms > ' + cache);
            }

            console.log('[INFO] API call "' + endpoint + '" started');

            // Execute retrieve function
            retrieveApiData(apiKey, endpoint, function(data) {
                // In case of server side API error, return false
                // Otherwise proceed to transfer the API results into local database
                if (!data) {
                    console.log('[WARN] Couldn\'t finish API call "' + endpoint + '"!');
                    callback(false);
                } else {
                    // Create new object to append a timestamp
                    var jsonObject = {};
                    jsonObject.timestamp  = timestampNow;
                    jsonObject.jsonData = data;

                    // Try to submit the data into the DB
                    submitDbData(jsonObject, endpoint, function(submitData){
                        // Return false in case of error
                        // Otherwise, send to callback 
                        if (!submitData) {
                            callback(false);
                        } else {
                            callback(data);
                            console.log('[INFO] API call "' + endpoint + '" finished');                               
                        }
                    });
                }
            });
        } else {
            console.log('[INFO] DB call "' + endpoint + '" started');
            // Retrieve cached data
            retrieveDbData(endpoint, function(data){
                // Send callback
                callback(data.jsonData);
                console.log('[INFO] DB call "' + endpoint + '" finished');
            });
        }
    });
};

/* Retrieve direct API data function */
    
var retrieveApiData = function(apiKey, endpoint, callback) {
    // Prepare ajax request
    $.ajaxSetup({
        beforeSend: function(xhr) {
            // Add X-AUTH header
            xhr.setRequestHeader('X-AUTH', apiKey);
        }
    });

    // Fire GET
    $.get('https://xboxapi.com/v2'+endpoint, function (data) {
        callback(data);
    // In case of any error, return false
    }).fail(function() {
        callback(false);
    });
};

/* Retrieve DB data function */

var retrieveDbData = function (endpoint, callback) {
    // PouchDB init
    var pouchdb = new PouchDB(endpoint);
    // Try to get()
    pouchdb.get('jsonData', function(err, doc) {
        // In case of error, return false
        if (err) {
            if (err.status != 404) {
                console.log(err);
            }
            callback(false);
        }
        // If no error, send data to callback
        if (doc) {
            callback(doc);
        }
    });
};

/* Sumbit API data function */

var submitApiData = function (apikey, endpoint, content, callback) {
    // Test API key
    apikey = "ABC123";

    // Prepare ajax request<commit_msg>Remove apikey parameter in submitApiData()<commit_after>/*  Function to check API key */

var checkApiKey = function(input, callback) {
    var apiKey = input;
    $.ajaxSetup({
        beforeSend: function(xhr) {
            xhr.setRequestHeader('X-AUTH', apiKey);
        }
    });

    // Send request to check API key and also store the Xuid on success 
    $.get('https://xboxapi.com/v2/accountXuid', function (data) {
        callback(data);
    // Return false
    }).fail(function() {
        callback(false);
    });
};

/* API call function */

var apiCall = function (apiKey, endpoint, cache, callback) {
    // Try to get the cached API results out of PouchDB
    retrieveDbData(endpoint, function(data){
        timestampNow = $.now();
        var dataExpiredCondition = (timestampNow - data.timestamp) / 1000,
            dataExpired = (dataExpiredCondition >= cache);

        // If no result or caching value exceeded => call API directly
        // Otherwise return cached API results out of PouchDB
        if (!data || dataExpired) {
            // Display caching status of cachable objects in PouchDB
            if (data && dataExpired) {
                console.log('[INFO] Cache expired for "' + endpoint + ': ' + dataExpiredCondition + ' ms > ' + cache);
            }

            console.log('[INFO] API call "' + endpoint + '" started');

            // Execute retrieve function
            retrieveApiData(apiKey, endpoint, function(data) {
                // In case of server side API error, return false
                // Otherwise proceed to transfer the API results into local database
                if (!data) {
                    console.log('[WARN] Couldn\'t finish API call "' + endpoint + '"!');
                    callback(false);
                } else {
                    // Create new object to append a timestamp
                    var jsonObject = {};
                    jsonObject.timestamp  = timestampNow;
                    jsonObject.jsonData = data;

                    // Try to submit the data into the DB
                    submitDbData(jsonObject, endpoint, function(submitData){
                        // Return false in case of error
                        // Otherwise, send to callback 
                        if (!submitData) {
                            callback(false);
                        } else {
                            callback(data);
                            console.log('[INFO] API call "' + endpoint + '" finished');                               
                        }
                    });
                }
            });
        } else {
            console.log('[INFO] DB call "' + endpoint + '" started');
            // Retrieve cached data
            retrieveDbData(endpoint, function(data){
                // Send callback
                callback(data.jsonData);
                console.log('[INFO] DB call "' + endpoint + '" finished');
            });
        }
    });
};

/* Retrieve direct API data function */
    
var retrieveApiData = function(apiKey, endpoint, callback) {
    // Prepare ajax request
    $.ajaxSetup({
        beforeSend: function(xhr) {
            // Add X-AUTH header
            xhr.setRequestHeader('X-AUTH', apiKey);
        }
    });

    // Fire GET
    $.get('https://xboxapi.com/v2'+endpoint, function (data) {
        callback(data);
    // In case of any error, return false
    }).fail(function() {
        callback(false);
    });
};

/* Retrieve DB data function */

var retrieveDbData = function (endpoint, callback) {
    // PouchDB init
    var pouchdb = new PouchDB(endpoint);
    // Try to get()
    pouchdb.get('jsonData', function(err, doc) {
        // In case of error, return false
        if (err) {
            if (err.status != 404) {
                console.log(err);
            }
            callback(false);
        }
        // If no error, send data to callback
        if (doc) {
            callback(doc);
        }
    });
};

/* Sumbit API data function */

var submitApiData = function (endpoint, content, callback) {
    // Test API key
    apikey = "ABC123";

    // Prepare ajax request
{'fraction_non_alphanumeric': 0.11263373001562688, 'fraction_numerical': 0.00288496213487198, 'mean_word_length': 1.5227410551849605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '4403956', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2049, 'n_words': 628}