import { Injectable } from '@angular/core' ; import { ConfigService } from './config.service' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { Settings } from '../classes/settings' ; import { Observable , of } from 'rxjs' ; import { catchError , tap } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class SettingsService { getSettingsUrl = this . configService . baseUrl + '/settings' + '/config' ; applySettingsUrl = this . configService . baseUrl + '/settings' + '/apply' ; private formHttpOptions = { headers : new HttpHeaders ( { 'Content-type' : 'application/json; charset=UTF-8' , 'Access-Control-Allow-Origin' : this . configService . addr , } ) } ; constructor ( private httpClient , private configService ) { } getSettings ( ) { return this . httpClient . get < Settings > ( this . getSettingsUrl ) . pipe ( tap ( _ => console . log ( 'get settings' ) ) , catchError ( this . handleError < Settings > ( 'get settings' ) ) ) ; } applySettings ( newSettings ) { return this . httpClient . post < boolean > ( this . applySettingsUrl , newSettings , this . formHttpOptions ) . pipe ( tap ( _ => console . log ( 'apply settings' ) ) , catchError ( this . handleError < boolean > ( 'apply settings' ) ) ) ; } param operation param result private handleError < T > ( operation = 'operation' , result ? ) { return ( error ) : Observable < T > => { console . error ( error ) ; return of ( result as T ) ; } ; } }