mirror of
https://github.com/pierre42100/ComunicWeb
synced 2024-12-25 02:18:51 +00:00
Splited api function in two
This commit is contained in:
parent
2045e1294c
commit
c345e66bce
@ -3,56 +3,74 @@
|
|||||||
*
|
*
|
||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
ComunicWeb.common.api = {
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
/**
|
//Add API service tokens
|
||||||
* Make an API request
|
params.serviceName = ComunicWeb.__config.apiServiceName;
|
||||||
*
|
params.serviceToken = ComunicWeb.__config.apiServiceToken;
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginTokens, nextAction){
|
|
||||||
//Prepare the request URL
|
|
||||||
var requestURL = ComunicWeb.__config.apiURL + apiURI;
|
|
||||||
|
|
||||||
//Add API service tokens
|
//Add login tokens to params if required
|
||||||
params.serviceName = ComunicWeb.__config.apiServiceName;
|
if(requireLoginTokens){
|
||||||
params.serviceToken = ComunicWeb.__config.apiServiceToken;
|
//Get login tokens
|
||||||
|
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
|
||||||
|
|
||||||
//Add login tokens to params if required
|
if(tokens){
|
||||||
if(requireLoginTokens){
|
//Add tokens
|
||||||
//Get login tokens
|
params.userToken1 = tokens.token1;
|
||||||
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
|
params.userToken2 = tokens.token2;
|
||||||
|
}
|
||||||
|
|
||||||
if(tokens){
|
|
||||||
//Add tokens
|
|
||||||
params.userToken1 = tokens.token1;
|
|
||||||
params.userToken2 = tokens.token2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
//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 += "&";
|
||||||
|
|
||||||
//Prepare data to send in request
|
//We add field value
|
||||||
var count = 0;
|
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
|
||||||
var datas = "";
|
|
||||||
for(paramName in params){
|
|
||||||
//We add a "&" if it isn't the first param
|
|
||||||
if(count != 0)
|
|
||||||
datas += "&";
|
|
||||||
|
|
||||||
//We add field value
|
count++; //Increment counter
|
||||||
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
|
}
|
||||||
|
|
||||||
count++; //Increment counter
|
//Create request
|
||||||
}
|
var apiXHR = new XMLHttpRequest();
|
||||||
|
apiXHR.open("POST", requestURL);
|
||||||
|
|
||||||
//Create request
|
//Prepare request response
|
||||||
var apiXHR = new XMLHttpRequest();
|
apiXHR.onreadystatechange = function(){
|
||||||
apiXHR.open("POST", requestURL);
|
ComunicWeb.common.api._on_state_change(apiXHR, nextAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set request headers
|
||||||
|
apiXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
//Submit request
|
||||||
|
apiXHR.send(datas);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle xhr request chnages
|
||||||
|
*
|
||||||
|
* @param {XMLHttpRequest} apiXHR The request element
|
||||||
|
* @param {Function} nextAction What to do once the request is done
|
||||||
|
*/
|
||||||
|
_on_state_change: function(apiXHR, nextAction){
|
||||||
|
|
||||||
//Prepare request response
|
|
||||||
apiXHR.onreadystatechange = function(){
|
|
||||||
//We continue only if request is terminated
|
//We continue only if request is terminated
|
||||||
if(apiXHR.readyState == 4){
|
if(apiXHR.readyState == 4){
|
||||||
|
|
||||||
@ -90,11 +108,5 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginToke
|
|||||||
if(nextAction)
|
if(nextAction)
|
||||||
nextAction(result);
|
nextAction(result);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
}
|
||||||
//Set request headers
|
|
||||||
apiXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
|
|
||||||
//Submit request
|
|
||||||
apiXHR.send(datas);
|
|
||||||
};
|
|
@ -51,6 +51,8 @@ var ComunicWeb = {
|
|||||||
* Make an API request
|
* Make an API request
|
||||||
*/
|
*/
|
||||||
makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){},
|
makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){},
|
||||||
|
|
||||||
|
//TODO : implement
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user