|
1 | 1 | // #docplaster
|
2 |
| - |
3 | 2 | // #docregion
|
| 3 | +// #docregion v1 |
4 | 4 | import 'dart:async';
|
5 | 5 | import 'dart:convert';
|
6 |
| - |
| 6 | +import 'hero.dart'; |
7 | 7 | import 'package:angular2/core.dart';
|
8 |
| -// #enddocregion v1 |
9 |
| -// #docregion import-request-options |
10 | 8 | import 'package:http/browser_client.dart';
|
11 |
| -// #enddocregion import-request-options |
12 |
| -// #docregion v1 |
13 |
| -import 'hero.dart'; |
| 9 | +import 'package:http/http.dart' show Response; |
14 | 10 |
|
15 | 11 | @Injectable()
|
16 | 12 | class HeroService {
|
17 |
| - final String _heroesUrl = 'app/heroes'; |
18 |
| - BrowserClient _http; |
| 13 | + // #docregion endpoint, http-get |
| 14 | + final String _heroesUrl = 'app/heroes'; // URL to web API |
| 15 | + // #enddocregion endpoint, http-get |
| 16 | + final BrowserClient _http; |
19 | 17 |
|
20 | 18 | HeroService(this._http);
|
21 | 19 |
|
22 |
| -// #docregion methods |
| 20 | + // #docregion methods, error-handling, http-get |
23 | 21 | Future<List<Hero>> getHeroes() async {
|
24 |
| - final response = await _http.get(_heroesUrl); |
25 |
| - final heroes = JSON |
26 |
| - .decode(response.body)['data'] |
27 |
| - .map((value) => new Hero.fromJson(value)) |
28 |
| - .toList(); |
29 |
| - print(JSON.encode(heroes)); // eyeball results in the console |
30 |
| - return heroes; |
| 22 | + try { |
| 23 | + final response = await _http.get(_heroesUrl); |
| 24 | + final heroes = _extractData(response) |
| 25 | + .map((value) => new Hero.fromJson(value)) |
| 26 | + .toList(); |
| 27 | + return heroes; |
| 28 | + } catch (e) { |
| 29 | + throw _handleError(e); |
| 30 | + } |
31 | 31 | }
|
| 32 | + // #enddocregion error-handling, http-get, v1 |
32 | 33 |
|
| 34 | + // #docregion addhero, addhero-sig |
33 | 35 | Future<Hero> addHero(String name) async {
|
34 |
| - final headers = {'content-type': 'application/json'}; |
35 |
| - final body = JSON.encode({'name': name}); |
36 |
| - final response = await _http.post(_heroesUrl, headers: headers, body: body); |
37 |
| - return new Hero.fromJson(JSON.decode(response.body)); |
| 36 | + // #enddocregion addhero-sig |
| 37 | + try { |
| 38 | + final response = await _http.post(_heroesUrl, |
| 39 | + headers: {'Content-Type': 'application/json'}, |
| 40 | + body: JSON.encode({'name': name})); |
| 41 | + return new Hero.fromJson(_extractData(response)); |
| 42 | + } catch (e) { |
| 43 | + throw _handleError(e); |
| 44 | + } |
38 | 45 | }
|
39 |
| -// #enddocregion methods |
| 46 | + // #enddocregion addhero, v1 |
| 47 | + |
| 48 | + // #docregion extract-data |
| 49 | + dynamic _extractData(Response res) { |
| 50 | + if (res.statusCode < 200 || res.statusCode >= 300) { |
| 51 | + throw new Exception('Response status: ${res.statusCode}'); |
| 52 | + } |
| 53 | + var body = JSON.decode(res.body); |
| 54 | + // TODO: once fixed, https://github.com/adaojunior/http-in-memory-web-api/issues/1 |
| 55 | + // Drop the `?? body` term |
| 56 | + return body['data'] ?? body; |
| 57 | + } |
| 58 | + // #enddocregion extract-data |
| 59 | + // #docregion error-handling |
| 60 | + |
| 61 | + Exception _handleError(dynamic e) { |
| 62 | + // In a real world app, we might use a remote logging infrastructure |
| 63 | + print(e); // log to console instead |
| 64 | + return new Exception('Server error; cause: $e'); |
| 65 | + } |
| 66 | + // #enddocregion error-handling, methods |
40 | 67 | }
|
41 | 68 | // #enddocregion
|
| 69 | + |
| 70 | +/* |
| 71 | + // #docregion endpoint-json |
| 72 | + private _heroesUrl = 'heroes.json'; // URL to JSON file |
| 73 | + // #enddocregion endpoint-json |
| 74 | +*/ |
0 commit comments