1 import { HttpStatusCode, LiveVideo, VideoDetails } from '../../../../../shared/models'
2 import { AuthHTTP } from './auth-http'
4 export class VideoFetcher {
6 constructor (private readonly http: AuthHTTP) {
10 async loadVideo (videoId: string) {
11 const videoPromise = this.loadVideoInfo(videoId)
13 let videoResponse: Response
14 let isResponseOk: boolean
17 videoResponse = await videoPromise
18 isResponseOk = videoResponse.status === HttpStatusCode.OK_200
26 if (videoResponse?.status === HttpStatusCode.NOT_FOUND_404) {
27 throw new Error('This video does not exist.')
30 throw new Error('We cannot fetch the video. Please try again later.')
33 const captionsPromise = this.loadVideoCaptions(videoId)
35 return { captionsPromise, videoResponse }
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 }))
44 getVideoViewsUrl (videoUUID: string) {
45 return this.getVideoUrl(videoUUID) + '/views'
48 private loadVideoInfo (videoId: string): Promise<Response> {
49 return this.http.fetch(this.getVideoUrl(videoId), { optionalAuth: true })
52 private loadVideoCaptions (videoId: string): Promise<Response> {
53 return this.http.fetch(this.getVideoUrl(videoId) + '/captions', { optionalAuth: true })
56 private getVideoUrl (id: string) {
57 return window.location.origin + '/api/v1/videos/' + id
60 private getLiveUrl (videoId: string) {
61 return window.location.origin + '/api/v1/videos/live/' + videoId