1 import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models'
2 import { logger } from '../../../root-helpers'
3 import { AuthHTTP } from './auth-http'
5 export class VideoFetcher {
7 constructor (private readonly http: AuthHTTP) {
11 async loadVideo (videoId: string) {
12 const videoPromise = this.loadVideoInfo(videoId)
14 let videoResponse: Response
15 let isResponseOk: boolean
18 videoResponse = await videoPromise
19 isResponseOk = videoResponse.status === HttpStatusCode.OK_200
27 if (videoResponse?.status === HttpStatusCode.NOT_FOUND_404) {
28 throw new Error('This video does not exist.')
31 throw new Error('We cannot fetch the video. Please try again later.')
34 const captionsPromise = this.loadVideoCaptions(videoId)
36 return { captionsPromise, videoResponse }
39 loadLive (video: VideoDetails) {
40 return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true })
41 .then(res => res.json() as Promise<LiveVideo>)
44 loadVideoToken (video: VideoDetails) {
45 return this.http.fetch(this.getVideoTokenUrl(video.uuid), { optionalAuth: true, method: 'POST' })
46 .then(res => res.json() as Promise<VideoToken>)
47 .then(token => token.files.token)
50 getVideoViewsUrl (videoUUID: string) {
51 return this.getVideoUrl(videoUUID) + '/views'
54 private loadVideoInfo (videoId: string): Promise<Response> {
55 return this.http.fetch(this.getVideoUrl(videoId), { optionalAuth: true })
58 private loadVideoCaptions (videoId: string): Promise<Response> {
59 return this.http.fetch(this.getVideoUrl(videoId) + '/captions', { optionalAuth: true })
62 private getVideoUrl (id: string) {
63 return window.location.origin + '/api/v1/videos/' + id
66 private getLiveUrl (videoId: string) {
67 return window.location.origin + '/api/v1/videos/live/' + videoId
70 private getVideoTokenUrl (id: string) {
71 return this.getVideoUrl(id) + '/token'