2017-01-04 18:14:27 +00:00
|
|
|
/**
|
|
|
|
* API main functions
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request
|
|
|
|
*
|
|
|
|
* @param {String} apiURI The URI to call in the API
|
2017-01-08 14:54:25 +00:00
|
|
|
* @param {Object} params The params to include in request
|
2017-01-29 14:49:21 +00:00
|
|
|
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
|
2017-01-04 18:14:27 +00:00
|
|
|
* @param {Function} nextAction What to do next
|
|
|
|
*/
|
2017-01-29 14:49:21 +00:00
|
|
|
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginTokens, nextAction){
|
2017-01-04 18:14:27 +00:00
|
|
|
//Prepare the request URL
|
2017-01-08 14:54:25 +00:00
|
|
|
var requestURL = ComunicWeb.__config.apiURL + apiURI;
|
2017-01-04 18:14:27 +00:00
|
|
|
|
2018-01-02 16:48:04 +00:00
|
|
|
//Add API service tokens
|
|
|
|
params.serviceName = ComunicWeb.__config.apiServiceName;
|
|
|
|
params.serviceToken = ComunicWeb.__config.apiServiceToken;
|
|
|
|
|
2017-01-29 14:49:21 +00:00
|
|
|
//Add login tokens to params if required
|
|
|
|
if(requireLoginTokens){
|
|
|
|
//Get login tokens
|
|
|
|
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
|
|
|
|
|
2017-05-19 16:17:32 +00:00
|
|
|
if(tokens){
|
|
|
|
//Add tokens
|
|
|
|
params.userToken1 = tokens.token1;
|
|
|
|
params.userToken2 = tokens.token2;
|
|
|
|
}
|
|
|
|
|
2017-01-29 14:49:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 18:14:27 +00:00
|
|
|
//Prepare data to send in request
|
|
|
|
var count = 0;
|
|
|
|
var datas = "";
|
2017-01-08 14:54:25 +00:00
|
|
|
for(paramName in params){
|
|
|
|
//We add a "&" if it isn't the first param
|
|
|
|
if(count != 0)
|
|
|
|
datas += "&";
|
|
|
|
|
|
|
|
//We add field value
|
|
|
|
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
|
|
|
|
|
|
|
|
count++; //Increment counter
|
2017-01-29 14:49:21 +00:00
|
|
|
}
|
2017-01-04 18:14:27 +00:00
|
|
|
|
|
|
|
//Create request
|
|
|
|
var apiXHR = new XMLHttpRequest();
|
|
|
|
apiXHR.open("POST", requestURL);
|
|
|
|
|
|
|
|
//Prepare request response
|
|
|
|
apiXHR.onreadystatechange = function(){
|
|
|
|
//We continue only if request is terminated
|
|
|
|
if(apiXHR.readyState == 4){
|
2017-06-07 15:26:51 +00:00
|
|
|
|
|
|
|
//Check if response code is 0
|
|
|
|
if(apiXHR.status == 0){
|
|
|
|
//An error occured
|
|
|
|
ComunicWeb.common.network.setStatus(false);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//It is a success
|
|
|
|
ComunicWeb.common.network.setStatus(true);
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//Prepare result
|
|
|
|
var result = JSON.parse(apiXHR.responseText);
|
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);
|
2017-01-29 14:49:21 +00:00
|
|
|
}
|
2017-01-04 18:14:27 +00:00
|
|
|
|
|
|
|
//We can do the next step
|
2017-06-04 14:02:51 +00:00
|
|
|
if(nextAction)
|
|
|
|
nextAction(result);
|
2017-01-04 18:14:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 14:54:25 +00:00
|
|
|
|
|
|
|
//Set request headers
|
|
|
|
apiXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
//Submit request
|
|
|
|
apiXHR.send(datas);
|
2017-01-04 18:14:27 +00:00
|
|
|
};
|