Browse Source

Set up Ghostfolio data provider

pull/4016/head
Thomas Kaul 9 months ago
parent
commit
16ef4d27ee
  1. 64
      apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts

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

@ -15,7 +15,8 @@ import { PropertyService } from '@ghostfolio/api/services/property/property.serv
import { PROPERTY_API_KEY_GHOSTFOLIO } from '@ghostfolio/common/config'; import { PROPERTY_API_KEY_GHOSTFOLIO } from '@ghostfolio/common/config';
import { import {
DataProviderInfo, DataProviderInfo,
LookupResponse LookupResponse,
QuotesResponse
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
@ -84,11 +85,48 @@ export class GhostfolioService implements DataProviderInterface {
return DataSource.GHOSTFOLIO; return DataSource.GHOSTFOLIO;
} }
public async getQuotes({}: GetQuotesParams): Promise<{ public async getQuotes({
requestTimeout = this.configurationService.get('REQUEST_TIMEOUT'),
symbols
}: GetQuotesParams): Promise<{
[symbol: string]: IDataProviderResponse; [symbol: string]: IDataProviderResponse;
}> { }> {
// TODO let response: { [symbol: string]: IDataProviderResponse } = {};
return {};
if (symbols.length <= 0) {
return response;
}
try {
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, requestTimeout);
const { quotes } = await got(
`${this.URL}/v1/data-providers/ghostfolio/quotes?symbols=${symbols.join(',')}`,
{
headers: this.getRequestHeaders(),
// @ts-ignore
signal: abortController.signal
}
).json<QuotesResponse>();
response = quotes;
} catch (error) {
let message = error;
if (error?.code === 'ABORT_ERR') {
message = `RequestError: The operation to get the quotes was aborted because the request to the data provider took more than ${(
this.configurationService.get('REQUEST_TIMEOUT') / 1000
).toFixed(3)} seconds`;
}
Logger.error(message, 'GhostfolioService');
}
return response;
} }
public getTestSymbol() { public getTestSymbol() {
@ -108,20 +146,18 @@ export class GhostfolioService implements DataProviderInterface {
searchResult = await got( searchResult = await got(
`${this.URL}/v1/data-providers/ghostfolio/lookup?query=${query}`, `${this.URL}/v1/data-providers/ghostfolio/lookup?query=${query}`,
{ {
headers: { headers: this.getRequestHeaders(),
Authorization: `Bearer ${this.apiKey}`
},
// @ts-ignore // @ts-ignore
signal: abortController.signal signal: abortController.signal
} }
).json<any>(); ).json<LookupResponse>();
} catch (error) { } catch (error) {
let message = error; let message = error;
if (error?.code === 'ABORT_ERR') { if (error?.code === 'ABORT_ERR') {
message = `RequestError: The operation to search for ${query} was aborted because the request to the data provider took more than ${this.configurationService.get( message = `RequestError: The operation to search for ${query} was aborted because the request to the data provider took more than ${(
'REQUEST_TIMEOUT' this.configurationService.get('REQUEST_TIMEOUT') / 1000
)}ms`; ).toFixed(3)} seconds`;
} }
Logger.error(message, 'GhostfolioService'); Logger.error(message, 'GhostfolioService');
@ -129,4 +165,10 @@ export class GhostfolioService implements DataProviderInterface {
return searchResult; return searchResult;
} }
private getRequestHeaders() {
return {
Authorization: `Bearer ${this.apiKey}`
};
}
} }

Loading…
Cancel
Save