Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F91905049
ontology-query.service.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Nov 15, 15:12
Size
2 KB
Mime Type
text/x-java
Expires
Sun, Nov 17, 15:12 (2 d)
Engine
blob
Format
Raw Data
Handle
22344691
Attached To
rNIETZSCHEBETAAPP Nietzsche-Beta-App
ontology-query.service.ts
View Options
import { Injectable, EventEmitter } from '@angular/core';
import {HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { EMPTY, Observable, throwError } from 'rxjs';
import { catchError, shareReplay } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class OntologyQueryService {
baseUrl: string;
cache = {};
constructor(private http: HttpClient) { }
/**
* Gets the data from an endpoint via http post
*
* @param query: The query to run.
* @param this.baseUrl: The URL of the endpoint
* @returns response
*/
public getData(query: string): Observable<any> {
let httpOptions = {
headers: new HttpHeaders(
{ 'Content-Type': 'application/sparql-query',
'Accept': 'application/sparql-results+json; charset=UTF-8'}
)
};
return this.http.post(this.baseUrl, query, httpOptions);
}
public getOldData(query: string): Observable<any> {
let httpOptions = {
headers: new HttpHeaders(
{ 'Content-Type': 'application/sparql-query',
'Accept': 'application/sparql-results+json; charset=UTF-8'}
)
};
if (this.cache[query]){
console.log('Returning cached value!')
return this.cache[this.baseUrl + query];
}
this.cache[query] = this.http.post(this.baseUrl, query, httpOptions).pipe(
shareReplay(1),
catchError(error =>{
delete this.cache[this.baseUrl + query];
this.handleError(error);
return EMPTY
})
);
return this.cache[this.baseUrl + query]
}
private handleError(error: HttpErrorResponse) {
switch (error.status){
case 0:
// A client-side or network error occurred. Handle it accordingly.
console.error('The backend data server is offline:', error.error);
break;
default:
console.error( `Backend returned code ${error.status}, ` + `body was: ${error.error}`);
}
return throwError( 'Something bad happened; please try again later.');
}
}
Event Timeline
Log In to Comment