|
|
@ -26,6 +26,9 @@ import { |
|
|
FXMacroDataSourcesResponse |
|
|
FXMacroDataSourcesResponse |
|
|
} from './interfaces/interfaces'; |
|
|
} from './interfaces/interfaces'; |
|
|
|
|
|
|
|
|
|
|
|
const PAGE_SIZE = 100; |
|
|
|
|
|
const MAX_PAGES = 1000; |
|
|
|
|
|
|
|
|
@Injectable() |
|
|
@Injectable() |
|
|
export class FXMacroDataService implements DataProviderInterface { |
|
|
export class FXMacroDataService implements DataProviderInterface { |
|
|
private readonly baseUrl = 'https://api.fxmacrodata.com/v1'; |
|
|
private readonly baseUrl = 'https://api.fxmacrodata.com/v1'; |
|
|
@ -100,14 +103,16 @@ export class FXMacroDataService implements DataProviderInterface { |
|
|
|
|
|
|
|
|
// The endpoint caps a page at 100 rows and orders most-recent-first, so
|
|
|
// The endpoint caps a page at 100 rows and orders most-recent-first, so
|
|
|
// longer ranges are walked page by page rather than silently truncated.
|
|
|
// longer ranges are walked page by page rather than silently truncated.
|
|
|
for (let page = 1; ; page++) { |
|
|
let offset = 0; |
|
|
const { data } = await this.get<FXMacroDataForexResponse>({ |
|
|
|
|
|
|
|
|
for (let page = 0; page < MAX_PAGES; page++) { |
|
|
|
|
|
const { data, pagination } = await this.get<FXMacroDataForexResponse>({ |
|
|
requestTimeout, |
|
|
requestTimeout, |
|
|
path: `forex/${base.toLowerCase()}/${quote.toLowerCase()}`, |
|
|
path: `forex/${base.toLowerCase()}/${quote.toLowerCase()}`, |
|
|
searchParams: { |
|
|
searchParams: { |
|
|
end_date: format(to, DATE_FORMAT), |
|
|
end_date: format(to, DATE_FORMAT), |
|
|
limit: '100', |
|
|
limit: PAGE_SIZE.toString(), |
|
|
page: page.toString(), |
|
|
offset: offset.toString(), |
|
|
start_date: format(from, DATE_FORMAT) |
|
|
start_date: format(from, DATE_FORMAT) |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -124,9 +129,11 @@ export class FXMacroDataService implements DataProviderInterface { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (data.length < 100) { |
|
|
if (!pagination?.has_more) { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
offset = pagination.next_offset ?? offset + data.length; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return response; |
|
|
return response; |
|
|
|