2017-03-08 07:23:07 +01:00
|
|
|
const jsonrpc = {};
|
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
jsonrpc.call = function(
|
2017-06-20 14:08:52 +02:00
|
|
|
connectionString,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
callback,
|
|
|
|
errorCallback,
|
|
|
|
connectFailedCallback,
|
|
|
|
timeout
|
2017-06-06 06:21:55 +02:00
|
|
|
) {
|
2017-11-10 15:55:30 +01:00
|
|
|
function checkAndParse(response) {
|
2017-10-12 20:09:58 +02:00
|
|
|
if (response.status >= 200 && response.status < 300) {
|
2017-11-10 15:55:30 +01:00
|
|
|
return response.json();
|
2017-10-12 20:09:58 +02:00
|
|
|
} else {
|
2017-11-10 15:55:30 +01:00
|
|
|
return response.json().then(json => {
|
|
|
|
let error;
|
|
|
|
if (json.error) {
|
|
|
|
error = new Error(json.error);
|
|
|
|
} else {
|
|
|
|
error = new Error("Protocol error with unknown response signature");
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
2017-06-20 14:08:52 +02:00
|
|
|
}
|
2017-10-12 20:09:58 +02:00
|
|
|
}
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-10-12 20:09:58 +02:00
|
|
|
function makeRequest(url, options) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-21 20:51:12 +01:00
|
|
|
fetch(url, options)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2017-10-12 20:09:58 +02:00
|
|
|
|
|
|
|
if (timeout) {
|
2017-11-10 15:55:30 +01:00
|
|
|
const e = new Error(__("Protocol request timed out"));
|
2017-10-12 20:09:58 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
return reject(e);
|
|
|
|
}, timeout);
|
|
|
|
}
|
2017-06-20 14:08:52 +02:00
|
|
|
});
|
|
|
|
}
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-10-12 20:09:58 +02:00
|
|
|
const counter = parseInt(sessionStorage.getItem("JSONRPCCounter") || 0);
|
|
|
|
const url = connectionString;
|
|
|
|
const options = {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: counter,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
sessionStorage.setItem("JSONRPCCounter", counter + 1);
|
|
|
|
|
|
|
|
return fetch(url, options)
|
2017-11-10 15:55:30 +01:00
|
|
|
.then(checkAndParse)
|
2017-10-12 20:09:58 +02:00
|
|
|
.then(response => {
|
|
|
|
const error =
|
|
|
|
response.error || (response.result && response.result.error);
|
|
|
|
|
|
|
|
if (!error && typeof callback === "function") {
|
|
|
|
return callback(response.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error && typeof errorCallback === "function") {
|
|
|
|
return errorCallback(error);
|
2017-06-20 14:08:52 +02:00
|
|
|
}
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
var errorEvent = new CustomEvent("unhandledError", {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
|
|
|
method: method,
|
|
|
|
params: params,
|
2017-10-12 20:09:58 +02:00
|
|
|
code: error.code,
|
|
|
|
message: error.message || error,
|
|
|
|
data: error.data,
|
2017-06-20 14:08:52 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
document.dispatchEvent(errorEvent);
|
|
|
|
})
|
2017-10-12 20:09:58 +02:00
|
|
|
.catch(e => {
|
|
|
|
if (connectFailedCallback) {
|
|
|
|
return connectFailedCallback(e);
|
|
|
|
}
|
2017-05-15 18:34:33 +02:00
|
|
|
|
2017-10-12 20:09:58 +02:00
|
|
|
var errorEvent = new CustomEvent("unhandledError", {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
code: e.response && e.response.status,
|
|
|
|
message: __("Connection to API server failed"),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
document.dispatchEvent(errorEvent);
|
|
|
|
});
|
2017-03-08 07:23:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default jsonrpc;
|