import { Injectable } from '@angular/core' ; import { Http , Response } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/operator/map' ; @ Injectable ( ) export class Rest { private apiUrl = 'https://restcountries.eu/rest/v2/all' ; constructor ( public http ) { } getCountries ( ) { return this . http . get ( this . apiUrl ) . map ( this . extractData ) . catch ( this . handleError ) ; } private extractData ( res ) { let body = res . json ( ) ; return body || { } ; } private handleError ( error ) { let errMsg ; if ( error instanceof Response ) { const body = error . json ( ) || '' ; const err = body . error || JSON . stringify ( body ) ; errMsg = `${ error . status } - ${ error . statusText || '' } ${ err }` ; } else { errMsg = error . message ? error . message : error . toString ( ) ; } console . error ( errMsg ) ; return Observable . throw ( errMsg ) ; } }