Browse Source

Refactoring

pull/5413/head
Thomas Kaul 4 months ago
parent
commit
5b2c925724
  1. 79
      apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts

79
apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts

@ -51,10 +51,10 @@ export class GhostfolioService implements DataProviderInterface {
requestTimeout = this.configurationService.get('REQUEST_TIMEOUT'), requestTimeout = this.configurationService.get('REQUEST_TIMEOUT'),
symbol symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> { }: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
let response: DataProviderGhostfolioAssetProfileResponse; let assetProfile: DataProviderGhostfolioAssetProfileResponse;
try { try {
const res = await fetch( const response = await fetch(
`${this.URL}/v1/data-providers/ghostfolio/asset-profile/${symbol}`, `${this.URL}/v1/data-providers/ghostfolio/asset-profile/${symbol}`,
{ {
headers: await this.getRequestHeaders(), headers: await this.getRequestHeaders(),
@ -62,16 +62,15 @@ export class GhostfolioService implements DataProviderInterface {
} }
); );
if (!res.ok) { if (!response.ok) {
throw new Response(await res.text(), { throw new Response(await response.text(), {
status: res.status, status: response.status,
statusText: res.statusText statusText: response.statusText
}); });
} }
const assetProfile = assetProfile =
(await res.json()) as DataProviderGhostfolioAssetProfileResponse; (await response.json()) as DataProviderGhostfolioAssetProfileResponse;
response = assetProfile;
} catch (error) { } catch (error) {
let message = error; let message = error;
@ -93,7 +92,7 @@ export class GhostfolioService implements DataProviderInterface {
Logger.error(message, 'GhostfolioService'); Logger.error(message, 'GhostfolioService');
} }
return response; return assetProfile;
} }
public getDataProviderInfo(): DataProviderInfo { public getDataProviderInfo(): DataProviderInfo {
@ -114,12 +113,12 @@ export class GhostfolioService implements DataProviderInterface {
}: GetDividendsParams): Promise<{ }: GetDividendsParams): Promise<{
[date: string]: IDataProviderHistoricalResponse; [date: string]: IDataProviderHistoricalResponse;
}> { }> {
let response: { let dividends: {
[date: string]: IDataProviderHistoricalResponse; [date: string]: IDataProviderHistoricalResponse;
} = {}; } = {};
try { try {
const res = await fetch( const response = await fetch(
`${this.URL}/v2/data-providers/ghostfolio/dividends/${symbol}?from=${format(from, DATE_FORMAT)}&granularity=${granularity}&to=${format( `${this.URL}/v2/data-providers/ghostfolio/dividends/${symbol}?from=${format(from, DATE_FORMAT)}&granularity=${granularity}&to=${format(
to, to,
DATE_FORMAT DATE_FORMAT
@ -130,15 +129,14 @@ export class GhostfolioService implements DataProviderInterface {
} }
); );
if (!res.ok) { if (!response.ok) {
throw new Response(await res.text(), { throw new Response(await response.text(), {
status: res.status, status: response.status,
statusText: res.statusText statusText: response.statusText
}); });
} }
const { dividends } = (await res.json()) as DividendsResponse; dividends = ((await response.json()) as DividendsResponse).dividends;
response = dividends;
} catch (error) { } catch (error) {
let message = error; let message = error;
@ -156,7 +154,7 @@ export class GhostfolioService implements DataProviderInterface {
Logger.error(message, 'GhostfolioService'); Logger.error(message, 'GhostfolioService');
} }
return response; return dividends;
} }
public async getHistorical({ public async getHistorical({
@ -169,7 +167,7 @@ export class GhostfolioService implements DataProviderInterface {
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; [symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
}> { }> {
try { try {
const res = await fetch( const response = await fetch(
`${this.URL}/v2/data-providers/ghostfolio/historical/${symbol}?from=${format(from, DATE_FORMAT)}&granularity=${granularity}&to=${format( `${this.URL}/v2/data-providers/ghostfolio/historical/${symbol}?from=${format(from, DATE_FORMAT)}&granularity=${granularity}&to=${format(
to, to,
DATE_FORMAT DATE_FORMAT
@ -180,14 +178,14 @@ export class GhostfolioService implements DataProviderInterface {
} }
); );
if (!res.ok) { if (!response.ok) {
throw new Response(await res.text(), { throw new Response(await response.text(), {
status: res.status, status: response.status,
statusText: res.statusText statusText: response.statusText
}); });
} }
const { historicalData } = (await res.json()) as HistoricalResponse; const { historicalData } = (await response.json()) as HistoricalResponse;
return { return {
[symbol]: historicalData [symbol]: historicalData
@ -232,14 +230,14 @@ export class GhostfolioService implements DataProviderInterface {
}: GetQuotesParams): Promise<{ }: GetQuotesParams): Promise<{
[symbol: string]: IDataProviderResponse; [symbol: string]: IDataProviderResponse;
}> { }> {
let response: { [symbol: string]: IDataProviderResponse } = {}; let quotes: { [symbol: string]: IDataProviderResponse } = {};
if (symbols.length <= 0) { if (symbols.length <= 0) {
return response; return quotes;
} }
try { try {
const res = await fetch( const response = await fetch(
`${this.URL}/v2/data-providers/ghostfolio/quotes?symbols=${symbols.join(',')}`, `${this.URL}/v2/data-providers/ghostfolio/quotes?symbols=${symbols.join(',')}`,
{ {
headers: await this.getRequestHeaders(), headers: await this.getRequestHeaders(),
@ -247,15 +245,14 @@ export class GhostfolioService implements DataProviderInterface {
} }
); );
if (!res.ok) { if (!response.ok) {
throw new Response(await res.text(), { throw new Response(await response.text(), {
status: res.status, status: response.status,
statusText: res.statusText statusText: response.statusText
}); });
} }
const { quotes } = (await res.json()) as QuotesResponse; quotes = ((await response.json()) as QuotesResponse).quotes;
response = quotes;
} catch (error) { } catch (error) {
let message = error; let message = error;
@ -279,7 +276,7 @@ export class GhostfolioService implements DataProviderInterface {
Logger.error(message, 'GhostfolioService'); Logger.error(message, 'GhostfolioService');
} }
return response; return quotes;
} }
public getTestSymbol() { public getTestSymbol() {
@ -293,7 +290,7 @@ export class GhostfolioService implements DataProviderInterface {
let searchResult: LookupResponse = { items: [] }; let searchResult: LookupResponse = { items: [] };
try { try {
const res = await fetch( const response = await fetch(
`${this.URL}/v2/data-providers/ghostfolio/lookup?query=${query}`, `${this.URL}/v2/data-providers/ghostfolio/lookup?query=${query}`,
{ {
headers: await this.getRequestHeaders(), headers: await this.getRequestHeaders(),
@ -301,14 +298,14 @@ export class GhostfolioService implements DataProviderInterface {
} }
); );
if (!res.ok) { if (!response.ok) {
throw new Response(await res.text(), { throw new Response(await response.text(), {
status: res.status, status: response.status,
statusText: res.statusText statusText: response.statusText
}); });
} }
searchResult = (await res.json()) as LookupResponse; searchResult = (await response.json()) as LookupResponse;
} catch (error) { } catch (error) {
let message = error; let message = error;

Loading…
Cancel
Save