]>
Commit | Line | Data |
---|---|---|
f1a0f3b7 C |
1 | import { HttpStatusCode, LiveVideo, VideoDetails } from '../../../../../shared/models' |
2 | import { AuthHTTP } from './auth-http' | |
3 | ||
4 | export class VideoFetcher { | |
5 | ||
6 | constructor (private readonly http: AuthHTTP) { | |
7 | ||
8 | } | |
9 | ||
10 | async loadVideo (videoId: string) { | |
11 | const videoPromise = this.loadVideoInfo(videoId) | |
12 | ||
13 | let videoResponse: Response | |
14 | let isResponseOk: boolean | |
15 | ||
16 | try { | |
17 | videoResponse = await videoPromise | |
18 | isResponseOk = videoResponse.status === HttpStatusCode.OK_200 | |
19 | } catch (err) { | |
20 | console.error(err) | |
21 | ||
22 | isResponseOk = false | |
23 | } | |
24 | ||
25 | if (!isResponseOk) { | |
26 | if (videoResponse?.status === HttpStatusCode.NOT_FOUND_404) { | |
27 | throw new Error('This video does not exist.') | |
28 | } | |
29 | ||
30 | throw new Error('We cannot fetch the video. Please try again later.') | |
31 | } | |
32 | ||
33 | const captionsPromise = this.loadVideoCaptions(videoId) | |
34 | ||
35 | return { captionsPromise, videoResponse } | |
36 | } | |
37 | ||
38 | loadVideoWithLive (video: VideoDetails) { | |
39 | return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true }) | |
40 | .then(res => res.json()) | |
41 | .then((live: LiveVideo) => ({ video, live })) | |
42 | } | |
43 | ||
44 | getVideoViewsUrl (videoUUID: string) { | |
45 | return this.getVideoUrl(videoUUID) + '/views' | |
46 | } | |
47 | ||
48 | private loadVideoInfo (videoId: string): Promise<Response> { | |
49 | return this.http.fetch(this.getVideoUrl(videoId), { optionalAuth: true }) | |
50 | } | |
51 | ||
52 | private loadVideoCaptions (videoId: string): Promise<Response> { | |
53 | return this.http.fetch(this.getVideoUrl(videoId) + '/captions', { optionalAuth: true }) | |
54 | } | |
55 | ||
56 | private getVideoUrl (id: string) { | |
57 | return window.location.origin + '/api/v1/videos/' + id | |
58 | } | |
59 | ||
60 | private getLiveUrl (videoId: string) { | |
61 | return window.location.origin + '/api/v1/videos/live/' + videoId | |
62 | } | |
63 | } |