@dasch-swiss/dsp-js
Version:
TypeScript client library for DSP-API
179 lines • 8.45 kB
JavaScript
import { catchError, map, mergeMap } from 'rxjs';
import { ListNodeV2Cache } from '../../../cache/ListNodeV2Cache';
import { OntologyCache } from '../../../cache/ontology-cache/OntologyCache';
import { CanDoResponse } from '../../../models/v2/ontologies/read/can-do-response';
import { ResourcesConversionUtil } from '../../../models/v2/resources/ResourcesConversionUtil';
import { DeleteResourceResponse } from '../../../models/v2/resources/delete/delete-resource-response';
import { UpdateResourceMetadataResponse } from '../../../models/v2/resources/update/update-resource-metadata-response';
import { Endpoint } from '../../endpoint';
const jsonld = require('jsonld/dist/jsonld.js');
/**
* Handles requests to the resources route of the Knora API.
*
* @category Endpoint V2
*/
export class ResourcesEndpointV2 extends Endpoint {
/**
* @category Internal
* @param knoraApiConfig the config object.
* @param path this endpoint's base path.
* @param v2Endpoint a reference to the v2 endpoint.
*/
constructor(knoraApiConfig, path, v2Endpoint) {
super(knoraApiConfig, path);
this.knoraApiConfig = knoraApiConfig;
this.path = path;
this.v2Endpoint = v2Endpoint;
}
/**
* Given a sequence of resource IRIs, gets the resources from Knora.
*
* @param resourceIris Iris of the resources to get.
* @param version Timestamp of the resource version.
*/
getResources(resourceIris, version) {
// TODO: Do not hard-code the URL and http call params, generate this from Knora
// make URL containing resource Iris as segments
const resIris = resourceIris
.map((resIri) => {
return `/${encodeURIComponent(resIri)}${version ? `?version=${version}` : ''}`;
})
.reduce((acc, currentValue) => {
return acc + currentValue;
});
// Create temporary caches for this request only
// These will be garbage collected after the request completes
const tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint);
const tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint);
return this.httpGet(resIris).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), mergeMap((jsonldobj) => {
// console.log(JSON.stringify(jsonldobj));
return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, this.jsonConvert);
}), catchError(error => {
return this.handleError(error);
}));
}
/**
* Given a resource IRI, gets the resource from Knora.
*
* @param resourceIri Iri of the resource to get.
* @param version Timestamp of the resource version.
*/
getResource(resourceIri, version) {
return this.getResources([resourceIri], version).pipe(map((resources) => resources.resources[0]), catchError(error => {
return this.handleError(error);
}));
}
/**
* Creates a new resource.
*
* @param resource the resource to be created.
*/
createResource(resource) {
const res = this.jsonConvert.serializeObject(resource);
// get property Iris
const propIris = Object.keys(resource.properties);
// for each property, serialize its values
// and assign them to the resource
propIris.forEach(propIri => {
// check that array contains least one value
if (resource.properties[propIri].length === 0) {
throw new Error(`No values defined for ${propIri}`);
}
// if array contains only one element, serialize as an object
if (resource.properties[propIri].length === 1) {
res[propIri] = this.jsonConvert.serializeObject(resource.properties[propIri][0]);
}
else {
res[propIri] = this.jsonConvert.serializeArray(resource.properties[propIri]);
}
});
// Create temporary caches for this request only
// These will be garbage collected after the request completes
const tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint);
const tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint);
return this.httpPost('', res, 'json', { 'X-Asset-Ingested': 'true' }).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), mergeMap((jsonldobj) => {
// console.log(JSON.stringify(jsonldobj));
return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, this.jsonConvert);
}), map((resources) => resources.resources[0]), catchError(error => {
return this.handleError(error);
}));
}
/**
* Updates a resource's metadata.
*
* @param resourceMetadata the new metadata.
*/
updateResourceMetadata(resourceMetadata) {
// check that at least one of the following properties is updated: label, hasPermissions, newModificationDate
if (resourceMetadata.label === undefined &&
resourceMetadata.hasPermissions === undefined &&
resourceMetadata.newModificationDate === undefined) {
throw new Error('At least one of the following properties has to be updated: label, hasPermissions, newModificationDate');
}
const res = this.jsonConvert.serializeObject(resourceMetadata);
return this.httpPut('', res).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, UpdateResourceMetadataResponse);
}), catchError(error => this.handleError(error)));
}
/**
* Checks whether a resource can be deleted or not.
*
* @param resource the resource to check.
*/
canDeleteResource(resource) {
const res = JSON.stringify(this.jsonConvert.serializeObject(resource));
return this.httpGet(`/candelete?jsonLd=${encodeURIComponent(res)}`).pipe(mergeMap(ajaxResponse => {
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, CanDoResponse);
}), catchError(error => this.handleError(error)));
}
/**
* Deletes a resource.
*
* @param resource the resource to be deleted.
*/
deleteResource(resource) {
const res = this.jsonConvert.serializeObject(resource);
return this.httpPost('/delete', res).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, DeleteResourceResponse);
}), catchError(error => this.handleError(error)));
}
/**
* Erases a resource.
*
* @param resource the resource to be deleted.
*/
eraseResource(resource) {
const res = this.jsonConvert.serializeObject(resource);
return this.httpPost('/erase', res).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, DeleteResourceResponse);
}), catchError(error => this.handleError(error)));
}
}
//# sourceMappingURL=resources-endpoint-v2.js.map