refactor data-provider.service.ts to have dynamic list of services (#429)

* Refactor data-provider.service.ts to have dynamic list of services

* Update changelog

Co-authored-by: Valentin Zickner <ghostfolio@zickner.ch>
Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com>
pull/430/head
Valentin Zickner 3 years ago committed by GitHub
parent e3cd99f5d2
commit 099ad18aaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Changed
- Changed the data provider service to handle a dynamic list of services
## 1.63.0 - 19.10.2021
### Added

@ -72,15 +72,7 @@ describe('CurrentRateService', () => {
let marketDataService: MarketDataService;
beforeAll(async () => {
dataProviderService = new DataProviderService(
null,
null,
[],
null,
null,
null,
null
);
dataProviderService = new DataProviderService(null, [], [], null);
exchangeRateDataService = new ExchangeRateDataService(null, null);
marketDataService = new MarketDataService(null);

@ -84,6 +84,10 @@ export class AlphaVantageService implements DataProviderInterface {
}
}
public getName(): DataSource {
return DataSource.ALPHA_VANTAGE;
}
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
const result = await this.alphaVantage.data.search(aSymbol);

@ -22,6 +22,26 @@ import { DataProviderService } from './data-provider.service';
inject: [TrackinsightDataEnhancerService],
provide: 'DataEnhancers',
useFactory: (trackinsight) => [trackinsight]
},
{
inject: [
AlphaVantageService,
GhostfolioScraperApiService,
RakutenRapidApiService,
YahooFinanceService
],
provide: 'DataProviderInterfaces',
useFactory: (
alphaVantageService,
ghostfolioScraperApiService,
rakutenRapidApiService,
yahooFinanceService
) => [
alphaVantageService,
ghostfolioScraperApiService,
rakutenRapidApiService,
yahooFinanceService
]
}
],
exports: [DataProviderService, GhostfolioScraperApiService]

@ -13,26 +13,18 @@ import { Inject, Injectable } from '@nestjs/common';
import { DataSource, MarketData } from '@prisma/client';
import { format } from 'date-fns';
import { isEmpty } from 'lodash';
import { AlphaVantageService } from './alpha-vantage/alpha-vantage.service';
import { GhostfolioScraperApiService } from './ghostfolio-scraper-api/ghostfolio-scraper-api.service';
import { RakutenRapidApiService } from './rakuten-rapid-api/rakuten-rapid-api.service';
import { YahooFinanceService } from './yahoo-finance/yahoo-finance.service';
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
@Injectable()
export class DataProviderService {
public constructor(
private readonly alphaVantageService: AlphaVantageService,
private readonly configurationService: ConfigurationService,
@Inject('DataEnhancers')
private readonly dataEnhancers: DataEnhancerInterface[],
private readonly ghostfolioScraperApiService: GhostfolioScraperApiService,
private readonly prismaService: PrismaService,
private readonly rakutenRapidApiService: RakutenRapidApiService,
private readonly yahooFinanceService: YahooFinanceService
) {
this.rakutenRapidApiService?.setPrisma(this.prismaService);
}
@Inject('DataProviderInterfaces')
private readonly dataProviderInterfaces: DataProviderInterface[],
private readonly prismaService: PrismaService
) {}
public async get(items: IDataGatheringItem[]): Promise<{
[symbol: string]: IDataProviderResponse;
@ -204,17 +196,11 @@ export class DataProviderService {
}
private getDataProvider(providerName: DataSource) {
switch (providerName) {
case DataSource.ALPHA_VANTAGE:
return this.alphaVantageService;
case DataSource.GHOSTFOLIO:
return this.ghostfolioScraperApiService;
case DataSource.RAKUTEN:
return this.rakutenRapidApiService;
case DataSource.YAHOO:
return this.yahooFinanceService;
default:
throw new Error('No data provider has been found.');
for (const dataProviderInterface of this.dataProviderInterfaces) {
if (dataProviderInterface.getName() === providerName) {
return dataProviderInterface;
}
}
throw new Error('No data provider has been found.');
}
}

@ -144,6 +144,10 @@ export class GhostfolioScraperApiService implements DataProviderInterface {
return [];
}
public getName(): DataSource {
return DataSource.GHOSTFOLIO;
}
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
return { items: [] };
}

@ -5,6 +5,7 @@ import {
IDataProviderHistoricalResponse,
IDataProviderResponse
} from '../../interfaces/interfaces';
import { DataSource } from '@prisma/client';
export interface DataProviderInterface {
canHandle(symbol: string): boolean;
@ -20,5 +21,7 @@ export interface DataProviderInterface {
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
}>;
getName(): DataSource;
search(aSymbol: string): Promise<{ items: LookupItem[] }>;
}

@ -25,10 +25,9 @@ import { DataProviderInterface } from '../interfaces/data-provider.interface';
export class RakutenRapidApiService implements DataProviderInterface {
public static FEAR_AND_GREED_INDEX_NAME = 'Fear & Greed Index';
private prismaService: PrismaService;
public constructor(
private readonly configurationService: ConfigurationService
private readonly configurationService: ConfigurationService,
private readonly prismaService: PrismaService
) {}
public canHandle(symbol: string) {
@ -134,12 +133,12 @@ export class RakutenRapidApiService implements DataProviderInterface {
return {};
}
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
return { items: [] };
public getName(): DataSource {
return DataSource.RAKUTEN;
}
public setPrisma(aPrismaService: PrismaService) {
this.prismaService = aPrismaService;
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
return { items: [] };
}
private async getFearAndGreedIndex(): Promise<{

@ -171,6 +171,10 @@ export class YahooFinanceService implements DataProviderInterface {
}
}
public getName(): DataSource {
return DataSource.YAHOO;
}
public async search(aSymbol: string): Promise<{ items: LookupItem[] }> {
const items: LookupItem[] = [];

Loading…
Cancel
Save