2017-11-29 09:52:38 +08:00
|
|
|
const server = (function() {
|
2017-11-30 12:30:35 +08:00
|
|
|
function getHeaders() {
|
2017-12-01 08:58:00 +08:00
|
|
|
let protectedSessionId = null;
|
|
|
|
|
|
|
|
try { // this is because protected session might not be declared in some cases - like when it's included in migration page
|
|
|
|
protectedSessionId = protected_session.getProtectedSessionId();
|
|
|
|
}
|
|
|
|
catch(e) {}
|
|
|
|
|
2017-11-30 12:30:35 +08:00
|
|
|
return {
|
2017-12-01 08:58:00 +08:00
|
|
|
'x-protected-session-id': protectedSessionId
|
2017-11-30 12:30:35 +08:00
|
|
|
};
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function get(url) {
|
2017-11-29 11:01:02 +08:00
|
|
|
return await call('GET', url);
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function post(url, data) {
|
2017-11-29 11:01:02 +08:00
|
|
|
return await call('POST', url, data);
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function put(url, data) {
|
2017-11-29 11:01:02 +08:00
|
|
|
return await call('PUT', url, data);
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(url) {
|
2017-11-29 11:01:02 +08:00
|
|
|
return await call('DELETE', url);
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 11:01:02 +08:00
|
|
|
let i = 1;
|
|
|
|
const reqResolves = {};
|
|
|
|
|
|
|
|
async function call(method, url, data) {
|
|
|
|
if (isElectron()) {
|
|
|
|
const ipc = require('electron').ipcRenderer;
|
|
|
|
const requestId = i++;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
reqResolves[requestId] = resolve;
|
|
|
|
|
2017-11-30 12:30:35 +08:00
|
|
|
console.log("Request #" + requestId + " to " + method + " " + url);
|
|
|
|
|
2017-11-29 11:01:02 +08:00
|
|
|
ipc.send('server-request', {
|
|
|
|
requestId: requestId,
|
2017-11-30 12:30:35 +08:00
|
|
|
headers: getHeaders(),
|
2017-11-29 11:01:02 +08:00
|
|
|
method: method,
|
|
|
|
url: "/" + baseApiUrl + url,
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return await ajax(url, method, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isElectron()) {
|
|
|
|
const ipc = require('electron').ipcRenderer;
|
|
|
|
|
|
|
|
ipc.on('server-response', (event, arg) => {
|
2017-11-30 12:30:35 +08:00
|
|
|
console.log("Response #" + arg.requestId + ": " + arg.statusCode);
|
|
|
|
|
2017-11-29 11:01:02 +08:00
|
|
|
reqResolves[arg.requestId](arg.body);
|
2017-11-30 12:30:35 +08:00
|
|
|
|
|
|
|
delete reqResolves[arg.requestId];
|
2017-11-29 11:01:02 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ajax(url, method, data) {
|
2017-11-29 09:52:38 +08:00
|
|
|
const options = {
|
|
|
|
url: baseApiUrl + url,
|
2017-11-30 12:30:35 +08:00
|
|
|
type: method,
|
|
|
|
headers: getHeaders()
|
2017-11-29 09:52:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
options.data = JSON.stringify(data);
|
|
|
|
options.contentType = "application/json";
|
|
|
|
}
|
|
|
|
|
|
|
|
return await $.ajax(options).catch(e => {
|
2017-11-30 11:03:03 +08:00
|
|
|
showError("Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText);
|
2017-11-29 09:52:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
get,
|
|
|
|
post,
|
|
|
|
put,
|
2017-11-30 12:30:35 +08:00
|
|
|
remove
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
})();
|