flipside-js/src/api.ts
2019-01-29 10:36:34 -05:00

74 lines
1.7 KiB
TypeScript

import axios, { AxiosInstance, AxiosPromise } from "axios";
export default class API {
key: string;
client: AxiosInstance;
constructor(apiKey: string) {
this.key = apiKey;
this.client = axios.create({
baseURL: "https://platform-api.flipsidecrypto.com/api/v1",
params: { api_key: apiKey }
});
}
async _fetch(
method: string,
url: string,
params = {},
retryCount = 0,
retryMax = 15
): Promise<any> {
let res;
try {
res = await this.client.request({
url,
method,
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("GET", url, params, retryCount + 1);
}
return { data: null, success: false };
}
async fetchAssetMetric(symbol: string, metric: string, days = 7) {
const sym = `${symbol}`.toUpperCase();
return await this._fetch("GET", `/assets/${sym}/metrics/${metric}`, {
change_over: days
});
}
async fetchAssetMetrics(symbol: string) {
const sym = `${symbol}`.toUpperCase();
return await this._fetch("GET", `/assets/${sym}/metrics`);
}
async fetchFCASDistribution() {
return await this._fetch("GET", `/metrics/FCAS/assets`, {
visual_distribution: true
});
}
async fetchMetrics(payload: {
assets: string[];
exclusions: string[];
sort_by?: string;
sort_desc?: boolean;
page: number;
size?: number;
metrics: string[];
change_over: number;
}) {
return await this.client.post(`/assets/metrics`, payload);
}
}