Skip to content
Snippets Groups Projects
Commit dba7c723 authored by Neuber, Eugen Ramon's avatar Neuber, Eugen Ramon :speech_balloon: Committed by Reiter, Christoph
Browse files
parent 2126fd02
No related branches found
No related tags found
No related merge requests found
...@@ -61,63 +61,65 @@ export default class JSONLD { ...@@ -61,63 +61,65 @@ 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 && xhr.status === 200) { if (xhr.readyState === 4) {
const json = JSON.parse(xhr.responseText); if (xhr.status === 200) {
const json = JSON.parse(xhr.responseText);
let entryPoints = {};
for (let property in json) { let entryPoints = {};
// for some reason the properties start with a lower case character for (let property in json) {
if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property]; // for some reason the properties start with a lower case character
} if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property];
}
// read the link header of the api response
// const utils = require("./utils"); // read the link header of the api response
const links = utils.parseLinkHeader(this.getResponseHeader("link")); // const utils = require("./utils");
const links = utils.parseLinkHeader(this.getResponseHeader("link"));
// get the hydra apiDocumentation url
const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"]; // get the hydra apiDocumentation url
const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"];
if (apiDocUrl !== undefined) {
// load the hydra apiDocumentation if (apiDocUrl !== undefined) {
const docXhr = new XMLHttpRequest(); // load the hydra apiDocumentation
docXhr.open("GET", apiDocUrl, true); const docXhr = new XMLHttpRequest();
docXhr.setRequestHeader("Content-Type", "application/json"); docXhr.open("GET", apiDocUrl, true);
docXhr.onreadystatechange = function () { docXhr.setRequestHeader("Content-Type", "application/json");
if (docXhr.readyState === 4 && docXhr.status === 200) { docXhr.onreadystatechange = function () {
const json = JSON.parse(docXhr.responseText); if (docXhr.readyState === 4 && docXhr.status === 200) {
const supportedClasses = json["hydra:supportedClass"]; const json = JSON.parse(docXhr.responseText);
const supportedClasses = json["hydra:supportedClass"];
let entities = {};
const baseUrl = utils.parseBaseUrl(apiUrl); let entities = {};
const baseUrl = utils.parseBaseUrl(apiUrl);
// gather the entities
supportedClasses.forEach(function (classData) { // gather the entities
// add entry point url supportedClasses.forEach(function (classData) {
const entityName = classData["hydra:title"]; // add entry point url
let entryPoint = entryPoints[entityName.toLowerCase()]; const entityName = classData["hydra:title"];
if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint; let entryPoint = entryPoints[entityName.toLowerCase()];
classData["@entryPoint"] = entryPoint; if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint;
classData["@entryPoint"] = entryPoint;
entities[entityName] = classData;
}); entities[entityName] = classData;
});
const instance = new JSONLD(baseUrl, entities);
instances[apiUrl] = instance; 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); // return the initialized JSONLD object
successFunctions[apiUrl] = []; for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance);
} else { successFunctions[apiUrl] = [];
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.api-documentation-server', { apiUrl: apiDocUrl })); } else {
} JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.api-documentation-server', {apiUrl: apiDocUrl}));
}; }
};
docXhr.send();
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-hydra-documentation-url-not-set', { apiUrl: apiUrl })); JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-api-server', {apiUrl: apiUrl}));
} }
} else {
JSONLD.executeFailureFunctions(apiUrl, i18n.t('jsonld.error-api-server', { apiUrl: apiUrl }));
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment