import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/Rx' ; import { Http , RequestOptions , RequestOptionsArgs , Response , Request , Headers , XHRBackend } from '@angular/http' ; import { AngularReduxRequestOptions } from './angular-redux-request.options' ; import { LoaderService } from './loader/loader.service' ; @ Injectable ( ) export class HttpService extends Http { apiUrl = 'https://dingo-api.codingo.me/' ; constructor ( backend , defaultOptions , private loaderService ) { super ( backend , defaultOptions ) ; } get ( url , options ? ) { this . showLoader ( ) ; return super . get ( this . getFullUrl ( url ) , this . requestOptions ( options ) ) . catch ( this . onCatch ) . do ( ( res ) => { this . onSuccess ( res ) ; } , ( error ) => { this . onError ( error ) ; } ) . finally ( ( ) => { this . onEnd ( ) ; } ) ; } private requestOptions ( options ? ) { if ( options == null ) { options = new AngularReduxRequestOptions ( ) ; } if ( options . headers == null ) { options . headers = new Headers ( ) ; } return options ; } private getFullUrl ( url ) { return this . apiUrl + url ; } private onCatch ( error , caught ) { return Observable . throw ( error ) ; } private onSuccess ( res ) { console . log ( 'Request successful' ) ; } private onError ( res ) { console . log ( 'Error, status code: ' + res . status ) ; } private onEnd ( ) { this . hideLoader ( ) ; } private showLoader ( ) { this . loaderService . show ( ) ; } private hideLoader ( ) { this . loaderService . hide ( ) ; } }