From 936209a9390f1f2918a181aa931c1b5ae3f43c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Fri, 14 May 2021 10:13:54 +0200 Subject: [PATCH] Add: Request timeout and hide loading on error --- dist/plex-meets-homeassistant.js | 34 +++++++++++++++++++++---------- src/modules/Plex.ts | 35 ++++++++++++++++++++++++++------ src/plex-meets-homeassistant.ts | 8 +------- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 5b50661..4959ded 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18671,6 +18671,7 @@ class Plex { constructor(ip, port = 32400, token, protocol = 'http') { this.serverInfo = {}; this.clients = []; + this.requestTimeout = 5000; this.init = async () => { await this.getClients(); /* @@ -18681,8 +18682,15 @@ class Plex { }; this.getClients = async () => { const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`; - const result = await axios.get(url); - this.clients = result.data.MediaContainer.Server; + try { + const result = await axios.get(url, { + timeout: this.requestTimeout + }); + this.clients = result.data.MediaContainer.Server; + } + catch (err) { + throw Error(`${err.message} while requesting URL "${url}".`); + } return this.clients; }; this.getServerID = async () => { @@ -18693,24 +18701,32 @@ class Plex { }; this.getServerInfo = async () => { 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; }; this.getSections = async () => { 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 () => { const sections = await this.getSections(); const sectionsRequests = []; 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)); }; this.getLibraryData = async (id) => { 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) => { const processedData = []; @@ -19560,7 +19576,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.episodesElemHidden = true; this.data = {}; this.config = {}; - this.requestTimeout = 3000; this.loading = false; this.maxCount = false; this.error = ''; @@ -19569,11 +19584,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.loadInitialData = async () => { this.loading = true; this.renderPage(); - if (this.plex) { - await this.plex.init(); - } try { if (this.plex) { + await this.plex.init(); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); // eslint-disable-next-line @typescript-eslint/camelcase this.data.serverID = serverID; @@ -19591,7 +19604,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { } } catch (err) { - // todo: proper timeout here this.error = `Plex server did not respond.
Details of the error: ${escapeHtml(err.message)}`; this.renderPage(); } diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index 18102c7..766da6c 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -15,6 +15,8 @@ class Plex { clients: Array> = []; + requestTimeout = 5000; + constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') { this.ip = ip; this.port = port; @@ -33,8 +35,14 @@ class Plex { getClients = async (): Promise> => { const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`; - const result = await axios.get(url); - this.clients = result.data.MediaContainer.Server; + try { + const result = await axios.get(url, { + timeout: this.requestTimeout + }); + this.clients = result.data.MediaContainer.Server; + } catch (err) { + throw Error(`${err.message} while requesting URL "${url}".`); + } return this.clients; }; @@ -47,13 +55,21 @@ class Plex { getServerInfo = async (): Promise => { 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; }; getSections = async (): Promise => { 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 => { @@ -62,7 +78,10 @@ class Plex { _.forEach(sections, section => { sectionsRequests.push( 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 => { 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): Array => { diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index e070c51..9589ec4 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -65,8 +65,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { config: Record = {}; - requestTimeout = 3000; - loading = false; maxCount: false | number = false; @@ -100,12 +98,9 @@ class PlexMeetsHomeAssistant extends HTMLElement { loadInitialData = async (): Promise => { this.loading = true; this.renderPage(); - if (this.plex) { - await this.plex.init(); - } - try { if (this.plex) { + await this.plex.init(); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); // eslint-disable-next-line @typescript-eslint/camelcase this.data.serverID = serverID; @@ -123,7 +118,6 @@ class PlexMeetsHomeAssistant extends HTMLElement { throw Error('Plex not initialized.'); } } catch (err) { - // todo: proper timeout here this.error = `Plex server did not respond.
Details of the error: ${escapeHtml(err.message)}`; this.renderPage(); }