Browse Source

fix(coingecko.service): Add API key support for `getQuotes()` and `search()` + small refactor

pull/2827/head
Tanguy Herbron 2 years ago
committed by Thomas Kaul
parent
commit
5e2bd1ce1d
  1. 17
      apps/api/src/services/data-provider/coingecko/coingecko.service.ts

17
apps/api/src/services/data-provider/coingecko/coingecko.service.ts

@ -27,11 +27,14 @@ export class CoinGeckoService implements DataProviderInterface {
public constructor( public constructor(
private readonly configurationService: ConfigurationService private readonly configurationService: ConfigurationService
) { ) {
if (this.configurationService.get('COINGECKO_API_KEY_DEMO')) { const apiKeyDemo = this.configurationService.get('COINGECKO_API_KEY_DEMO');
const apiKeyPro = this.configurationService.get('COINGECKO_API_KEY_PRO');
if (apiKeyDemo) {
this.headers['x-cg-demo-api-key'] = this.configurationService.get('COINGECKO_API_KEY_DEMO'); this.headers['x-cg-demo-api-key'] = this.configurationService.get('COINGECKO_API_KEY_DEMO');
} }
if (this.configurationService.get('COINGECKO_API_KEY_PRO')) { if (apiKeyPro) {
this.headers['x-cg-pro-api-key'] = this.configurationService.get('COINGECKO_API_KEY_PRO'); this.headers['x-cg-pro-api-key'] = this.configurationService.get('COINGECKO_API_KEY_PRO');
this.URL = 'https://pro-api.coingecko.com/api/v3'; this.URL = 'https://pro-api.coingecko.com/api/v3';
} }
@ -60,9 +63,9 @@ export class CoinGeckoService implements DataProviderInterface {
}, this.configurationService.get('REQUEST_TIMEOUT')); }, this.configurationService.get('REQUEST_TIMEOUT'));
const { name } = await got(`${this.URL}/coins/${aSymbol}`, { const { name } = await got(`${this.URL}/coins/${aSymbol}`, {
headers: this.headers,
// @ts-ignore // @ts-ignore
signal: abortController.signal, signal: abortController.signal
headers: this.headers
}).json<any>(); }).json<any>();
response.name = name; response.name = name;
@ -117,9 +120,9 @@ export class CoinGeckoService implements DataProviderInterface {
from from
)}&to=${getUnixTime(to)}`, )}&to=${getUnixTime(to)}`,
{ {
headers: this.headers,
// @ts-ignore // @ts-ignore
signal: abortController.signal, signal: abortController.signal
headers: this.headers
} }
).json<any>(); ).json<any>();
@ -179,6 +182,7 @@ export class CoinGeckoService implements DataProviderInterface {
',' ','
)}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`, )}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`,
{ {
headers: this.headers,
// @ts-ignore // @ts-ignore
signal: abortController.signal signal: abortController.signal
} }
@ -229,6 +233,7 @@ export class CoinGeckoService implements DataProviderInterface {
}, this.configurationService.get('REQUEST_TIMEOUT')); }, this.configurationService.get('REQUEST_TIMEOUT'));
const { coins } = await got(`${this.URL}/search?query=${query}`, { const { coins } = await got(`${this.URL}/search?query=${query}`, {
headers: this.headers,
// @ts-ignore // @ts-ignore
signal: abortController.signal signal: abortController.signal
}).json<any>(); }).json<any>();

Loading…
Cancel
Save