Skip to content

Commit

Permalink
Use Coingecko proxy with special CI auth token for FiatApi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Apr 8, 2024
1 parent b15cfff commit 87a7d3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/complete-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ jobs:
- name: Install modules
run: yarn
- name: Run
env:
COINGECKO_PROXY_AUTHORIZATION_HEADER: ${{ secrets.COINGECKO_PROXY_AUTHORIZATION_HEADER }}
CI_AUTHORIZATION_TOKEN: ${{ secrets.CI_AUTHORIZATION_TOKEN }}
run: yarn pr
12 changes: 10 additions & 2 deletions src/fiat-api/FiatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ export function setCoingeckoApiUrl(url: string) {
API_URL = url;
}

const coingeckoExtraHeaders = new Map<string, string>();

export function setCoingeckoApiExtraHeader(name: string, value: string) {
coingeckoExtraHeaders.set(name, value);
}

type VsCurrency = FiatApiSupportedFiatCurrency | FiatApiBridgedFiatCurrency | FiatApiSupportedCryptoCurrency;
export async function getExchangeRates<C extends FiatApiSupportedCryptoCurrency, V extends VsCurrency>(
cryptoCurrencies: C[],
Expand Down Expand Up @@ -510,13 +516,15 @@ function _findTimestampChunk(
};
}

async function _fetch<T>(input: RequestInfo, init?: RequestInit): Promise<T> {
async function _fetch<T>(input: RequestInfo): Promise<T> {
let result: T | null = null;
do {
let retry = true;
try {
// eslint-disable-next-line no-await-in-loop
const response = await fetch(input, init); // throws when user is offline
const response = await fetch(input, {
headers: [...coingeckoExtraHeaders.entries()],
}); // throws when user is offline
if (!response.ok) {
if (response.status === 400) {
retry = false;
Expand Down
9 changes: 9 additions & 0 deletions tests/FiatApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ import {
FiatApiSupportedFiatCurrency,
getExchangeRates,
getHistoricExchangeRates,
setCoingeckoApiUrl,
setCoingeckoApiExtraHeader,
} from '../src/fiat-api/FiatApi';

// If tests are failing locally, increase the timeout
const timeout = /* use default timeout of 5000 */ 0;
// const timeout = 300000;

describe('FiatApi', () => {
beforeEach(() => {
setCoingeckoApiUrl('https://nq-coingecko-proxy.deno.dev/api/v3');
if (process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER && process.env.CI_AUTHORIZATION_TOKEN) {
setCoingeckoApiExtraHeader(process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER, process.env.CI_AUTHORIZATION_TOKEN);
}
});

it('can fetch current USD rate for BTC', async () => {
const rate = await getExchangeRates(
[FiatApiSupportedCryptoCurrency.BTC],
Expand Down

0 comments on commit 87a7d3f

Please sign in to comment.