File size: 3,791 Bytes
0527ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17406e2
0527ba9
 
17406e2
 
0527ba9
 
17406e2
 
0527ba9
 
 
 
 
 
 
 
17406e2
 
0527ba9
 
 
 
 
 
 
 
17406e2
 
0527ba9
 
17406e2
 
0527ba9
 
 
17406e2
0527ba9
 
17406e2
0527ba9
 
 
17406e2
 
0527ba9
3b85fd7
 
 
 
 
0527ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class LoadBalancerAPI {
    constructor(baseURL) {
      this.baseURL = baseURL;
      this.filmCache = null; // Cache for film store
      this.tvCache = null;   // Cache for TV store
    }
  
    async getInstances() {
      return this._getRequest('/api/get/instances');
    }
  
    async getInstancesHealth() {
      return this._getRequest('/api/get/instances/health');
    }
  
    async getMovieByTitle(title) {
      return this._getRequest(`/api/get/movie/${encodeURIComponent(title)}`);
    }
  
    async getSeriesEpisode(title, season, episode) {
      return this._getRequest(`/api/get/series/${encodeURIComponent(title)}/${season}/${episode}`);
    }
  
    async getSeriesStore() {
      const response = await this._getRequest('/api/get/series/store');
      
      if (response && Object.keys(response).length > 0) {
        this.tvCache = response; // Update cache if response is not empty
      }
      
      return this.tvCache || {}; // Return cache if response is empty
    }
  
    async getMovieStore() {
      const response = await this._getRequest('/api/get/movie/store');
      
      if (response && Object.keys(response).length > 0) {
        this.filmCache = response; // Update cache if response is not empty
      }
      
      return this.filmCache || {}; // Return cache if response is empty
    }
  
    async getMovieMetadataByTitle(title) {
      return this._getRequest(`/api/get/movie/metadata/${encodeURIComponent(title)}`);
    }
  
    async getSeriesMetadataByTitle(title) {
      return this._getRequest(`/api/get/series/metadata/${encodeURIComponent(title)}`);
    }

    async getSeasonMetadataBySeriesId(seires_id, season) {
        return this._getRequest(`/api/get/series/metadata/${seires_id}/${season}`);
      }
  
    async getAllMovies() {
      return this._getRequest('/api/get/film/all');
    }
  
    async getAllSeriesShows() {
      return this._getRequest('/api/get/series/all');
    }

    async getRecent() {
      return this._getRequest('/api/get/recent');
    }

    async getDownloadProgress(url) {
      return this._getRequestNoBase(url);
    }
  
    // Helper methods for GET and POST requests
    async _getRequest(endpoint) {
      try {
        const response = await fetch(`${this.baseURL}${endpoint}`, {
          method: 'GET',
          headers: { 'Content-Type': 'application/json' },
        });
        console.log(`api endpoint: ${this.baseURL}${endpoint}`);
        return await this._handleResponse(response);
      } catch (error) {
        console.error(`Error during GET request to ${endpoint}:`, error);
        throw error;
      }
    }
  
    async _postRequest(endpoint, body) {
      try {
        const response = await fetch(`${this.baseURL}${endpoint}`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify(body),
        });
        return await this._handleResponse(response);
      } catch (error) {
        console.error(`Error during POST request to ${endpoint}:`, error);
        throw error;
      }
    }
  
    async _handleResponse(response) {
      if (!response.ok) {
        const errorDetails = await response.text();
        throw new Error(`HTTP error! status: ${response.status}, details: ${errorDetails}`);
      }
      return response.json();
    }
  
    async _getRequestNoBase(url) {
      try {
        const response = await fetch(`${url}`, {
          method: 'GET',
          headers: { 'Content-Type': 'application/json' },
        });
        console.log(`api endpoint: ${url}`);
        return await this._handleResponse(response);
      } catch (error) {
        console.error(`Error during GET request to ${url}:`, error);
        throw error;
      }
    }
  }
  
  export { LoadBalancerAPI };