| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07: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-04-27 11:28:22 -07:00
										 |  |  | import {Headers, RequestOptions} from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | // #enddocregion import-request-options
 | 
					
						
							|  |  |  | // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  | import {Hero}           from './hero'; | 
					
						
							|  |  |  | import {Observable}     from 'rxjs/Observable'; | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							|  |  |  | export class HeroService { | 
					
						
							|  |  |  |   constructor (private http: Http) {} | 
					
						
							| 
									
										
										
										
											2016-02-17 18:13:57 +01:00
										 |  |  | // #enddocregion
 | 
					
						
							|  |  |  | // #enddocregion v1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* | 
					
						
							|  |  |  |   // #docregion endpoint-json
 | 
					
						
							|  |  |  |   private _heroesUrl = 'app/heroes.json'; // URL to JSON file
 | 
					
						
							|  |  |  |   // #enddocregion endpoint-json
 | 
					
						
							|  |  |  |   */ | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #docregion endpoint
 | 
					
						
							| 
									
										
										
										
											2016-02-17 18:13:57 +01: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-01-28 10:22:59 +01:00
										 |  |  |   // #docregion methods
 | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   // #docregion error-handling, http-get
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   getHeroes (): Observable<Hero[]> { | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01: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-04-13 08:21:32 -07:00
										 |  |  |   // #enddocregion error-handling, http-get
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #enddocregion v1
 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   // #docregion addhero
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   addHero (name: string): Observable<Hero>  { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let body = JSON.stringify({ name }); | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     // #docregion headers
 | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  |     let headers = new Headers({ 'Content-Type': 'application/json' }); | 
					
						
							|  |  |  |     let options = new RequestOptions({ headers: headers }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.http.post(this._heroesUrl, body, options) | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     // #enddocregion headers
 | 
					
						
							|  |  |  |                     .map(this.extractData) | 
					
						
							|  |  |  |                     .catch(this.handleError); | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion addhero
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // #docregion v1
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   // #docregion extract-data
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   private extractData(res: Response) { | 
					
						
							|  |  |  |     if (res.status < 200 || res.status >= 300) { | 
					
						
							|  |  |  |       throw new Error('Bad response status: ' + res.status); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     let body = res.json(); | 
					
						
							|  |  |  |     return body.data || { }; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   // #enddocregion extract-data
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #docregion error-handling
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |   private handleError (error: any) { | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |     // In a real world app, we might send the error to remote logging infrastructure
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     let errMsg = error.message || 'Server error'; | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |     console.error(errMsg); // log to console instead
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     return Observable.throw(errMsg); | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion error-handling
 | 
					
						
							|  |  |  |   // #enddocregion methods
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 |