nopaque/vre_www/website/js/vre_manager_rest.js

180 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-06-03 12:57:09 +00:00
var VREMANAGER = "http://" + window.location.hostname + ":5000";
/**
* Sends an asynchronous HTTP request to the vre manager in order to create a
* new job.
* @param {File} file - The file to process with the job.
* @param {string} language - The language of the file contents.
* @param {string} name - A name for the job, this doesn't have to be unique.
* @param {string} service - The service this job belongs to.
* @param {string} user - The user this job belongs to.
* @param {function} [callback] - A function, that is called after the HTTP
* request finished.
*/
function createJob(file, language, name, service, user, callback = console.log) {
var formData;
var oReq;
var query;
formData = new FormData();
formData.append("file", file);
oReq = new XMLHttpRequest();
oReq.addEventListener("loadend", function(oEvent) {
if (this.status === 201) {
M.toast({html: "Auftrag '" + name + "' wurde erstellt."});
callback(JSON.parse(this.responseText));
} else {
if (this.status === 415) {
M.toast({html: "Auftrag '" + name + "' konnte nicht erstellt werden.<br>(Unerwarteter Dateityp)"});
} else {
M.toast({html: "Auftrag '" + name + "' konnte nicht erstellt werden.<br>(Fehlercode: " + this.status + ")"});
}
}
});
query = "?language=" + language + "&name=" + name + "&service=" + service + "&user=" + user;
oReq.open("POST", VREMANAGER + "/vre/jobs" + query);
oReq.send(formData);
}
/**
* Sends an asynchronous HTTP request to the vre manager in order to delete an
* existing job.
* @param {string} id - The id of the job to be deleted.
* @param {function} [callback] - A function, that is called after the HTTP
* request finished.
*/
function deleteJob(id, callback = console.log) {
var oReq;
oReq = new XMLHttpRequest();
oReq.addEventListener("loadend", function(oEvent) {
if (this.status === 204) {
M.toast({html: "Auftrag '" + id + "' wurde gelöscht."});
callback();
} else {
M.toast({html: "Auftrag '" + id + "' konnte nicht gelöscht werden.<br>(Fehlercode: " + this.status + ")"});
}
});
oReq.open("DELETE", VREMANAGER + "/vre/jobs/" + id);
oReq.send();
}
/**
* Sends an asynchronous HTTP request to the vre manager in order to get all
* informations about a specific and existing job.
* @param {string} id - The id of the job to get the information from.
* @param {function} [callback] - A function, that is called after the HTTP
* request finished.
*/
function getJob(id, callback = console.log) {
var oReq;
oReq = new XMLHttpRequest();
oReq.addEventListener("loadend", function(oEvent) {
if (this.status === 200) {
callback(JSON.parse(this.responseText));
}
});
oReq.open("GET", VREMANAGER + "/vre/jobs/" + id);
oReq.send();
}
/**
* Sends an asynchronous HTTP request to the vre manager in order to get a
* (filtered) list of all jobs.
* @param {object} [filters=null] - An object containing filters. E.g.
* {'service': 'nlp', 'status': 'finished'}
* @param {function} [callback] - A function, that is called after the HTTP
* request finished.
*/
function getJobs(filters = null, callback = console.log) {
var oReq;
var query;
oReq = new XMLHttpRequest();
oReq.addEventListener("loadend", function(oEvent) {
if (this.status === 200) {
callback(JSON.parse(this.responseText));
}
});
query = ""
if (filters) {
if (filters["name"]) {
query += (query === "") ? "?" : "&";
query += "name=" + filters["name"];
}
if (filters["service"]) {
query += (query === "") ? "?" : "&";
query += "service=" + filters["service"];
}
if (filters["status"]) {
query += (query === "") ? "?" : "&";
query += "status=" + filters["status"];
}
if (filters["user"]) {
query += (query === "") ? "?" : "&";
query += "user=" + filters["user"];
}
}
oReq.open("GET", VREMANAGER + "/vre/jobs" + query);
oReq.send();
}
/**
* Sends an asynchronous HTTP request to the vre manager in order to update the
* values of a specific and existing job.
* @param {string} id - The id of the job to update.
* @param {object} [newValues=null] - An object containing the new values. E.g.
* {'status': 'finished'}
* @param {function} [callback] - A function, that is called after the HTTP
* request finished.
*/
function updateJob(id, newValues = null, callback = console.log) {
var oReq;
var query;
oReq = new XMLHttpRequest();
oReq.addEventListener("loadend", function(oEvent) {
if (this.status === 200) {
M.toast({html: "Auftrag '" + id + "' wurde aktualisiert."});
callback(JSON.parse(this.responseText));
} else {
M.toast({html: "Auftrag '" + id + "' konnte nicht aktualisiert werden.<br>(Fehlercode: " + this.status + ")"});
}
});
query = ""
if (newValues) {
if (newValues["name"]) {
query += (query === "") ? "?" : "&";
query += "name=" + newValues["name"];
}
if (newValues["language"]) {
query += (query === "") ? "?" : "&";
query += "language=" + newValues["language"];
}
if (newValues["report"]) {
query += (query === "") ? "?" : "&";
query += "report=" + newValues["report"];
}
if (newValues["status"]) {
query += (query === "") ? "?" : "&";
query += "status=" + newValues["status"];
}
}
oReq.open("PUT", VREMANAGER + "/vre/jobs/" + id + query);
oReq.send();
}