import { HttpClient , HttpErrorResponse , HttpHeaders , HttpParams } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { GlobalService } from '@shared/services/global.service' ; import { ErrorService } from '@shared/services/error-service' ; export class RestApi { protected API_URL ; constructor ( protected globalService , protected httpClient , protected errorService , apiUrl ? ) { this . API_URL = apiUrl || `http://${ globalService . getDaemonIP ( ) }:${ globalService . getApiPort ( ) }/api` ; } protected getHttpOptions ( accept , contentType , httpParams ? ) { return { headers : new HttpHeaders ( { 'Accept' : accept , 'Content-Type' : contentType , } ) , params : httpParams } ; } public get < TResult > ( path , params ? , accept = 'application/json' , contentType = 'application/json' ) { const options = this . getHttpOptions ( accept , contentType , params ) ; return this . httpClient . get ( `${ this . API_URL }/${ path }` , options ) as Observable < TResult > ; } public delete < TResult > ( path , params ? , accept = 'application/json' , contentType = 'application/json' ) { const options = this . getHttpOptions ( accept , contentType , params ) ; return this . httpClient . delete ( `${ this . API_URL }/${ path }` , options ) as Observable < TResult > ; } public post < TResult > ( path , body , contentType ? ) { return this . executeHttp < TResult > ( 'post' , path , body , contentType ) ; } public put < TResult > ( path , body , contentType ? ) { return this . executeHttp < TResult > ( 'put' , path , body , contentType ) ; } protected executeHttp < TResult > ( method , path , body , contentType ? , params ? ) { return this . httpClient [ method ] ( `${ this . API_URL }/${ path }` , body , this . getHttpOptions ( 'application/json' , contentType || 'application/json' , params ) ) as Observable < TResult > ; } protected handleHttpError ( error , silent ? ) { return this . errorService . handleHttpError ( error , silent ) ; } }