Skip to content
Snippets Groups Projects
Commit 5826a7a3 authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire: Committed by Reiter, Christoph
Browse files

Do JSONLD code refactoring

parent 7830e3ab
No related branches found
No related tags found
No related merge requests found
...@@ -61,73 +61,88 @@ export default class JSONLD { ...@@ -61,73 +61,88 @@ export default class JSONLD {
xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken); xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { if (xhr.readyState !== 4) {
if (xhr.status === 200) { return;
const json = JSON.parse(xhr.responseText); }
let entryPoints = {}; if (xhr.status === 200) {
for (let property in json) { const json = JSON.parse(xhr.responseText);
// for some reason the properties start with a lower case character
if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property]; let entryPoints = {};
} for (let property in json) {
// for some reason the properties start with a lower case character
// read the link header of the api response if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property];
// const utils = require("./utils"); }
const links = utils.parseLinkHeader(this.getResponseHeader("link"));
// read the link header of the api response
// get the hydra apiDocumentation url // const utils = require("./utils");
const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"]; const links = utils.parseLinkHeader(this.getResponseHeader("link"));
if (apiDocUrl !== undefined) { // get the hydra apiDocumentation url
// load the hydra apiDocumentation const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"];
const docXhr = new XMLHttpRequest();
docXhr.open("GET", apiDocUrl, true); if (apiDocUrl !== undefined) {
docXhr.setRequestHeader("Content-Type", "application/json"); // load the hydra apiDocumentation
docXhr.onreadystatechange = function () { const docXhr = new XMLHttpRequest();
if (docXhr.readyState === 4) { docXhr.open("GET", apiDocUrl, true);
if (docXhr.status === 200) { docXhr.setRequestHeader("Content-Type", "application/json");
const json = JSON.parse(docXhr.responseText); docXhr.onreadystatechange = function () {
const supportedClasses = json["hydra:supportedClass"]; if (docXhr.readyState !== 4) {
return;
let entities = {}; }
const baseUrl = utils.parseBaseUrl(apiUrl);
if (docXhr.status === 200) {
// gather the entities JSONLD.gatherEntities(docXhr, apiUrl, entryPoints);
supportedClasses.forEach(function (classData) { } else {
// add entry point url JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.api-documentation-server', {apiUrl: apiDocUrl}));
const entityName = classData["hydra:title"]; }
let entryPoint = entryPoints[entityName.toLowerCase()]; };
if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint;
classData["@entryPoint"] = entryPoint; docXhr.send();
entities[entityName] = classData;
});
const instance = new JSONLD(baseUrl, entities);
instances[apiUrl] = instance;
// return the initialized JSONLD object
for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance);
successFunctions[apiUrl] = [];
} else {
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.api-documentation-server', {apiUrl: apiDocUrl}));
}
}
};
docXhr.send();
} else {
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-hydra-documentation-url-not-set', {apiUrl: apiUrl}));
}
} else { } else {
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-api-server', {apiUrl: apiUrl})); JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-hydra-documentation-url-not-set', {apiUrl: apiUrl}));
} }
} else {
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-api-server', {apiUrl: apiUrl}));
} }
}; };
xhr.send(); xhr.send();
} }
/**
* Gather the entities
*
* @param docXhr
* @param apiUrl
* @param entryPoints
*/
static gatherEntities(docXhr, apiUrl, entryPoints) {
const json = JSON.parse(docXhr.responseText);
const supportedClasses = json["hydra:supportedClass"];
let entities = {};
const baseUrl = utils.parseBaseUrl(apiUrl);
// gather the entities
supportedClasses.forEach(function (classData) {
// add entry point url
const entityName = classData["hydra:title"];
let entryPoint = entryPoints[entityName.toLowerCase()];
if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint;
classData["@entryPoint"] = entryPoint;
entities[entityName] = classData;
});
const instance = new JSONLD(baseUrl, entities);
instances[apiUrl] = instance;
// return the initialized JSONLD object
for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance);
successFunctions[apiUrl] = [];
}
/** /**
* Execute failure functions and send general notification * Execute failure functions and send general notification
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment