Spaces:
Build error
Build error
| 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/instances'); | |
| } | |
| async getInstancesHealth() { | |
| return this._getRequest('/api/instances/health'); | |
| } | |
| async getMovieByTitle(title) { | |
| return this._getRequest(`/api/film/${encodeURIComponent(title)}`); | |
| } | |
| async getTVEpisode(title, season, episode) { | |
| return this._getRequest(`/api/tv/${encodeURIComponent(title)}/${season}/${episode}`); | |
| } | |
| async getFilmIDByTitle(title) { | |
| return this._getRequest(`/api/filmid/${encodeURIComponent(title)}`); | |
| } | |
| async getEpisodeIDByTitleSeasonEpisode(title, season, episode) { | |
| return this._getRequest(`/api/episodeid/${encodeURIComponent(title)}/${season}/${episode}`); | |
| } | |
| async getCacheSize() { | |
| return this._getRequest('/api/cache/size'); | |
| } | |
| async clearCache() { | |
| return this._postRequest('/api/cache/clear'); | |
| } | |
| async getTVStore() { | |
| const response = await this._getRequest('/api/tv/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 getFilmStore() { | |
| const response = await this._getRequest('/api/film/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 getFilmMetadataByTitle(title) { | |
| return this._getRequest(`/api/film/metadata/${encodeURIComponent(title)}`); | |
| } | |
| async getTVMetadataByTitle(title) { | |
| return this._getRequest(`/api/tv/metadata/${encodeURIComponent(title)}`); | |
| } | |
| async getAllFilms() { | |
| return this._getRequest('/api/film/all'); | |
| } | |
| async getAllTVShows() { | |
| return this._getRequest('/api/tv/all'); | |
| } | |
| 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 }; | |