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-17 09:48:34 +08:00
|
|
|
protected_session_id: protectedSessionId,
|
|
|
|
source_id: glob.sourceId
|
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-12-19 11:06:24 +08:00
|
|
|
console.log(now(), "Request #" + requestId + " to " + method + " " + url);
|
2017-11-30 12:30:35 +08:00
|
|
|
|
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-12-19 11:06:24 +08:00
|
|
|
console.log(now(), "Response #" + arg.requestId + ": " + arg.statusCode);
|
2017-11-30 12:30:35 +08:00
|
|
|
|
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-12-19 11:56:44 +08:00
|
|
|
|
|
|
|
throw e;
|
2017-11-29 09:52:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
get,
|
|
|
|
post,
|
|
|
|
put,
|
2018-01-07 21:24:04 +08:00
|
|
|
remove,
|
|
|
|
getHeaders
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
|
|
|
})();
|