From 232220aa789eded78132b01934c2580e4b01b825 Mon Sep 17 00:00:00 2001 From: Jim Myers Date: Mon, 3 Dec 2018 10:29:29 -0500 Subject: [PATCH] Add retry handling logic, in case of request failures. --- README.md | 4 ++-- package.json | 2 +- src/api.js | 18 ++++++++++++------ src/fcas/index.js | 24 ++++++++++++++++++++---- src/fcas/plot/index.js | 16 +++++++++++++--- 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6eafcbd..bc9e340 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,14 @@ To get started, request an API Key: api@flipsidecrypto.com. ## Live Example -[View Live FCAS Widget Example](https://jsfiddle.net/flipsidejim/f7zpd0uj/18/) +[View Live FCAS Widget Example](https://jsfiddle.net/flipsidejim/f7zpd0uj/20/) ## Install The FlipsideJS library is made available over our CDN to ensure a speedy response time. ```html - + ``` ## Usage diff --git a/package.json b/package.json index 8c76ef8..bf60b05 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flipside-js", - "version": "1.2.0", + "version": "1.3.0", "description": "FlipsideJS provides a library embeddable widgets that display data from the Flipside Platform API, including FCAS.", "main": "index.js", "scripts": { diff --git a/src/api.js b/src/api.js index d71c608..3b1dbca 100644 --- a/src/api.js +++ b/src/api.js @@ -8,16 +8,22 @@ export default class API { }); } - async _fetch(url, params, retryCount = 0, retryMax = 100) { - const res = await this.client.get(url, { params: params }); - if (res.status >= 200 || res.status < 300) { - return res.data; + async _fetch(url, params, retryCount = 0, retryMax = 15) { + let res; + try { + res = await this.client.get(url, { params: params }); + if (res.status >= 200 && res.status < 300) { + return { data: res.data, success: true }; + } + } catch (e) { + console.log( + `Failed to fetch data from: "${url}". \nError message: "${e}"` + ); } if (retryCount < retryMax) { return await this._fetch(url, params, retryCount + 1); - } else { - throw `Failed to fetch data from: ${url}.`; } + return { data: null, success: false }; } async fetchAssetMetric(symbol, metric = "FCAS", days = 7) { diff --git a/src/fcas/index.js b/src/fcas/index.js index c359767..593cdc8 100644 --- a/src/fcas/index.js +++ b/src/fcas/index.js @@ -10,12 +10,17 @@ export default class FCAS extends Component { } async _getData() { - const data = await this.props.api.fetchAssetMetric( + const { data, success } = await this.props.api.fetchAssetMetric( this.props.symbol, "FCAS" ); - if (!data) return; + if (!success || !data) { + setTimeout(() => { + return this._getData(); + }, 2000); + return success; + } this.setState({ loading: false, @@ -25,12 +30,13 @@ export default class FCAS extends Component { name: data.asset_name } }); + return success; } _update() { this.interval = setInterval(async () => { await this._getData(); - }, 30000); + }, 300000); } componentWillUnmount() { @@ -38,7 +44,17 @@ export default class FCAS extends Component { } async componentDidMount() { - await this._getData(); + const success = await this._getData(); + if (!success) { + this.setState({ + loading: false, + metric: { + fcas: "NA", + change: "NA", + name: "NA" + } + }); + } this._update(); } diff --git a/src/fcas/plot/index.js b/src/fcas/plot/index.js index 2dddd24..cfda043 100644 --- a/src/fcas/plot/index.js +++ b/src/fcas/plot/index.js @@ -14,16 +14,23 @@ export default class Plot extends Component { } async _getData() { - let data = await this.props.api.fetchFCASDistribution(); + const { data, success } = await this.props.api.fetchFCASDistribution(); + if (!success || !data) { + setTimeout(() => { + return this._getData(); + }, 2000); + return success; + } if (data && data.length > 0) { this.setState({ loading: false, distribution: data }); } + return success; } _update() { this.interval = setInterval(async () => { await this._getData(); - }, 30000); + }, 300000); } componentWillUnmount() { @@ -31,7 +38,10 @@ export default class Plot extends Component { } async componentDidMount() { - await this._getData(); + const success = await this._getData(); + if (!success) { + this.setState({ loading: false, distribution: [] }); + } this._update(); }