]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/app/videos/shared/video.service.ts
Add authentication tokens to make friends/quit friends
[github/Chocobozzz/PeerTube.git] / client / app / videos / shared / video.service.ts
index 78789c3cc95543f6e7405388d2b758948ac83613..a786b2ab26984ab601a10cc56f4e291b7b1226ee 100644 (file)
@@ -5,35 +5,38 @@ import { Observable } from 'rxjs/Rx';
 import { Pagination } from './pagination.model';
 import { Search } from '../../shared/index';
 import { SortField } from './sort-field.type';
-import { AuthService } from '../../users/index';
+import { AuthService } from '../../shared/index';
 import { Video } from './video.model';
 
 @Injectable()
 export class VideoService {
-  private _baseVideoUrl = '/api/v1/videos/';
+  private static BASE_VIDEO_URL = '/api/v1/videos/';
 
-  constructor (private http: Http, private _authService: AuthService) {}
+  constructor(
+    private authService: AuthService,
+    private http: Http
+  ) {}
+
+  getVideo(id: string) {
+    return this.http.get(VideoService.BASE_VIDEO_URL + id)
+                    .map(res => <Video> res.json())
+                    .catch(this.handleError);
+  }
 
   getVideos(pagination: Pagination, sort: SortField) {
     const params = this.createPaginationParams(pagination);
 
     if (sort) params.set('sort', sort);
 
-    return this.http.get(this._baseVideoUrl, { search: params })
+    return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
                     .map(res => res.json())
                     .map(this.extractVideos)
                     .catch(this.handleError);
   }
 
-  getVideo(id: string) {
-    return this.http.get(this._baseVideoUrl + id)
-                    .map(res => <Video> res.json())
-                    .catch(this.handleError);
-  }
-
   removeVideo(id: string) {
-    const options = this._authService.getAuthRequestOptions();
-    return this.http.delete(this._baseVideoUrl + id, options)
+    const options = this.authService.getAuthRequestOptions();
+    return this.http.delete(VideoService.BASE_VIDEO_URL + id, options)
                     .map(res => <number> res.status)
                     .catch(this.handleError);
   }
@@ -44,13 +47,24 @@ export class VideoService {
     if (search.field) params.set('field', search.field);
     if (sort) params.set('sort', sort);
 
-    return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search.value), { search: params })
+    return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
                     .map(res => res.json())
                     .map(this.extractVideos)
                     .catch(this.handleError);
   }
 
-  private extractVideos (body: any) {
+  private createPaginationParams(pagination: Pagination) {
+    const params = new URLSearchParams();
+    const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
+    const count: number = pagination.itemsPerPage;
+
+    params.set('start', start.toString());
+    params.set('count', count.toString());
+
+    return params;
+  }
+
+  private extractVideos(body: any) {
     const videos_json = body.data;
     const totalVideos = body.total;
     const videos = [];
@@ -61,19 +75,8 @@ export class VideoService {
     return { videos, totalVideos };
   }
 
-  private handleError (error: Response) {
+  private handleError(error: Response) {
     console.error(error);
     return Observable.throw(error.json().error || 'Server error');
   }
-
-  private createPaginationParams(pagination: Pagination) {
-    const params = new URLSearchParams();
-    const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
-    const count: number = pagination.itemsPerPage;
-
-    params.set('start', start.toString());
-    params.set('count', count.toString());
-
-    return params;
-  }
 }