- 1 :
/**
- 2 :
* @file This file provides functions for sending AJAX requests to the Ingress API.
- 3 :
* @module send_request
- 4 :
*/
- 5 :
- 6 :
/**
- 7 :
* Sends an AJAX POST request to the Ingress API.
- 8 :
*
- 9 :
* @function postAjax
- 10 :
* @param {string} action - The last part of the URL, automatically appended to the Ingress API endpoint.
- 11 :
* @param {Object} data - JSON data to post. The method is derived automatically from action but may be overridden.
- 12 :
* Expects to be given a Hash. Strings are not supported.
- 13 :
* @param {Function} successCallback - Function to call on success. See jQuery API docs for available arguments.
- 14 :
* @param {Function} errorCallback - Function to call on error. Additionally, it is logged if the request failed.
- 15 :
* @returns {jqXHR} The jQuery wrapped XMLHttpRequest object.
- 16 :
*/
- 17 :
window.postAjax = function (action, data, successCallback, errorCallback) {
- 18 :
// state management functions... perhaps should be outside of this func?
- 19 :
- 20 :
// var remove = function(data, textStatus, jqXHR) { window.requests.remove(jqXHR); };
- 21 :
// var errCnt = function(jqXHR) { window.failedRequestCount++; window.requests.remove(jqXHR); };
- 22 :
- 23 :
if (window.latestFailedRequestTime && window.latestFailedRequestTime < Date.now() - 120 * 1000) {
- 24 :
// no errors in the last two minutes - clear the error count
- 25 :
window.failedRequestCount = 0;
- 26 :
window.latestFailedRequestTime = undefined;
- 27 :
}
- 28 :
- 29 :
var onError = function (jqXHR, textStatus, errorThrown) {
- 30 :
window.requests.remove(jqXHR);
- 31 :
window.failedRequestCount++;
- 32 :
- 33 :
window.latestFailedRequestTime = Date.now();
- 34 :
- 35 :
// pass through to the user error func, if one exists
- 36 :
if (errorCallback) {
- 37 :
errorCallback(jqXHR, textStatus, errorThrown);
- 38 :
}
- 39 :
};
- 40 :
- 41 :
var onSuccess = function (data, textStatus, jqXHR) {
- 42 :
window.requests.remove(jqXHR);
- 43 :
- 44 :
// the Niantic server can return a HTTP success, but the JSON response contains an error. handle that sensibly
- 45 :
if (data && data.error && data.error === 'out of date') {
- 46 :
window.failedRequestCount++;
- 47 :
// let's call the error callback in thos case...
- 48 :
if (errorCallback) {
- 49 :
errorCallback(jqXHR, textStatus, "data.error == 'out of date'");
- 50 :
}
- 51 :
- 52 :
window.outOfDateUserPrompt();
- 53 :
} else {
- 54 :
successCallback(data, textStatus, jqXHR);
- 55 :
}
- 56 :
};
- 57 :
- 58 :
// we set this flag when we want to block all requests due to having an out of date CURRENT_VERSION
- 59 :
if (window.blockOutOfDateRequests) {
- 60 :
window.failedRequestCount++;
- 61 :
window.latestFailedRequestTime = Date.now();
- 62 :
- 63 :
// call the error callback, if one exists
- 64 :
if (errorCallback) {
- 65 :
// NOTE: error called on a setTimeout - as it won't be expected to be synchronous
- 66 :
// ensures no recursion issues if the error handler immediately resends the request
- 67 :
setTimeout(function () {
- 68 :
errorCallback(null, undefined, 'window.blockOutOfDateRequests is set');
- 69 :
}, 10);
- 70 :
}
- 71 :
return;
- 72 :
}
- 73 :
- 74 :
var versionStr = window.niantic_params.CURRENT_VERSION;
- 75 :
var post_data = JSON.stringify($.extend({}, data, { v: versionStr }));
- 76 :
- 77 :
var result = $.ajax({
- 78 :
url: '/r/' + action,
- 79 :
type: 'POST',
- 80 :
data: post_data,
- 81 :
context: data,
- 82 :
dataType: 'json',
- 83 :
success: [onSuccess],
- 84 :
error: [onError],
- 85 :
contentType: 'application/json; charset=utf-8',
- 86 :
beforeSend: function (req) {
- 87 :
req.setRequestHeader('X-CSRFToken', window.readCookie('csrftoken'));
- 88 :
},
- 89 :
});
- 90 :
- 91 :
window.requests.add(result);
- 92 :
- 93 :
return result;
- 94 :
};
- 95 :
- 96 :
/**
- 97 :
* Displays a dialog prompt to the user when the IITC version is out of date.
- 98 :
* Blocks all requests while the dialog is open.
- 99 :
*
- 100 :
* @function outOfDateUserPrompt
- 101 :
*/
- 102 :
window.outOfDateUserPrompt = function () {
- 103 :
// we block all requests while the dialog is open.
- 104 :
if (!window.blockOutOfDateRequests) {
- 105 :
window.blockOutOfDateRequests = true;
- 106 :
- 107 :
window.dialog({
- 108 :
title: 'Reload IITC',
- 109 :
html:
- 110 :
'<p>IITC is using an outdated version code. This will happen when Niantic updates the standard intel site.</p>' +
- 111 :
'<p>You need to reload the page to get the updated changes.</p>' +
- 112 :
'<p>If you have just reloaded the page, then an old version of the standard site script is cached somewhere.' +
- 113 :
'In this case, try clearing your cache, or waiting 15-30 minutes for the stale data to expire.</p>',
- 114 :
buttons: {
- 115 :
RELOAD: function () {
- 116 :
if (window.isApp && window.app.reloadIITC) {
- 117 :
window.app.reloadIITC();
- 118 :
} else {
- 119 :
window.location.reload();
- 120 :
}
- 121 :
},
- 122 :
},
- 123 :
close: function () {
- 124 :
delete window.blockOutOfDateRequests;
- 125 :
},
- 126 :
});
- 127 :
}
- 128 :
};