Add: Request timeout and hide loading on error

pull/16/head
Juraj Nyíri 4 years ago
parent 3f12a5942e
commit 936209a939

@ -18671,6 +18671,7 @@ class Plex {
constructor(ip, port = 32400, token, protocol = 'http') { constructor(ip, port = 32400, token, protocol = 'http') {
this.serverInfo = {}; this.serverInfo = {};
this.clients = []; this.clients = [];
this.requestTimeout = 5000;
this.init = async () => { this.init = async () => {
await this.getClients(); await this.getClients();
/* /*
@ -18681,8 +18682,15 @@ class Plex {
}; };
this.getClients = async () => { this.getClients = async () => {
const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`;
const result = await axios.get(url); try {
const result = await axios.get(url, {
timeout: this.requestTimeout
});
this.clients = result.data.MediaContainer.Server; this.clients = result.data.MediaContainer.Server;
}
catch (err) {
throw Error(`${err.message} while requesting URL "${url}".`);
}
return this.clients; return this.clients;
}; };
this.getServerID = async () => { this.getServerID = async () => {
@ -18693,24 +18701,32 @@ class Plex {
}; };
this.getServerInfo = async () => { this.getServerInfo = async () => {
const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`;
this.serverInfo = (await axios.get(url)).data.MediaContainer; this.serverInfo = (await axios.get(url, {
timeout: this.requestTimeout
})).data.MediaContainer;
return this.serverInfo; return this.serverInfo;
}; };
this.getSections = async () => { this.getSections = async () => {
const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Directory; return (await axios.get(url, {
timeout: this.requestTimeout
})).data.MediaContainer.Directory;
}; };
this.getSectionsData = async () => { this.getSectionsData = async () => {
const sections = await this.getSections(); const sections = await this.getSections();
const sectionsRequests = []; const sectionsRequests = [];
lodash.forEach(sections, section => { lodash.forEach(sections, section => {
sectionsRequests.push(axios.get(`${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}`)); sectionsRequests.push(axios.get(`${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}`, {
timeout: this.requestTimeout
}));
}); });
return this.exportSectionsData(await Promise.all(sectionsRequests)); return this.exportSectionsData(await Promise.all(sectionsRequests));
}; };
this.getLibraryData = async (id) => { this.getLibraryData = async (id) => {
const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Metadata; return (await axios.get(url, {
timeout: this.requestTimeout
})).data.MediaContainer.Metadata;
}; };
this.exportSectionsData = (sectionsData) => { this.exportSectionsData = (sectionsData) => {
const processedData = []; const processedData = [];
@ -19560,7 +19576,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.episodesElemHidden = true; this.episodesElemHidden = true;
this.data = {}; this.data = {};
this.config = {}; this.config = {};
this.requestTimeout = 3000;
this.loading = false; this.loading = false;
this.maxCount = false; this.maxCount = false;
this.error = ''; this.error = '';
@ -19569,11 +19584,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.loadInitialData = async () => { this.loadInitialData = async () => {
this.loading = true; this.loading = true;
this.renderPage(); this.renderPage();
if (this.plex) {
await this.plex.init();
}
try { try {
if (this.plex) { if (this.plex) {
await this.plex.init();
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]);
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
this.data.serverID = serverID; this.data.serverID = serverID;
@ -19591,7 +19604,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
} }
catch (err) { catch (err) {
// todo: proper timeout here
this.error = `Plex server did not respond.<br/>Details of the error: ${escapeHtml(err.message)}`; this.error = `Plex server did not respond.<br/>Details of the error: ${escapeHtml(err.message)}`;
this.renderPage(); this.renderPage();
} }

@ -15,6 +15,8 @@ class Plex {
clients: Array<Record<string, any>> = []; clients: Array<Record<string, any>> = [];
requestTimeout = 5000;
constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') { constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
@ -33,8 +35,14 @@ class Plex {
getClients = async (): Promise<Record<string, any>> => { getClients = async (): Promise<Record<string, any>> => {
const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`;
const result = await axios.get(url); try {
const result = await axios.get(url, {
timeout: this.requestTimeout
});
this.clients = result.data.MediaContainer.Server; this.clients = result.data.MediaContainer.Server;
} catch (err) {
throw Error(`${err.message} while requesting URL "${url}".`);
}
return this.clients; return this.clients;
}; };
@ -47,13 +55,21 @@ class Plex {
getServerInfo = async (): Promise<any> => { getServerInfo = async (): Promise<any> => {
const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/?X-Plex-Token=${this.token}`;
this.serverInfo = (await axios.get(url)).data.MediaContainer; this.serverInfo = (
await axios.get(url, {
timeout: this.requestTimeout
})
).data.MediaContainer;
return this.serverInfo; return this.serverInfo;
}; };
getSections = async (): Promise<any> => { getSections = async (): Promise<any> => {
const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Directory; return (
await axios.get(url, {
timeout: this.requestTimeout
})
).data.MediaContainer.Directory;
}; };
getSectionsData = async (): Promise<any> => { getSectionsData = async (): Promise<any> => {
@ -62,7 +78,10 @@ class Plex {
_.forEach(sections, section => { _.forEach(sections, section => {
sectionsRequests.push( sectionsRequests.push(
axios.get( axios.get(
`${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}` `${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}`,
{
timeout: this.requestTimeout
}
) )
); );
}); });
@ -71,7 +90,11 @@ class Plex {
getLibraryData = async (id: number): Promise<any> => { getLibraryData = async (id: number): Promise<any> => {
const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Metadata; return (
await axios.get(url, {
timeout: this.requestTimeout
})
).data.MediaContainer.Metadata;
}; };
private exportSectionsData = (sectionsData: Array<any>): Array<any> => { private exportSectionsData = (sectionsData: Array<any>): Array<any> => {

@ -65,8 +65,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
config: Record<string, any> = {}; config: Record<string, any> = {};
requestTimeout = 3000;
loading = false; loading = false;
maxCount: false | number = false; maxCount: false | number = false;
@ -100,12 +98,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
loadInitialData = async (): Promise<void> => { loadInitialData = async (): Promise<void> => {
this.loading = true; this.loading = true;
this.renderPage(); this.renderPage();
if (this.plex) {
await this.plex.init();
}
try { try {
if (this.plex) { if (this.plex) {
await this.plex.init();
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]);
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
this.data.serverID = serverID; this.data.serverID = serverID;
@ -123,7 +118,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw Error('Plex not initialized.'); throw Error('Plex not initialized.');
} }
} catch (err) { } catch (err) {
// todo: proper timeout here
this.error = `Plex server did not respond.<br/>Details of the error: ${escapeHtml(err.message)}`; this.error = `Plex server did not respond.<br/>Details of the error: ${escapeHtml(err.message)}`;
this.renderPage(); this.renderPage();
} }

Loading…
Cancel
Save