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-06-20 14:08:52 +02:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
if (typeof connectFailedCallback !== "undefined") {
|
|
|
|
if (timeout) {
|
|
|
|
xhr.timeout = timeout;
|
|
|
|
}
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
xhr.addEventListener("error", function(e) {
|
|
|
|
connectFailedCallback(e);
|
|
|
|
});
|
|
|
|
xhr.addEventListener("timeout", function() {
|
|
|
|
connectFailedCallback(
|
|
|
|
new Error(__("XMLHttpRequest connection timed out"))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
xhr.addEventListener("load", function() {
|
|
|
|
var response = JSON.parse(xhr.responseText);
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-07-30 21:37:44 +02:00
|
|
|
let error = response.error || response.result && response.result.error;
|
2017-07-30 21:20:54 +02:00
|
|
|
if (error) {
|
2017-06-20 14:08:52 +02:00
|
|
|
if (errorCallback) {
|
2017-07-30 21:20:54 +02:00
|
|
|
errorCallback(error);
|
2017-06-20 14:08:52 +02:00
|
|
|
} else {
|
|
|
|
var errorEvent = new CustomEvent("unhandledError", {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
|
|
|
method: method,
|
|
|
|
params: params,
|
2017-07-30 21:20:54 +02:00
|
|
|
code: error.code,
|
|
|
|
message: error.message || error,
|
|
|
|
data: error.data,
|
2017-06-20 14:08:52 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
document.dispatchEvent(errorEvent);
|
|
|
|
}
|
|
|
|
} else if (callback) {
|
|
|
|
callback(response.result);
|
|
|
|
}
|
|
|
|
});
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
if (connectFailedCallback) {
|
|
|
|
xhr.addEventListener("error", function(event) {
|
|
|
|
connectFailedCallback(event);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
xhr.addEventListener("error", function(event) {
|
|
|
|
var errorEvent = new CustomEvent("unhandledError", {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
code: xhr.status,
|
|
|
|
message: __("Connection to API server failed"),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
document.dispatchEvent(errorEvent);
|
|
|
|
});
|
|
|
|
}
|
2017-03-08 07:23:07 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
const counter = parseInt(sessionStorage.getItem("JSONRPCCounter") || 0);
|
2017-03-08 10:28:00 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
xhr.open("POST", connectionString, true);
|
|
|
|
xhr.send(
|
|
|
|
JSON.stringify({
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: counter,
|
|
|
|
})
|
|
|
|
);
|
2017-03-08 10:28:00 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
sessionStorage.setItem("JSONRPCCounter", counter + 1);
|
2017-05-15 18:34:33 +02:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
return xhr;
|
2017-03-08 07:23:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default jsonrpc;
|