Refactor interface of getQuotes() to object (#2570)

pull/2578/head
Thomas Kaul 1 year ago committed by GitHub
parent cf7ce64de7
commit a704378702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -105,9 +105,11 @@ export class AlphaVantageService implements DataProviderInterface {
return DataSource.ALPHA_VANTAGE; return DataSource.ALPHA_VANTAGE;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }> {
return {}; return {};
} }

@ -134,13 +134,15 @@ export class CoinGeckoService implements DataProviderInterface {
return DataSource.COINGECKO; return DataSource.COINGECKO;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
const results: { [symbol: string]: IDataProviderResponse } = {}; symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }> {
const response: { [symbol: string]: IDataProviderResponse } = {};
if (aSymbols.length <= 0) { if (symbols.length <= 0) {
return {}; return response;
} }
try { try {
@ -151,7 +153,7 @@ export class CoinGeckoService implements DataProviderInterface {
}, DEFAULT_REQUEST_TIMEOUT); }, DEFAULT_REQUEST_TIMEOUT);
const response = await got( const response = await got(
`${this.URL}/simple/price?ids=${aSymbols.join( `${this.URL}/simple/price?ids=${symbols.join(
',' ','
)}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`, )}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`,
{ {
@ -162,7 +164,7 @@ export class CoinGeckoService implements DataProviderInterface {
for (const symbol in response) { for (const symbol in response) {
if (Object.prototype.hasOwnProperty.call(response, symbol)) { if (Object.prototype.hasOwnProperty.call(response, symbol)) {
results[symbol] = { response[symbol] = {
currency: DEFAULT_CURRENCY, currency: DEFAULT_CURRENCY,
dataProviderInfo: this.getDataProviderInfo(), dataProviderInfo: this.getDataProviderInfo(),
dataSource: DataSource.COINGECKO, dataSource: DataSource.COINGECKO,
@ -175,7 +177,7 @@ export class CoinGeckoService implements DataProviderInterface {
Logger.error(error, 'CoinGeckoService'); Logger.error(error, 'CoinGeckoService');
} }
return results; return response;
} }
public getTestSymbol() { public getTestSymbol() {

@ -311,7 +311,9 @@ export class DataProviderService {
i + maximumNumberOfSymbolsPerRequest i + maximumNumberOfSymbolsPerRequest
); );
const promise = Promise.resolve(dataProvider.getQuotes(symbolsChunk)); const promise = Promise.resolve(
dataProvider.getQuotes({ symbols: symbolsChunk })
);
promises.push( promises.push(
promise.then(async (result) => { promise.then(async (result) => {

@ -131,17 +131,21 @@ export class EodHistoricalDataService implements DataProviderInterface {
return DataSource.EOD_HISTORICAL_DATA; return DataSource.EOD_HISTORICAL_DATA;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
const symbols = aSymbols.map((symbol) => { symbols: string[];
return this.convertToEodSymbol(symbol); }): Promise<{ [symbol: string]: IDataProviderResponse }> {
}); let response: { [symbol: string]: IDataProviderResponse } = {};
if (symbols.length <= 0) { if (symbols.length <= 0) {
return {}; return response;
} }
const eodHistoricalDataSymbols = symbols.map((symbol) => {
return this.convertToEodSymbol(symbol);
});
try { try {
const abortController = new AbortController(); const abortController = new AbortController();
@ -150,9 +154,9 @@ export class EodHistoricalDataService implements DataProviderInterface {
}, DEFAULT_REQUEST_TIMEOUT); }, DEFAULT_REQUEST_TIMEOUT);
const realTimeResponse = await got( const realTimeResponse = await got(
`${this.URL}/real-time/${symbols[0]}?api_token=${ `${this.URL}/real-time/${eodHistoricalDataSymbols[0]}?api_token=${
this.apiKey this.apiKey
}&fmt=json&s=${symbols.join(',')}`, }&fmt=json&s=${eodHistoricalDataSymbols.join(',')}`,
{ {
// @ts-ignore // @ts-ignore
signal: abortController.signal signal: abortController.signal
@ -160,10 +164,12 @@ export class EodHistoricalDataService implements DataProviderInterface {
).json<any>(); ).json<any>();
const quotes = const quotes =
symbols.length === 1 ? [realTimeResponse] : realTimeResponse; eodHistoricalDataSymbols.length === 1
? [realTimeResponse]
: realTimeResponse;
const searchResponse = await Promise.all( const searchResponse = await Promise.all(
symbols eodHistoricalDataSymbols
.filter((symbol) => { .filter((symbol) => {
return !symbol.endsWith('.FOREX'); return !symbol.endsWith('.FOREX');
}) })
@ -176,7 +182,7 @@ export class EodHistoricalDataService implements DataProviderInterface {
return items[0]; return items[0];
}); });
const response = quotes.reduce( response = quotes.reduce(
( (
result: { [symbol: string]: IDataProviderResponse }, result: { [symbol: string]: IDataProviderResponse },
{ close, code, timestamp } { close, code, timestamp }

@ -113,13 +113,15 @@ export class FinancialModelingPrepService implements DataProviderInterface {
return DataSource.FINANCIAL_MODELING_PREP; return DataSource.FINANCIAL_MODELING_PREP;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
const results: { [symbol: string]: IDataProviderResponse } = {}; symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }> {
const response: { [symbol: string]: IDataProviderResponse } = {};
if (aSymbols.length <= 0) { if (symbols.length <= 0) {
return {}; return response;
} }
try { try {
@ -130,7 +132,7 @@ export class FinancialModelingPrepService implements DataProviderInterface {
}, DEFAULT_REQUEST_TIMEOUT); }, DEFAULT_REQUEST_TIMEOUT);
const response = await got( const response = await got(
`${this.URL}/quote/${aSymbols.join(',')}?apikey=${this.apiKey}`, `${this.URL}/quote/${symbols.join(',')}?apikey=${this.apiKey}`,
{ {
// @ts-ignore // @ts-ignore
signal: abortController.signal signal: abortController.signal
@ -138,7 +140,7 @@ export class FinancialModelingPrepService implements DataProviderInterface {
).json<any>(); ).json<any>();
for (const { price, symbol } of response) { for (const { price, symbol } of response) {
results[symbol] = { response[symbol] = {
currency: DEFAULT_CURRENCY, currency: DEFAULT_CURRENCY,
dataProviderInfo: this.getDataProviderInfo(), dataProviderInfo: this.getDataProviderInfo(),
dataSource: DataSource.FINANCIAL_MODELING_PREP, dataSource: DataSource.FINANCIAL_MODELING_PREP,
@ -150,7 +152,7 @@ export class FinancialModelingPrepService implements DataProviderInterface {
Logger.error(error, 'FinancialModelingPrepService'); Logger.error(error, 'FinancialModelingPrepService');
} }
return results; return response;
} }
public getTestSymbol() { public getTestSymbol() {

@ -99,18 +99,20 @@ export class GoogleSheetsService implements DataProviderInterface {
return DataSource.GOOGLE_SHEETS; return DataSource.GOOGLE_SHEETS;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
if (aSymbols.length <= 0) { symbols: string[];
return {}; }): Promise<{ [symbol: string]: IDataProviderResponse }> {
const response: { [symbol: string]: IDataProviderResponse } = {};
if (symbols.length <= 0) {
return response;
} }
try { try {
const response: { [symbol: string]: IDataProviderResponse } = {};
const symbolProfiles = await this.symbolProfileService.getSymbolProfiles( const symbolProfiles = await this.symbolProfileService.getSymbolProfiles(
aSymbols.map((symbol) => { symbols.map((symbol) => {
return { return {
symbol, symbol,
dataSource: this.getName() dataSource: this.getName()
@ -129,7 +131,7 @@ export class GoogleSheetsService implements DataProviderInterface {
const marketPrice = parseFloat(row['marketPrice']); const marketPrice = parseFloat(row['marketPrice']);
const symbol = row['symbol']; const symbol = row['symbol'];
if (aSymbols.includes(symbol)) { if (symbols.includes(symbol)) {
response[symbol] = { response[symbol] = {
marketPrice, marketPrice,
currency: symbolProfiles.find((symbolProfile) => { currency: symbolProfiles.find((symbolProfile) => {

@ -36,9 +36,11 @@ export interface DataProviderInterface {
getName(): DataSource; getName(): DataSource;
getQuotes( getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }>; }: {
symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }>;
getTestSymbol(): string; getTestSymbol(): string;

@ -133,18 +133,20 @@ export class ManualService implements DataProviderInterface {
return DataSource.MANUAL; return DataSource.MANUAL;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }> {
const response: { [symbol: string]: IDataProviderResponse } = {}; const response: { [symbol: string]: IDataProviderResponse } = {};
if (aSymbols.length <= 0) { if (symbols.length <= 0) {
return response; return response;
} }
try { try {
const symbolProfiles = await this.symbolProfileService.getSymbolProfiles( const symbolProfiles = await this.symbolProfileService.getSymbolProfiles(
aSymbols.map((symbol) => { symbols.map((symbol) => {
return { symbol, dataSource: this.getName() }; return { symbol, dataSource: this.getName() };
}) })
); );
@ -154,10 +156,10 @@ export class ManualService implements DataProviderInterface {
orderBy: { orderBy: {
date: 'desc' date: 'desc'
}, },
take: aSymbols.length, take: symbols.length,
where: { where: {
symbol: { symbol: {
in: aSymbols in: symbols
} }
} }
}); });

@ -87,15 +87,17 @@ export class RapidApiService implements DataProviderInterface {
return DataSource.RAPID_API; return DataSource.RAPID_API;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
if (aSymbols.length <= 0) { symbols: string[];
}): Promise<{ [symbol: string]: IDataProviderResponse }> {
if (symbols.length <= 0) {
return {}; return {};
} }
try { try {
const symbol = aSymbols[0]; const symbol = symbols[0];
if (symbol === ghostfolioFearAndGreedIndexSymbol) { if (symbol === ghostfolioFearAndGreedIndexSymbol) {
const fgi = await this.getFearAndGreedIndex(); const fgi = await this.getFearAndGreedIndex();

@ -156,20 +156,22 @@ export class YahooFinanceService implements DataProviderInterface {
return DataSource.YAHOO; return DataSource.YAHOO;
} }
public async getQuotes( public async getQuotes({
aSymbols: string[] symbols
): Promise<{ [symbol: string]: IDataProviderResponse }> { }: {
if (aSymbols.length <= 0) { symbols: string[];
return {}; }): Promise<{ [symbol: string]: IDataProviderResponse }> {
const response: { [symbol: string]: IDataProviderResponse } = {};
if (symbols.length <= 0) {
return response;
} }
const yahooFinanceSymbols = aSymbols.map((symbol) => const yahooFinanceSymbols = symbols.map((symbol) =>
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(symbol) this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(symbol)
); );
try { try {
const response: { [symbol: string]: IDataProviderResponse } = {};
let quotes: Pick< let quotes: Pick<
Quote, Quote,
'currency' | 'marketState' | 'regularMarketPrice' | 'symbol' 'currency' | 'marketState' | 'regularMarketPrice' | 'symbol'

Loading…
Cancel
Save