2019-10-11 18:16:14 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { Observable, of } from 'rxjs';
|
2019-10-16 11:02:59 +02:00
|
|
|
import { catchError } from 'rxjs/operators';
|
2019-10-16 11:50:26 +02:00
|
|
|
import { TypeDefinition } from './is-model/types/TypeDefinition';
|
2019-10-11 18:16:14 +02:00
|
|
|
import { types } from './types';
|
2019-10-16 11:02:59 +02:00
|
|
|
import { list } from './list';
|
2019-10-16 15:25:51 +02:00
|
|
|
import { Facet } from './is-model/reference/entities/Facet';
|
|
|
|
import { Resource } from './is-model/reference/entities/Resource';
|
|
|
|
import { resources } from './resources';
|
2019-10-11 18:16:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class IsService {
|
|
|
|
|
|
|
|
constructor(private httpClient: HttpClient) { }
|
|
|
|
|
2019-10-16 11:02:59 +02:00
|
|
|
private baseURL = 'http://pc-frosini.isti.cnr.it:8080/resource-registry';
|
|
|
|
private typesURL = this.baseURL + '/types';
|
|
|
|
private instancesURL = this.baseURL + '/instances';
|
|
|
|
private queryURL = this.baseURL + '/query';
|
|
|
|
|
|
|
|
|
2019-10-11 18:16:14 +02:00
|
|
|
/* NextNext Token */
|
|
|
|
private token = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Http operation that failed.
|
|
|
|
* Let the app continue.
|
|
|
|
* @param operation - name of the operation that failed
|
|
|
|
* @param result - optional value to return as the observable result
|
|
|
|
*/
|
|
|
|
private handleError<T>(operation = 'operation', result?: T) {
|
|
|
|
return (error: any): Observable<T> => {
|
2019-10-16 11:02:59 +02:00
|
|
|
console.error(`${operation} failed: ${error.message}`);
|
|
|
|
if (result) {
|
|
|
|
console.warn(`${operation} is going to provide hard-coded data which is better than nothing.`);
|
|
|
|
}
|
2019-10-11 18:16:14 +02:00
|
|
|
return of(result as T);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:50:26 +02:00
|
|
|
public getResourceTypes(callback: (isTypes: TypeDefinition[]) => void): void {
|
2019-10-16 11:02:59 +02:00
|
|
|
const url = this.typesURL + '/Resource?polymorphic=true&gcube-token=' + this.token;
|
2019-10-11 18:16:14 +02:00
|
|
|
// const observable: Observable<ISType[]> = of(types);
|
2019-10-16 11:50:26 +02:00
|
|
|
const observable: Observable<TypeDefinition[]> = this.httpClient.get<TypeDefinition[]>(url);
|
2019-10-11 18:16:14 +02:00
|
|
|
observable.pipe(
|
2019-10-16 11:50:26 +02:00
|
|
|
catchError(this.handleError<TypeDefinition[]>('getResourceTypes()', types))
|
2019-10-16 11:02:59 +02:00
|
|
|
).subscribe(data => callback(data));
|
|
|
|
}
|
|
|
|
|
2019-10-16 15:25:51 +02:00
|
|
|
public getIdentifyingFacets(resourceType: string, callback: (instances: Facet[]) => void): void {
|
2019-10-16 11:02:59 +02:00
|
|
|
const url = this.queryURL + resourceType + '?polymorphic=true&gcube-token=' + this.token;
|
|
|
|
// const observable: Observable<ISType[]> = of(types);
|
|
|
|
const observable: Observable<any[]> = this.httpClient.get<any[]>(url);
|
|
|
|
observable.pipe(
|
2019-10-16 15:25:51 +02:00
|
|
|
catchError(this.handleError<any[]>('getIdentifyingFacets()', list))
|
|
|
|
).subscribe(data => callback(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getResourceInstances(resourceType: string, callback: (instances: Resource[]) => void): void {
|
|
|
|
const url = this.queryURL + resourceType + '?polymorphic=true&gcube-token=' + this.token;
|
|
|
|
// const observable: Observable<ISType[]> = of(types);
|
|
|
|
const observable: Observable<any[]> = this.httpClient.get<any[]>(url);
|
|
|
|
observable.pipe(
|
|
|
|
catchError(this.handleError<any[]>('getResourceInstances()', resources))
|
2019-10-16 11:02:59 +02:00
|
|
|
).subscribe(data => callback(data));
|
2019-10-11 18:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|