diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 16:54:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-23 16:54:21 +0200 |
commit | de59c48f5f317018e3f746bbe4a7b7efe00109f2 (patch) | |
tree | bc3d007c5aaed8dc72119763f3b1731c5777f218 /client/src/app/shared/rest/rest-extractor.service.ts | |
parent | def16d33d19153c6583fa8a30634760b3d64d34c (diff) | |
download | PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.tar.gz PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.tar.zst PeerTube-de59c48f5f317018e3f746bbe4a7b7efe00109f2.zip |
Client: centralize http res extraction in a service
Diffstat (limited to 'client/src/app/shared/rest/rest-extractor.service.ts')
-rw-r--r-- | client/src/app/shared/rest/rest-extractor.service.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts new file mode 100644 index 000000000..aa44799af --- /dev/null +++ b/client/src/app/shared/rest/rest-extractor.service.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | ||
4 | |||
5 | export interface ResultList { | ||
6 | data: any[]; | ||
7 | total: number; | ||
8 | } | ||
9 | |||
10 | @Injectable() | ||
11 | export class RestExtractor { | ||
12 | |||
13 | constructor () { ; } | ||
14 | |||
15 | extractDataBool(res: Response) { | ||
16 | return true; | ||
17 | } | ||
18 | |||
19 | extractDataList(res: Response) { | ||
20 | const body = res.json(); | ||
21 | |||
22 | const ret: ResultList = { | ||
23 | data: body.data, | ||
24 | total: body.total | ||
25 | }; | ||
26 | |||
27 | return ret; | ||
28 | } | ||
29 | |||
30 | extractDataGet(res: Response) { | ||
31 | return res.json(); | ||
32 | } | ||
33 | |||
34 | handleError(res: Response) { | ||
35 | let text = 'Server error: '; | ||
36 | text += res.text(); | ||
37 | let json = res.json(); | ||
38 | |||
39 | const error = { | ||
40 | json, | ||
41 | text | ||
42 | }; | ||
43 | |||
44 | return Observable.throw(error); | ||
45 | } | ||
46 | } | ||