diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-04-19 03:22:02 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-04-19 08:12:38 +0800 |
commit | 51b2fce8c13c8e9393558d34dd664b7dd7749941 (patch) | |
tree | 94d623b55fac4d7b2f1a24e9ad413b065b4676e5 /packages/website/ts/utils/backend_client.ts | |
parent | 39c0064ffb73b8cb0b0b350e13bd7afec69e7e7e (diff) | |
download | dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar.gz dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar.bz2 dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar.lz dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar.xz dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.tar.zst dexon-0x-contracts-51b2fce8c13c8e9393558d34dd664b7dd7749941.zip |
Implement backendClient object to unify calls to the portal api
Diffstat (limited to 'packages/website/ts/utils/backend_client.ts')
-rw-r--r-- | packages/website/ts/utils/backend_client.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/packages/website/ts/utils/backend_client.ts b/packages/website/ts/utils/backend_client.ts new file mode 100644 index 000000000..366519856 --- /dev/null +++ b/packages/website/ts/utils/backend_client.ts @@ -0,0 +1,59 @@ +import { BigNumber, logUtils } from '@0xproject/utils'; +import * as _ from 'lodash'; +import * as queryString from 'query-string'; + +import { ArticlesBySection, ItemByAddress, WebsiteBackendGasInfo, WebsiteBackendPriceInfo } from 'ts/types'; +import { configs } from 'ts/utils/configs'; +import { errorReporter } from 'ts/utils/error_reporter'; + +const ETH_GAS_STATION_ENDPOINT = '/eth_gas_station'; +const PRICES_ENDPOINT = '/prices'; +const WIKI_ENDPOINT = '/wiki'; + +export const backendClient = { + async getGasInfoAsync(): Promise<WebsiteBackendGasInfo> { + const result = await requestAsync(ETH_GAS_STATION_ENDPOINT); + return result; + }, + async getPriceInfosAsync(tokenAddresses: string[]): Promise<WebsiteBackendPriceInfo[]> { + if (_.isEmpty(tokenAddresses)) { + return []; + } + const joinedTokenAddresses = tokenAddresses.join(','); + const queryParams = { + tokens: joinedTokenAddresses, + }; + const result = await requestAsync(PRICES_ENDPOINT, queryParams); + return result; + }, + async getWikiArticlesBySectionAsync(): Promise<ArticlesBySection> { + const result = await requestAsync(WIKI_ENDPOINT); + return result; + }, +}; + +async function requestAsync(endpoint: string, queryParams?: object): Promise<any> { + const query = queryStringFromQueryParams(queryParams); + const url = `${configs.BACKEND_BASE_URL}${endpoint}${query}`; + const response = await fetch(url); + if (response.status !== 200) { + const errorText = `Error requesting url: ${url}, ${response.status}: ${response.statusText}`; + logUtils.log(errorText); + const error = Error(errorText); + // tslint:disable-next-line:no-floating-promises + errorReporter.reportAsync(error); + throw error; + } + const result = await response.json(); + return result; +} + +function queryStringFromQueryParams(queryParams?: object): string { + // if params are undefined or empty, return an empty string + if (_.isUndefined(queryParams) || _.isEmpty(queryParams)) { + return ''; + } + // stringify the formatted object + const stringifiedParams = queryString.stringify(queryParams); + return `?${stringifiedParams}`; +} |