import { Injectable } from '@angular/core'; import { Http, RequestOptions, Response, Headers } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import { Router, ActivatedRoute, RouterStateSnapshot } from '@angular/router'; import { MatSnackBar } from '@angular/material'; import { TranslateService } from '@ngx-translate/core'; import { HttpClient } from '@angular/common/http'; import { AuthService } from '../../services/auth/auth.service'; import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component'; import { BaseHttpResponseModel } from '../../models/http/BaseHttpResponseModel'; @Injectable() export class BaseHttpService { constructor( protected http: HttpClient, private router: Router, private authService: AuthService, public language: TranslateService, public snackBar: MatSnackBar, public route: ActivatedRoute ) { } get(url: string, options?: any): Observable { return this.interceptRepsonse(this.http.get(url, this.buildRequestOptions(options))); } post(url: string, body: any, options?: any): Observable { return this.interceptRepsonse(this.http.post(url, body, this.buildRequestOptions(options))); } put(url: string, body: any, options?: any): Observable { return this.interceptRepsonse(this.http.put(url, body, this.buildRequestOptions(options))); } delete(url: string, options?: any): Observable { return this.interceptRepsonse(this.http.delete(url, this.buildRequestOptions(options))); } patch(url: string, body: any, options?: any): Observable { return this.interceptRepsonse(this.http.patch(url, body, this.buildRequestOptions(options))); } head(url: string, options?: any): Observable { return this.interceptRepsonse(this.http.head(url, this.buildRequestOptions(options))); } options(url: string, options?: any): Observable { return this.interceptRepsonse(this.http.options(url, this.buildRequestOptions(options))); } protected buildRequestOptions(options?: any): Object { if (options == null) { options = new RequestOptions(); } if (options.headers == null) { options.headers = new Headers(); } if (!options.headers.has('Content-Type')) { options.headers = options.headers.set('Content-Type', 'application/json'); } if (!options.headers.has('Content-Type')) { options.headers = options.headers.set('Content-Type', 'application/json'); } if (!options.headers.has('AuthToken')) { const principal = this.authService.current(); if (principal) { options.headers = options.headers.set('AuthToken', principal.token); } } return options; } private interceptRepsonse(observable: Observable): Observable { return observable .catch((error) => { if (error.status === 401) { this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGOUT', language: this.language }, duration: 3000, extraClasses: ['snackbar-success'] }) let currentPage = this.router.url; this.router.navigate(['/unauthorized'], { queryParams: { returnUrl: currentPage } }); //this.notification.httpError(error); return Observable.of(); } else { return Observable.throw(error); } }) .map(response => { if (response.statusCode < 200 || response.statusCode >= 300) { //throw new Error('Request failed'); this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: 'GENERAL.ERRORS.HTTP-REQUEST-ERROR', language: this.language }, duration: 3000, extraClasses: ['snackbar-warning'] }) } else { return response.payload; } }); } // public handleResponse(response: BaseHttpResponseModel) { // if (response.statusCode < 200 || response.statusCode >= 300) { // //throw new Error('Request failed'); // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: 'GENERAL.ERRORS.HTTP-REQUEST-ERROR', language: this.language }, // duration: 3000, // extraClasses: ['snackbar-warning'] // }) // } // else { // return response.payload; // } // } }