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/admin | |
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/admin')
-rw-r--r-- | client/src/app/admin/friends/shared/friend.service.ts | 22 | ||||
-rw-r--r-- | client/src/app/admin/users/shared/user.service.ts | 28 |
2 files changed, 22 insertions, 28 deletions
diff --git a/client/src/app/admin/friends/shared/friend.service.ts b/client/src/app/admin/friends/shared/friend.service.ts index e4e680c29..75826fc17 100644 --- a/client/src/app/admin/friends/shared/friend.service.ts +++ b/client/src/app/admin/friends/shared/friend.service.ts | |||
@@ -1,9 +1,8 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | 2 | import { Observable } from 'rxjs/Observable'; |
4 | 3 | ||
5 | import { Friend } from './friend.model'; | 4 | import { Friend } from './friend.model'; |
6 | import { AuthHttp, AuthService } from '../../../shared'; | 5 | import { AuthHttp, RestExtractor } from '../../../shared'; |
7 | 6 | ||
8 | @Injectable() | 7 | @Injectable() |
9 | export class FriendService { | 8 | export class FriendService { |
@@ -11,13 +10,15 @@ export class FriendService { | |||
11 | 10 | ||
12 | constructor ( | 11 | constructor ( |
13 | private authHttp: AuthHttp, | 12 | private authHttp: AuthHttp, |
14 | private authService: AuthService | 13 | private restExtractor: RestExtractor |
15 | ) {} | 14 | ) {} |
16 | 15 | ||
17 | getFriends(): Observable<Friend[]> { | 16 | getFriends(): Observable<Friend[]> { |
18 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) | 17 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) |
19 | .map(res => <Friend[]>res.json()) | 18 | // Not implemented as a data list by the server yet |
20 | .catch(this.handleError); | 19 | // .map(this.restExtractor.extractDataList) |
20 | .map((res) => res.json()) | ||
21 | .catch((res) => this.restExtractor.handleError(res)); | ||
21 | } | 22 | } |
22 | 23 | ||
23 | makeFriends(notEmptyUrls) { | 24 | makeFriends(notEmptyUrls) { |
@@ -26,18 +27,13 @@ export class FriendService { | |||
26 | }; | 27 | }; |
27 | 28 | ||
28 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) | 29 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) |
29 | .map(res => res.status) | 30 | .map(this.restExtractor.extractDataBool) |
30 | .catch(this.handleError); | 31 | .catch((res) => this.restExtractor.handleError(res)); |
31 | } | 32 | } |
32 | 33 | ||
33 | quitFriends() { | 34 | quitFriends() { |
34 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') | 35 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') |
35 | .map(res => res.status) | 36 | .map(res => res.status) |
36 | .catch(this.handleError); | 37 | .catch((res) => this.restExtractor.handleError(res)); |
37 | } | ||
38 | |||
39 | private handleError (error: Response) { | ||
40 | console.error(error); | ||
41 | return Observable.throw(error.json().error || 'Server error'); | ||
42 | } | 38 | } |
43 | } | 39 | } |
diff --git a/client/src/app/admin/users/shared/user.service.ts b/client/src/app/admin/users/shared/user.service.ts index be433f0a1..d96db4575 100644 --- a/client/src/app/admin/users/shared/user.service.ts +++ b/client/src/app/admin/users/shared/user.service.ts | |||
@@ -1,15 +1,16 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Response } from '@angular/http'; | ||
3 | import { Observable } from 'rxjs/Observable'; | ||
4 | 2 | ||
5 | import { AuthHttp, User } from '../../../shared'; | 3 | import { AuthHttp, RestExtractor, ResultList, User } from '../../../shared'; |
6 | 4 | ||
7 | @Injectable() | 5 | @Injectable() |
8 | export class UserService { | 6 | export class UserService { |
9 | // TODO: merge this constant with account | 7 | // TODO: merge this constant with account |
10 | private static BASE_USERS_URL = '/api/v1/users/'; | 8 | private static BASE_USERS_URL = '/api/v1/users/'; |
11 | 9 | ||
12 | constructor(private authHttp: AuthHttp) {} | 10 | constructor( |
11 | private authHttp: AuthHttp, | ||
12 | private restExtractor: RestExtractor | ||
13 | ) {} | ||
13 | 14 | ||
14 | addUser(username: string, password: string) { | 15 | addUser(username: string, password: string) { |
15 | const body = { | 16 | const body = { |
@@ -17,23 +18,25 @@ export class UserService { | |||
17 | password | 18 | password |
18 | }; | 19 | }; |
19 | 20 | ||
20 | return this.authHttp.post(UserService.BASE_USERS_URL, body); | 21 | return this.authHttp.post(UserService.BASE_USERS_URL, body) |
22 | .map(this.restExtractor.extractDataBool) | ||
23 | .catch((res) => this.restExtractor.handleError(res)); | ||
21 | } | 24 | } |
22 | 25 | ||
23 | getUsers() { | 26 | getUsers() { |
24 | return this.authHttp.get(UserService.BASE_USERS_URL) | 27 | return this.authHttp.get(UserService.BASE_USERS_URL) |
25 | .map(res => res.json()) | 28 | .map(this.restExtractor.extractDataList) |
26 | .map(this.extractUsers) | 29 | .map(this.extractUsers) |
27 | .catch(this.handleError); | 30 | .catch((res) => this.restExtractor.handleError(res)); |
28 | } | 31 | } |
29 | 32 | ||
30 | removeUser(user: User) { | 33 | removeUser(user: User) { |
31 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); | 34 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id); |
32 | } | 35 | } |
33 | 36 | ||
34 | private extractUsers(body: any) { | 37 | private extractUsers(result: ResultList) { |
35 | const usersJson = body.data; | 38 | const usersJson = result.data; |
36 | const totalUsers = body.total; | 39 | const totalUsers = result.total; |
37 | const users = []; | 40 | const users = []; |
38 | for (const userJson of usersJson) { | 41 | for (const userJson of usersJson) { |
39 | users.push(new User(userJson)); | 42 | users.push(new User(userJson)); |
@@ -41,9 +44,4 @@ export class UserService { | |||
41 | 44 | ||
42 | return { users, totalUsers }; | 45 | return { users, totalUsers }; |
43 | } | 46 | } |
44 | |||
45 | private handleError(error: Response) { | ||
46 | console.error(error); | ||
47 | return Observable.throw(error.json().error || 'Server error'); | ||
48 | } | ||
49 | } | 47 | } |