From 4112b22faff1e01bca66e6afa76dbe0798935982 Mon Sep 17 00:00:00 2001 From: Akiomi Kamakura Date: Wed, 20 Oct 2021 18:11:54 +0900 Subject: [PATCH 1/2] Add /v3/bulk/quarter support to API client --- src/api/v3/client.test.ts | 23 +++++++++++++++++++++++ src/api/v3/client.ts | 15 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/api/v3/client.test.ts b/src/api/v3/client.test.ts index 1c8e860..39aafff 100644 --- a/src/api/v3/client.test.ts +++ b/src/api/v3/client.test.ts @@ -5,7 +5,9 @@ import { HttpError } from '~/api/http-error' import { useMockedUrlFetchApp } from '~/api/test-helper' import { BuffettCodeApiClientV3 } from '~/api/v3/client' import { DateParam } from '~/fiscal-periods/date-param' +import { YearQuarter } from '~/fiscal-periods/year-quarter' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' +import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range' describe('BuffettCodeApiClientV3', () => { test('HttpError#isInvalidTestingRequest', () => { @@ -110,6 +112,27 @@ describe('BuffettCodeApiClientV3', () => { }) }) + test('bulkQuarter', () => { + const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(quarter)) + + const client = new BuffettCodeApiClientV3('foo') + const ticker = '2371' + const range = new YearQuarterRange( + new YearQuarter(2018, 1), + new YearQuarter(2018, 4) + ) + expect(client.bulkQuarter(ticker, range)).toEqual(quarter['data']) + expect(mockFetch.mock.calls.length).toBe(1) + expect(mockFetch.mock.calls[0].length).toBe(2) + expect(mockFetch.mock.calls[0][0]).toBe( + 'https://api.buffett-code.com/api/v3/bulk/quarter?ticker=2371&from=2018Q1&to=2018Q4' + ) + expect(mockFetch.mock.calls[0][1]).toEqual({ + headers: { 'x-api-key': 'foo' }, + muteHttpExceptions: true + }) + }) + test('ondemandQuarter', () => { const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(quarter)) diff --git a/src/api/v3/client.ts b/src/api/v3/client.ts index 03530e6..2f32ce5 100644 --- a/src/api/v3/client.ts +++ b/src/api/v3/client.ts @@ -2,6 +2,7 @@ import { HttpError } from '~/api/http-error' import { UrlBuilder } from '~/api/url-builder' import { DateParam } from '~/fiscal-periods/date-param' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' +import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range' export class BuffettCodeApiClientV3 { static readonly baseUrl = 'https://api.buffett-code.com/api/v3' @@ -69,6 +70,20 @@ export class BuffettCodeApiClientV3 { return res['data'] } + public bulkQuarter(ticker: string, range: YearQuarterRange): object[] { + const endpoint = BuffettCodeApiClientV3.baseUrl + '/bulk/quarter' + const builder = new UrlBuilder(endpoint, { + ticker, + from: range.from.toString(), + to: range.to.toString() + }) + const url = builder.toString() + const options = this.defaultOptions() + + const res = BuffettCodeApiClientV3.request(url, options) + return res['data'] + } + public ondemandQuarter(ticker: string, period: YearQuarterParam): object { const endpoint = BuffettCodeApiClientV3.baseUrl + '/ondemand/quarter' const builder = new UrlBuilder(endpoint, { From f77bc26eb77d5e7c2380d3dc23e731a477a86204 Mon Sep 17 00:00:00 2001 From: Akiomi Kamakura Date: Wed, 20 Oct 2021 18:12:17 +0900 Subject: [PATCH 2/2] Add /v3/bulk/daily support to API client --- src/api/v3/client.test.ts | 19 +++++++++++++++++++ src/api/v3/client.ts | 15 +++++++++++++++ src/fiscal-periods/date-range.test.ts | 19 +++++++++++++++++++ src/fiscal-periods/date-range.ts | 11 +++++++++++ 4 files changed, 64 insertions(+) create mode 100644 src/fiscal-periods/date-range.test.ts create mode 100644 src/fiscal-periods/date-range.ts diff --git a/src/api/v3/client.test.ts b/src/api/v3/client.test.ts index 39aafff..8f65f96 100644 --- a/src/api/v3/client.test.ts +++ b/src/api/v3/client.test.ts @@ -5,6 +5,7 @@ import { HttpError } from '~/api/http-error' import { useMockedUrlFetchApp } from '~/api/test-helper' import { BuffettCodeApiClientV3 } from '~/api/v3/client' import { DateParam } from '~/fiscal-periods/date-param' +import { DateRange } from '~/fiscal-periods/date-range' import { YearQuarter } from '~/fiscal-periods/year-quarter' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range' @@ -169,6 +170,24 @@ describe('BuffettCodeApiClientV3', () => { }) }) + test('bulkDaily', () => { + const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(daily)) + + const client = new BuffettCodeApiClientV3('foo') + const ticker = '2371' + const range = new DateRange(new Date('2021-08-11'), new Date('2021-08-17')) + expect(client.bulkDaily(ticker, range)).toEqual(daily['data']) + expect(mockFetch.mock.calls.length).toBe(1) + expect(mockFetch.mock.calls[0].length).toBe(2) + expect(mockFetch.mock.calls[0][0]).toBe( + 'https://api.buffett-code.com/api/v3/bulk/daily?ticker=2371&from=2021-08-11&to=2021-08-17' + ) + expect(mockFetch.mock.calls[0][1]).toEqual({ + headers: { 'x-api-key': 'foo' }, + muteHttpExceptions: true + }) + }) + test('ondemandDaily', () => { const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(daily)) diff --git a/src/api/v3/client.ts b/src/api/v3/client.ts index 2f32ce5..765a587 100644 --- a/src/api/v3/client.ts +++ b/src/api/v3/client.ts @@ -1,6 +1,7 @@ import { HttpError } from '~/api/http-error' import { UrlBuilder } from '~/api/url-builder' import { DateParam } from '~/fiscal-periods/date-param' +import { DateRange } from '~/fiscal-periods/date-range' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range' @@ -111,6 +112,20 @@ export class BuffettCodeApiClientV3 { return res['data'] } + public bulkDaily(ticker: string, range: DateRange): object[] { + const endpoint = BuffettCodeApiClientV3.baseUrl + '/bulk/daily' + const builder = new UrlBuilder(endpoint, { + ticker, + from: new DateParam(range.from).toString(), + to: new DateParam(range.to).toString() + }) + const url = builder.toString() + const options = this.defaultOptions() + + const res = BuffettCodeApiClientV3.request(url, options) + return res['data'] + } + public ondemandDaily(ticker: string, date: DateParam): object { const endpoint = BuffettCodeApiClientV3.baseUrl + '/ondemand/daily' const builder = new UrlBuilder(endpoint, { diff --git a/src/fiscal-periods/date-range.test.ts b/src/fiscal-periods/date-range.test.ts new file mode 100644 index 0000000..591a361 --- /dev/null +++ b/src/fiscal-periods/date-range.test.ts @@ -0,0 +1,19 @@ +import { DateRange } from '~/fiscal-periods/date-range' + +test('diff', () => { + expect( + new DateRange(new Date(2020, 9, 6), new Date(2021, 9, 6)).diff() + ).toEqual(365) + expect( + new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 7)).diff() + ).toEqual(1) + expect( + new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 6)).diff() + ).toEqual(0) + expect( + new DateRange(new Date(2020, 9, 6), new Date(2020, 9, 5)).diff() + ).toEqual(-1) + expect( + new DateRange(new Date(2020, 9, 6), new Date(2019, 9, 6)).diff() + ).toEqual(-366) +}) diff --git a/src/fiscal-periods/date-range.ts b/src/fiscal-periods/date-range.ts new file mode 100644 index 0000000..b0ecbae --- /dev/null +++ b/src/fiscal-periods/date-range.ts @@ -0,0 +1,11 @@ +export class DateRange { + constructor(readonly from: Date, readonly to: Date) { + // + } + + diff(): number { + return Math.round( + (this.to.getTime() - this.from.getTime()) / (1000 * 60 * 60 * 24) + ) + } +}