Page MenuHomec4science

tln-query.service.ts
No OneTemporary

File Metadata

Created
Mon, Sep 9, 20:38

tln-query.service.ts

import { Injectable, EventEmitter, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { TlnQueryServiceInterface } from './models';
/**
* This is the internal query service
* that communicates with the SPARQL-endpoint.
* */
@Injectable({ providedIn: 'root'})
export class TlnQueryService implements TlnQueryServiceInterface {
//baseUrl = 'http://localhost:3030/nietzsche/query';
baseUrl = 'https://nietzsche.fuseki.services.dasch.swiss/nietzsche';
reset_data = new EventEmitter<string>();
constructor(private http: HttpClient) {
}
public resetData(key: string){
this.reset_data.emit(key);
}
/**
* Gets the data from an endpoint via http post
*
* @param query: The query to run.
* @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).pipe(catchError(this.handleError));
}
private handleError(error: HttpErrorResponse) {
if (error.status === 0) {
// A client-side or network error occurred. Handle it accordingly.
console.error('The backend data server is offline:', error.error);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong.
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
return throwError( 'Something bad happened; please try again later.');
}
}

Event Timeline