From 87a7d3fb8e5e5d32d2a2ddf40ebd53d3d9055b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Mon, 8 Apr 2024 16:46:24 +0200 Subject: [PATCH] Use Coingecko proxy with special CI auth token for FiatApi tests --- .github/workflows/complete-check.yml | 3 +++ src/fiat-api/FiatApi.ts | 12 ++++++++++-- tests/FiatApi.spec.ts | 9 +++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/complete-check.yml b/.github/workflows/complete-check.yml index c563c93..e3e14a8 100644 --- a/.github/workflows/complete-check.yml +++ b/.github/workflows/complete-check.yml @@ -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 diff --git a/src/fiat-api/FiatApi.ts b/src/fiat-api/FiatApi.ts index f4dfc6c..1f4807d 100644 --- a/src/fiat-api/FiatApi.ts +++ b/src/fiat-api/FiatApi.ts @@ -281,6 +281,12 @@ export function setCoingeckoApiUrl(url: string) { API_URL = url; } +const coingeckoExtraHeaders = new Map(); + +export function setCoingeckoApiExtraHeader(name: string, value: string) { + coingeckoExtraHeaders.set(name, value); +} + type VsCurrency = FiatApiSupportedFiatCurrency | FiatApiBridgedFiatCurrency | FiatApiSupportedCryptoCurrency; export async function getExchangeRates( cryptoCurrencies: C[], @@ -510,13 +516,15 @@ function _findTimestampChunk( }; } -async function _fetch(input: RequestInfo, init?: RequestInit): Promise { +async function _fetch(input: RequestInfo): Promise { 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; diff --git a/tests/FiatApi.spec.ts b/tests/FiatApi.spec.ts index c084171..8d05b0f 100644 --- a/tests/FiatApi.spec.ts +++ b/tests/FiatApi.spec.ts @@ -10,6 +10,8 @@ import { FiatApiSupportedFiatCurrency, getExchangeRates, getHistoricExchangeRates, + setCoingeckoApiUrl, + setCoingeckoApiExtraHeader, } from '../src/fiat-api/FiatApi'; // If tests are failing locally, increase the timeout @@ -17,6 +19,13 @@ 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],