| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-05-17 21:25:41 -07:00
										 |  |  | // Observable Version
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { Injectable }     from '@angular/core'; | 
					
						
							|  |  |  | import { Http, Response } from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | // #enddocregion v1
 | 
					
						
							|  |  |  | // #docregion import-request-options
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | import { Headers, RequestOptions } from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | // #enddocregion import-request-options
 | 
					
						
							|  |  |  | // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { Hero }           from './hero'; | 
					
						
							|  |  |  | import { Observable }     from 'rxjs/Observable'; | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							|  |  |  | export class HeroService { | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #docregion endpoint
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   private heroesUrl = 'app/heroes';  // URL to web API
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #enddocregion endpoint
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-25 15:24:13 -07:00
										 |  |  |   // #docregion ctor
 | 
					
						
							|  |  |  |   constructor (private http: Http) {} | 
					
						
							|  |  |  |   // #enddocregion ctor
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #docregion methods, error-handling, http-get
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   getHeroes (): Observable<Hero[]> { | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |     return this.http.get(this.heroesUrl) | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |                     .map(this.extractData) | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |                     .catch(this.handleError); | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #enddocregion error-handling, http-get, v1
 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #docregion addhero, addhero-sig
 | 
					
						
							| 
									
										
										
										
											2016-05-17 01:45:52 -07:00
										 |  |  |   addHero (name: string): Observable<Hero> { | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #enddocregion addhero-sig
 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  |     let headers = new Headers({ 'Content-Type': 'application/json' }); | 
					
						
							|  |  |  |     let options = new RequestOptions({ headers: headers }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 23:17:50 -07:00
										 |  |  |     return this.http.post(this.heroesUrl, { name }, options) | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |                     .map(this.extractData) | 
					
						
							|  |  |  |                     .catch(this.handleError); | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion addhero
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #docregion v1, extract-data
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   private extractData(res: Response) { | 
					
						
							|  |  |  |     let body = res.json(); | 
					
						
							|  |  |  |     return body.data || { }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   // #enddocregion extract-data
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #docregion error-handling
 | 
					
						
							| 
									
										
										
										
											2016-10-19 23:17:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private handleError (error: Response | any) { | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |     // In a real world app, we might use a remote logging infrastructure
 | 
					
						
							| 
									
										
										
										
											2016-10-19 23:17:50 -07:00
										 |  |  |     let errMsg: string; | 
					
						
							|  |  |  |     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); | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     return Observable.throw(errMsg); | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  |   // #enddocregion error-handling, methods
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 | 
					
						
							| 
									
										
										
										
											2016-05-13 13:32:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |   // #docregion endpoint-json
 | 
					
						
							|  |  |  |   private heroesUrl = 'app/heroes.json'; // URL to JSON file
 | 
					
						
							|  |  |  |   // #enddocregion endpoint-json
 | 
					
						
							|  |  |  | */ |