ComunicWeb/assets/js/common/api.js

192 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-01-04 18:14:27 +00:00
/**
* API main functions
*
* @author Pierre HUBERT
*/
2021-02-13 14:23:43 +00:00
const APIClient = {
2019-05-19 13:56:44 +00:00
/**
* Make an asynchronous request over the API
*
* @returns {Promise}
*/
exec: function(apiURI, args, withLogin){
if(!args)
args = {};
return new Promise((resolve, reject) => {
this.makeAPIrequest(apiURI, args, withLogin, result => {
if(result.error)
reject(result.error);
else
resolve(result);
});
});
},
2018-01-06 09:30:04 +00:00
/**
* Make an API request
*
* @param {String} apiURI The URI to call in the API
* @param {Object} params The params to include in request
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
* @param {Function} nextAction What to do next
*/
makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){
//Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI;
2021-02-13 14:34:37 +00:00
// Add client name
params.client = ComunicWeb.__config.apiClientName;
2018-01-06 09:30:04 +00:00
//Add login tokens to params if required
if(requireLoginTokens){
2021-02-13 14:43:58 +00:00
params.token = LoginTokens.getLoginToken();
}
2019-01-11 13:50:06 +00:00
//Enable incognito mode if required
if(ComunicWeb.components.incognito.management.isEnabled())
params.incognito = true;
2018-01-06 09:30:04 +00:00
//Prepare data to send in request
var count = 0;
var datas = "";
for(paramName in params){
//We add a "&" if it isn't the first param
if(count != 0)
datas += "&";
2017-01-29 14:49:21 +00:00
2018-01-06 09:30:04 +00:00
//We add field value
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
2018-01-06 09:30:04 +00:00
count++; //Increment counter
}
2018-01-06 09:30:04 +00:00
//Create request
var apiXHR = new XMLHttpRequest();
apiXHR.open("POST", requestURL);
2017-01-04 18:14:27 +00:00
2018-01-06 09:30:04 +00:00
//Prepare request response
apiXHR.onreadystatechange = function(){
ComunicWeb.common.api._on_state_change(requestURL, apiXHR, nextAction);
2018-01-06 09:30:04 +00:00
}
//Set request headers
apiXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//Submit request
apiXHR.send(datas);
},
/**
* Make an API request with a prepared form data object
*
* @param {String} apiURI The URI to call in the API
* @param {FormData} data The form data object
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
* @param {Function} nextAction What to do next
*/
makeFormDatarequest: function(apiURI, data, requireLoginTokens, nextAction){
//Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI;
2021-02-13 14:43:58 +00:00
// Add API client name
data.append('client', ComunicWeb.__config.apiClientName);
//Add login tokens to params if required
if(requireLoginTokens){
2021-02-13 14:43:58 +00:00
data.append("token", LoginTokens.getLoginToken())
2019-01-11 13:50:06 +00:00
}
//Enable incognito mode if required
if(ComunicWeb.components.incognito.management.isEnabled())
data.append("incognito", true);
//Create request
var apiXHR = new XMLHttpRequest();
apiXHR.open("POST", requestURL);
//Prepare request response
apiXHR.onreadystatechange = function(){
ComunicWeb.common.api._on_state_change(requestURL, apiXHR, nextAction);
}
//Submit request
apiXHR.send(data);
},
2018-01-06 09:30:04 +00:00
/**
* Handle xhr request chnages
*
* @param {string} requestURL The URL of the request
2018-01-06 09:30:04 +00:00
* @param {XMLHttpRequest} apiXHR The request element
* @param {Function} nextAction What to do once the request is done
*/
_on_state_change: function(requestURL, apiXHR, nextAction){
2017-01-04 18:14:27 +00:00
//We continue only if request is terminated
if(apiXHR.readyState == 4){
//Check if response code is 0
2021-02-13 14:23:43 +00:00
ComunicWeb.common.network.setStatus(apiXHR.status != 0);
2017-06-25 14:30:38 +00:00
//Check if response is empty
if(apiXHR.responseText.length == ""){
//Auto-create a response for empty responses (to avoid Javascript errors and allow the script to continue to execute)
result = {
error : {
code: 0,
message: "Empty response",
},
};
}
2018-05-05 10:12:33 +00:00
else {
//Catch JSON parsing errors
try {
//Parse result
var result = JSON.parse(apiXHR.responseText);
} catch (error) {
//Report error
ComunicWeb.common.error.syntaxtError(error, apiXHR.responseText);
//Set arbitray result content
result = {
error : {
code: 1,
message: "Invalid response",
},
};
}
}
2017-01-29 14:49:21 +00:00
//We check if we got any error
if(result.error){
//Log error
2017-05-27 09:57:05 +00:00
ComunicWeb.debug.logMessage("Got an error in a XHR request! \n Request URL: "+requestURL+" \n Response : "+apiXHR.responseText);
2021-02-13 14:23:43 +00:00
if (result.error.code == 412) {
UserLogin.__userLogin = false;
ComunicWeb.user.loginTokens.deleteLoginTokens();
System.restart();
}
2017-01-29 14:49:21 +00:00
}
2017-01-04 18:14:27 +00:00
//We can do the next step
if(nextAction)
nextAction(result);
2017-01-04 18:14:27 +00:00
}
2018-01-06 09:30:04 +00:00
},
2021-02-13 14:23:43 +00:00
}
ComunicWeb.common.api = APIClient;