|
class LoadBalancerAPI { |
|
constructor(baseURL) { |
|
this.baseURL = baseURL; |
|
this.filmCache = null; |
|
this.tvCache = null; |
|
} |
|
|
|
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; |
|
} |
|
|
|
return this.tvCache || {}; |
|
} |
|
|
|
async getMovieStore() { |
|
const response = await this._getRequest('/api/get/movie/store'); |
|
|
|
if (response && Object.keys(response).length > 0) { |
|
this.filmCache = response; |
|
} |
|
|
|
return this.filmCache || {}; |
|
} |
|
|
|
async getMovieMetadataByTitle(title) { |
|
return this._getRequest(`/api/get/movie/metadata/${encodeURIComponent(title)}`); |
|
} |
|
|
|
async getMovieCard(title) { |
|
return this._getRequest(`/api/get/movie/card/${encodeURIComponent(title)}`); |
|
} |
|
|
|
async getSeriesMetadataByTitle(title) { |
|
return this._getRequest(`/api/get/series/metadata/${encodeURIComponent(title)}`); |
|
} |
|
|
|
async getSeriesCard(title) { |
|
return this._getRequest(`/api/get/series/card/${encodeURIComponent(title)}`); |
|
} |
|
|
|
async getSeasonMetadataBySeriesId(series_id, season) { |
|
return this._getRequest(`/api/get/series/metadata/${series_id}/${season}`); |
|
} |
|
|
|
async getAllMovies() { |
|
return this._getRequest('/api/get/movie/all'); |
|
} |
|
|
|
async getAllSeriesShows() { |
|
return this._getRequest('/api/get/series/all'); |
|
} |
|
|
|
async getRecent() { |
|
return this._getRequest('/api/get/recent'); |
|
} |
|
|
|
async getGenreItems(genres, mediaType = null, limit = 5) { |
|
|
|
const queryParams = new URLSearchParams(); |
|
|
|
|
|
genres.forEach(genre => queryParams.append('genre', genre)); |
|
|
|
|
|
queryParams.append('limit', limit); |
|
|
|
|
|
if (mediaType) { |
|
queryParams.append('media_type', mediaType); |
|
} |
|
|
|
|
|
return this._getRequest(`/api/get/genre?${queryParams.toString()}`); |
|
} |
|
|
|
async getDownloadProgress(url) { |
|
return this._getRequestNoBase(url); |
|
} |
|
|
|
|
|
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 }; |
|
|