From e209f97fba89bd2bba348c866d55897df4c72e0d Mon Sep 17 00:00:00 2001 From: Adriel Diaz Date: Thu, 10 Sep 2020 09:06:38 -0600 Subject: [PATCH] feat: implement api failover endpoint, resolve #124 --- .env.example | 1 + src/config/evodex.config.js | 1 + src/utils/axios.util.js | 32 ++++++++++++++++++++++++++++++++ src/utils/evolutiondex.js | 9 +++++---- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/utils/axios.util.js diff --git a/.env.example b/.env.example index b55ca88..78834f2 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,5 @@ REACT_APP_EOS_BLOCK_EXPLORER_URL=https://jungle3.bloks.io REACT_APP_VOTE_CONTRACT=wevotethefee REACT_APP_EVODEX_CONTRACT=evolutiondex REACT_APP_EVODEX_API_URL=https://jungle.api.evolutiondex.io +REACT_APP_EVODEX_API_URL_FAILOVER=https://jungle.api-2.evolutiondex.io REACT_APP_PROJECT_VERSION=v0.1.0-beta diff --git a/src/config/evodex.config.js b/src/config/evodex.config.js index 18032bc..2fb8962 100644 --- a/src/config/evodex.config.js +++ b/src/config/evodex.config.js @@ -1,5 +1,6 @@ export const evodexConfig = { api: process.env.REACT_APP_EVODEX_API_URL, + apiFailover: process.env.REACT_APP_EVODEX_API_URL_FAILOVER, contract: process.env.REACT_APP_EVODEX_CONTRACT, voteContract: process.env.REACT_APP_VOTE_CONTRACT } diff --git a/src/utils/axios.util.js b/src/utils/axios.util.js new file mode 100644 index 0000000..e198a4f --- /dev/null +++ b/src/utils/axios.util.js @@ -0,0 +1,32 @@ +import axios from 'axios' + +export default (url, failover) => { + // Create instance + const axiosApi = axios.create({ + baseURL: url, + headers: { + 'Content-Type': 'application/json' + } + }) + + // Response interceptor failover for API calls + axiosApi.interceptors.response.use( + (response) => { + return response + }, + (error) => { + const config = error.config + + if (!config.retry) { + config.retry = true + config.baseURL = failover + + return axiosApi(config) + } + + return Promise.reject(error) + } + ) + + return axiosApi +} diff --git a/src/utils/evolutiondex.js b/src/utils/evolutiondex.js index 63ae7a0..acef249 100644 --- a/src/utils/evolutiondex.js +++ b/src/utils/evolutiondex.js @@ -1,12 +1,13 @@ -import axios from 'axios' import * as eosCommon from 'eos-common' import { evodexConfig } from '../config' import { getScatterError } from './getScatterError' +import axiosUtil from './axios.util' const { asset, number_to_asset: numberToAsset } = eosCommon const defaultState = { pairs: [], tokens: [] } +const axios = axiosUtil(evodexConfig.api, evodexConfig.apiFailover) const amountToAsset = (amount = '0', currentAsset) => { if (isNaN(amount)) { @@ -23,7 +24,7 @@ const amountToAsset = (amount = '0', currentAsset) => { return asset(`${validAmount} ${currentAsset.symbol.code().toString()}`) } const getInfo = async (ual) => { - const { data } = await axios.get(`${evodexConfig.api}/list`) + const { data } = await axios.get('/list') let userPools = [] if (ual?.activeUser) { @@ -43,7 +44,7 @@ const getInfo = async (ual) => { Pool2contract: pool2Contract, error } - } = await axios.get(`${evodexConfig.api}/info`, { + } = await axios.get('/info', { params: { pair: tokenPair } @@ -197,7 +198,7 @@ const exchange = async (amount, pair, ual) => { quantity: assetToGive.toString(), memo: `exchange: ${ pair.token - },${assetToReceive.toString()},sent using evodex.io` + },${assetToReceive.toString()},sent using evodex.io` } } ]