sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
61 lines • 2.01 kB
JavaScript
import LocalCacheData from "../../datastorage/LocalCacheData";
/**
* Fetches a URL
*/
export class UrlFetcher {
// private constructor
constructor(localCacheDataTtl, extraHeaders) {
this.localCacheDataTtl = localCacheDataTtl;
this.extraHeaders = extraHeaders;
}
fetchUrl(url, callback, errorCallback) {
return this.fetchUrlWithParameters(url,
// call with no provided parameters
{}, callback, errorCallback);
}
fetchUrlWithParameters(url, providedParameters, callback, errorCallback) {
var headers = new Headers();
if (this.extraHeaders) {
// honor extra headers
for (const k in this.extraHeaders) {
headers.append(k, this.extraHeaders[k]);
}
}
headers.append("Accept", "application/sparql-results+json, application/json, */*;q=0.01");
let defaultParameters = {
method: "GET",
headers: headers,
mode: "cors",
cache: "default"
};
let parameters = {
...defaultParameters,
...providedParameters
};
let temp = new LocalCacheData();
try {
let fetchpromise = temp.fetch(url, parameters, this.localCacheDataTtl);
fetchpromise
.then((response) => {
if (!response.ok) {
if (errorCallback)
errorCallback(response);
}
return response.json();
})
.catch((error) => {
if (errorCallback)
errorCallback("There was a problem calling " + url);
})
.then((data) => {
if (data)
callback(data);
});
}
catch (error) {
if (errorCallback)
errorCallback("There was a problem calling " + url);
}
}
}
//# sourceMappingURL=UrlFetcher.js.map