2018-03-26 01:41:29 +08:00
|
|
|
import protectedSessionService from './protected_session.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function getHeaders() {
|
|
|
|
let protectedSessionId = null;
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
try { // this is because protected session might not be declared in some cases - like when it's included in migration page
|
2018-03-26 01:41:29 +08:00
|
|
|
protectedSessionId = protectedSessionService.getProtectedSessionId();
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
catch(e) {}
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// headers need to be lowercase because node.js automatically converts them to lower case
|
|
|
|
// so hypothetical protectedSessionId becomes protectedsessionid on the backend
|
|
|
|
return {
|
|
|
|
protected_session_id: protectedSessionId,
|
|
|
|
source_id: glob.sourceId
|
|
|
|
};
|
|
|
|
}
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function get(url) {
|
|
|
|
return await call('GET', url);
|
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function post(url, data) {
|
|
|
|
return await call('POST', url, data);
|
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function put(url, data) {
|
|
|
|
return await call('PUT', url, data);
|
|
|
|
}
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function remove(url) {
|
|
|
|
return await call('DELETE', url);
|
|
|
|
}
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
let i = 1;
|
|
|
|
const reqResolves = {};
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function call(method, url, data) {
|
2018-03-25 11:37:55 +08:00
|
|
|
if (utils.isElectron()) {
|
2017-11-29 11:01:02 +08:00
|
|
|
const ipc = require('electron').ipcRenderer;
|
2018-03-25 23:09:17 +08:00
|
|
|
const requestId = i++;
|
2017-11-29 11:01:02 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
reqResolves[requestId] = resolve;
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
console.log(utils.now(), "Request #" + requestId + " to " + method + " " + url);
|
2017-11-30 12:30:35 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
ipc.send('server-request', {
|
|
|
|
requestId: requestId,
|
|
|
|
headers: getHeaders(),
|
|
|
|
method: method,
|
|
|
|
url: "/" + baseApiUrl + url,
|
|
|
|
data: data
|
|
|
|
});
|
2017-11-29 11:01:02 +08:00
|
|
|
});
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
else {
|
|
|
|
return await ajax(url, method, data);
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function ajax(url, method, data) {
|
|
|
|
const options = {
|
|
|
|
url: baseApiUrl + url,
|
|
|
|
type: method,
|
|
|
|
headers: getHeaders()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
options.data = JSON.stringify(data);
|
|
|
|
options.contentType = "application/json";
|
2017-11-29 09:52:38 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
return await $.ajax(options).catch(e => {
|
|
|
|
const message = "Error when calling " + method + " " + url + ": " + e.status + " - " + e.statusText;
|
|
|
|
utils.showError(message);
|
|
|
|
utils.throwError(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 01:13:26 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
if (utils.isElectron()) {
|
|
|
|
const ipc = require('electron').ipcRenderer;
|
|
|
|
|
|
|
|
ipc.on('server-response', (event, arg) => {
|
|
|
|
console.log(utils.now(), "Response #" + arg.requestId + ": " + arg.statusCode);
|
|
|
|
|
|
|
|
reqResolves[arg.requestId](arg.body);
|
|
|
|
|
|
|
|
delete reqResolves[arg.requestId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
get,
|
|
|
|
post,
|
|
|
|
put,
|
|
|
|
remove,
|
|
|
|
ajax,
|
|
|
|
// don't remove, used from CKEditor image upload!
|
|
|
|
getHeaders
|
|
|
|
};
|