import { Injectable } from '@angular/core' ; import { Headers , Http , RequestOptions , Response } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/observable/throw' ; import 'rxjs/add/operator/timeout' ; @ Injectable ( ) export class HttpService { private environmentUrl = '' ; constructor ( private http ) { } public post ( requestUrl , requestJson ) { return this . httpRequestPost ( requestUrl , requestJson ) . map ( response => response . json ( ) ) . catch ( this . handlerError ) ; } public get ( param ) { const header = new Headers ( { 'Content-type' : 'application/json' } ) ; const options = new RequestOptions ( { headers : header } ) ; return this . http . get ( this . environmentUrl + param , options ) . timeout ( 10000 ) . map ( response => response . json ( ) ) . catch ( this . handlerError ) ; } private httpRequestPost ( requestUrl , requestJson ) { const body = JSON . stringify ( requestJson ) ; const header = new Headers ( { 'Content-type' : 'application/json' } ) ; const options = new RequestOptions ( { headers : header } ) ; return this . http . post ( this . environmentUrl + requestUrl , body , options ) . timeout ( 10000 ) ; } private handlerError ( error ) { const body = error . json ( ) || { } ; return Observable . throw ( body ) ; } }