From dbf2ece3834052d118c18a1c2c25f5187932e7a8 Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Mon, 1 Nov 2021 19:02:16 +0900 Subject: [PATCH 1/7] wip: firster csv --- BuffettCodeAPIClient/ApiClientCore.cs | 13 +++- .../ApiClientCoreWithCache.cs | 9 ++- .../BuffettCodeApiV2Client.cs | 8 +-- .../BuffettCodeApiV2ClientTests.cs | 8 +-- .../BuffettCodeApiV3ClientTests.cs | 2 +- .../CsvDownload/ApiResourceGetter.cs | 8 ++- BuffettCodeAddinRibbon/CsvDownloadForm.cs | 8 ++- .../SettingForm.Designer.cs | 8 +-- BuffettCodeAddinRibbon/SettingForm.cs | 8 +-- .../CsvDownload/ApiResourceGetterTests.cs | 2 +- BuffettCodeIO/ApiTaskHelper.cs | 17 ++--- BuffettCodeIO/BuffettCodeApiTaskProcessor.cs | 4 +- .../Resolver/PeriodSupportedTierResolver.cs | 12 ++-- BuffettCodeIOTests/ApiTaskHelperTests.cs | 64 +++++++++---------- .../PeriodSupportedTierResolverTests.cs | 31 +++------ 15 files changed, 102 insertions(+), 100 deletions(-) diff --git a/BuffettCodeAPIClient/ApiClientCore.cs b/BuffettCodeAPIClient/ApiClientCore.cs index 3ce9f6a..afda487 100644 --- a/BuffettCodeAPIClient/ApiClientCore.cs +++ b/BuffettCodeAPIClient/ApiClientCore.cs @@ -11,7 +11,7 @@ namespace BuffettCodeAPIClient /// BuffettCode API と Http でやり取りする Client のコアクラス /// - public class ApiClientCore + public class ApiClientCore : IDisposable { public string ApiKey { set; get; } private readonly Uri baseUri; @@ -58,10 +58,17 @@ public async Task Get(ApiGetRequest request, bool isConfigureAwait) throw new BuffettCodeApiClientException(); } } - // to do : waiting too long to read as str in csv download - return await response.Content.ReadAsStringAsync().ConfigureAwait(isConfigureAwait); + var content = response.Content.ReadAsStringAsync().Result; + return content; } } + + public void Dispose() + { + httpClient.Dispose(); + } } + + } \ No newline at end of file diff --git a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs index 45fe18b..05eae49 100644 --- a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs +++ b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs @@ -31,15 +31,18 @@ public void UpdateApiKey(string apikey) } public string GetApiKey() => this.apiClientCore.ApiKey; + public async Task Get(ApiGetRequest request, bool isConfigureAwait, bool useCache) { if (useCache && cacheHelper.HasCache(request)) { return (string)cacheHelper.Get(request); } - var response = await apiClientCore.Get(request, isConfigureAwait); - cacheHelper.Set(request, response); - return response; + else + { + return apiClientCore.Get(request, isConfigureAwait).Result; + } } + } } \ No newline at end of file diff --git a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs index c3694db..24d1b6d 100644 --- a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs +++ b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs @@ -28,8 +28,8 @@ private BuffettCodeApiV2Client() public async Task GetQuarter(string ticker, FiscalQuarterPeriod period, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetQuarterRequest(ticker, period, useOndemand); - var response = await apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response); + var response = apiClientCore.Get(request, isConfigureAwait, useCache); + return ApiGetResponseBodyParser.Parse(response.Result); } public async Task GetIndicator(string ticker, bool isConfigureAwait = true, bool useCache = true) @@ -49,8 +49,8 @@ public async Task GetQuarterRange(string ticker, FiscalQuarterPeriod fr public async Task GetCompany(string ticker, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetCompanyRequest(ticker); - var response = await apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response); + var response = apiClientCore.Get(request, isConfigureAwait, useCache); + return ApiGetResponseBodyParser.Parse(response.Result); } public void UpdateApiKey(string apiKey) => apiClientCore.UpdateApiKey(apiKey); diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs index 04bcb6c..86264a0 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs @@ -26,7 +26,7 @@ public void GetQuarterTest() { var period = FiscalQuarterPeriod.Create(2019, 4); // test api key can get ticker=xx01 - Assert.IsNotNull(client.GetQuarter("6501", period, false, true, false).Result); + Assert.IsNotNull(client.GetQuarter("6501", period, false, true, false)); // test api key can get ticker=xx02 Assert.ThrowsExceptionAsync(() => client.GetQuarter("6502", period, false, true, false)); @@ -36,7 +36,7 @@ public void GetQuarterTest() public void GetIndicatorTest() { // test api key can get ticker=xx01 - Assert.IsNotNull(client.GetIndicator("6501", true).Result); + Assert.IsNotNull(client.GetIndicator("6501", true)); // test api key can get ticker=xx02 Assert.ThrowsExceptionAsync(() => client.GetIndicator("6502", true, false)); @@ -49,7 +49,7 @@ public void GetQuarterRangeTest() var to = FiscalQuarterPeriod.Create(2019, 4); // test api key can get ticker=xx01 - Assert.IsNotNull(client.GetQuarterRange("6501", from, to, true, false).Result); + Assert.IsNotNull(client.GetQuarterRange("6501", from, to, false, false)); // test api key can get ticker=xx02 Assert.ThrowsExceptionAsync(() => client.GetQuarterRange("6502", from, to, true, false)); @@ -59,7 +59,7 @@ public void GetQuarterRangeTest() public void GetCompanyTest() { // test api key can get ticker=xx01 - Assert.IsNotNull(client.GetCompany("6501", true, true).Result); + Assert.IsNotNull(client.GetCompany("6501", true, false)); // test api key can get ticker=xx02 Assert.ThrowsExceptionAsync(() => client.GetCompany("6502", true, false)); diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs index 56e568e..19c697d 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs @@ -25,7 +25,7 @@ public void GetDailyTest() { // test api key can get 01 data var day = DayPeriod.Create(2021, 2, 1); - Assert.IsNotNull(client.GetDaily("6501", day, false, true, false).Result); + Assert.IsNotNull(client.GetDaily("6501", day, false, true, false)); // test api key can get not 01 Assert.ThrowsExceptionAsync diff --git a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs index b2948c1..cfdb5a1 100644 --- a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs +++ b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs @@ -6,6 +6,7 @@ using BuffettCodeIO; using BuffettCodeIO.Property; using BuffettCodeAddinRibbon.Settings; +using System.Threading.Tasks; namespace BuffettCodeAddinRibbon.CsvDownload { @@ -29,11 +30,14 @@ public static ApiResourceGetter Create() return Create(Configuration.GetInstance()); } - public IEnumerable GetQuarters(CsvDownloadParameters parameters) + public async Task> GetQuarters(CsvDownloadParameters parameters) { + + // set isConfigureAwait for performance + // https://devblogs.microsoft.com/dotnet/configureawait-faq/#why-would-i-want-to-use-configureawaitfalse var quarters = PeriodRange.Slice(parameters.Range, 12) .SelectMany - (r => processor.GetApiResources(DataTypeConfig.Quarter, parameters.Ticker, r.From, r.To, true, true) + (r => processor.GetApiResources(DataTypeConfig.Quarter, parameters.Ticker, r.From, r.To, false, true) ).Cast(); return quarters.Distinct().OrderBy(q => q.Period); } diff --git a/BuffettCodeAddinRibbon/CsvDownloadForm.cs b/BuffettCodeAddinRibbon/CsvDownloadForm.cs index f33db46..a11fc48 100644 --- a/BuffettCodeAddinRibbon/CsvDownloadForm.cs +++ b/BuffettCodeAddinRibbon/CsvDownloadForm.cs @@ -20,6 +20,7 @@ public partial class CsvDownloadForm : Form private static readonly int upperLimitYear = DateTime.Today.Year; private readonly TabularWriterBuilder quarterTabularWriterBuilder = new TabularWriterBuilder(); + private readonly ApiResourceGetter apiResourceGetter = ApiResourceGetter.Create(); public CsvDownloadForm() { @@ -60,7 +61,7 @@ private void ButtonOK_Click(object sender, EventArgs e) Execute(); } - private void Execute() + private async void Execute() { if (!ValidateControls()) { @@ -69,7 +70,8 @@ private void Execute() try { var parameters = CreateParametersFromFormValues(); - var quarters = ApiResourceGetter.Create().GetQuarters(parameters).ToList(); + var resources = await apiResourceGetter.GetQuarters(parameters); + var quarters = resources.ToList(); if (quarters.Count == 0) { MessageBox.Show("条件に当てはまる財務データがありませんでした。", "CSV出力", MessageBoxButtons.OK); @@ -81,6 +83,7 @@ private void Execute() writer.Write(tabular); MessageBox.Show("財務データの取得が完了しました", "CSV出力", MessageBoxButtons.OK); } + DialogResult = DialogResult.OK; } catch (TestAPIConstraintException) { @@ -110,7 +113,6 @@ private void Execute() { MessageBox.Show("データの取得中にエラーが発生しました。", "CSV出力", MessageBoxButtons.OK); } - DialogResult = DialogResult.OK; Close(); } diff --git a/BuffettCodeAddinRibbon/SettingForm.Designer.cs b/BuffettCodeAddinRibbon/SettingForm.Designer.cs index d858c74..d0c0183 100644 --- a/BuffettCodeAddinRibbon/SettingForm.Designer.cs +++ b/BuffettCodeAddinRibbon/SettingForm.Designer.cs @@ -107,7 +107,7 @@ private void InitializeComponent() this.apiSpecialNotesLink.TabIndex = 7; this.apiSpecialNotesLink.TabStop = true; this.apiSpecialNotesLink.Text = "バフェットコード WEB API機能に関する特記事項"; - this.apiSpecialNotesLink.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.apiSpecialNotes_LinkClicked); + this.apiSpecialNotesLink.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.ApiSpecialNotes_LinkClicked); // // ondemandModeDesc // @@ -121,7 +121,7 @@ private void InitializeComponent() this.ondemandModeDesc.Size = new System.Drawing.Size(507, 235); this.ondemandModeDesc.TabIndex = 6; this.ondemandModeDesc.Text = resources.GetString("ondemandModeDesc.Text"); - this.ondemandModeDesc.TextChanged += new System.EventHandler(this.ondemandModeDescDesc_TextChanged); + this.ondemandModeDesc.TextChanged += new System.EventHandler(this.OndemandModeDescDesc_TextChanged); // // checkIsOndemandEndpointEnabled // @@ -148,7 +148,7 @@ private void InitializeComponent() this.textAPIKey.Name = "textAPIKey"; this.textAPIKey.Size = new System.Drawing.Size(518, 25); this.textAPIKey.TabIndex = 2; - this.textAPIKey.TextChanged += new System.EventHandler(this.textAPIKey_TextChanged); + this.textAPIKey.TextChanged += new System.EventHandler(this.TextAPIKey_TextChanged); // // tabDeveloper // @@ -213,7 +213,7 @@ private void InitializeComponent() this.ondemandUsageEntryLink.TabIndex = 7; this.ondemandUsageEntryLink.TabStop = true; this.ondemandUsageEntryLink.Text = "Web API従量課金エンドポイントのご利用にあたって"; - this.ondemandUsageEntryLink.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.ondemandUsageEntryLink_LinkClicked); + this.ondemandUsageEntryLink.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.OndemandUsageEntryLink_LinkClicked); // // SettingForm // diff --git a/BuffettCodeAddinRibbon/SettingForm.cs b/BuffettCodeAddinRibbon/SettingForm.cs index 90fd8b8..b88efc0 100644 --- a/BuffettCodeAddinRibbon/SettingForm.cs +++ b/BuffettCodeAddinRibbon/SettingForm.cs @@ -85,17 +85,17 @@ private void TabAPI_Click(object sender, EventArgs e) } - private void textAPIKey_TextChanged(object sender, EventArgs e) + private void TextAPIKey_TextChanged(object sender, EventArgs e) { } - private void ondemandModeDescDesc_TextChanged(object sender, EventArgs e) + private void OndemandModeDescDesc_TextChanged(object sender, EventArgs e) { } - private void apiSpecialNotes_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + private void ApiSpecialNotes_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // mark as visited apiSpecialNotesLink.LinkVisited = true; @@ -104,7 +104,7 @@ private void apiSpecialNotes_LinkClicked(object sender, LinkLabelLinkClickedEven } - private void ondemandUsageEntryLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + private void OndemandUsageEntryLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // mark as visited ondemandUsageEntryLink.LinkVisited = true; diff --git a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs index 33c5f3d..ad66946 100644 --- a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs +++ b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs @@ -38,7 +38,7 @@ public void GetQuartersTest() var to = FiscalQuarterPeriod.Create(2020, 2); var outputSettings = CsvDownloadOutputSettings.Create(TabularOutputEncoding.SJIS, TabularOutputDestination.NewWorksheet); var parameters = CsvDownloadParameters.Create(ticker, from, to, outputSettings); - var quarters = ApiResourceGetter.Create(Configuration.GetInstance()).GetQuarters(parameters).ToArray(); + var quarters = ApiResourceGetter.Create(Configuration.GetInstance()).GetQuarters(parameters).Result.ToArray(); var quarter2020Q1 = quarters[0]; var quarter2020Q2 = quarters[1]; Assert.AreEqual(2, quarters.Length); diff --git a/BuffettCodeIO/ApiTaskHelper.cs b/BuffettCodeIO/ApiTaskHelper.cs index a0dc3cf..947e772 100644 --- a/BuffettCodeIO/ApiTaskHelper.cs +++ b/BuffettCodeIO/ApiTaskHelper.cs @@ -14,9 +14,10 @@ public ApiTaskHelper(PeriodSupportedTierResolver tierResolver) this.tierResolver = tierResolver; } - public SupportedTier FindAvailableTier(DataTypeConfig dataType, string ticker, IPeriod period, bool IsOndemandEndpointEnabled) + public SupportedTier FindAvailableTier(DataTypeConfig dataType, string ticker, IPeriod period, bool IsOndemandEndpointEnabled, bool isConfigureAwait, bool useCache) { - var tier = tierResolver.Resolve(dataType, ticker, period); + var tier = tierResolver.Resolve(dataType, ticker, period, + isConfigureAwait, useCache); switch (tier) { case SupportedTier.None: @@ -37,14 +38,14 @@ public SupportedTier FindAvailableTier(DataTypeConfig dataType, string ticker, I } } - public bool ShouldUseOndemandEndpoint(DataTypeConfig dataType, string ticker, IPeriod period, bool IsOndemandEndpointEnabled) + public bool ShouldUseOndemandEndpoint(DataTypeConfig dataType, string ticker, IPeriod period, bool IsOndemandEndpointEnabled, bool isConfigureAwait, bool useCache) => - FindAvailableTier(dataType, ticker, period, IsOndemandEndpointEnabled).Equals(SupportedTier.OndemandTier); + FindAvailableTier(dataType, ticker, period, IsOndemandEndpointEnabled, isConfigureAwait, useCache).Equals(SupportedTier.OndemandTier); - public IComparablePeriod FindEndOfOndemandPeriod(DataTypeConfig dataType, string ticker, PeriodRange periodRange, bool IsOndemandEndpointEnabled) + public IComparablePeriod FindEndOfOndemandPeriod(DataTypeConfig dataType, string ticker, PeriodRange periodRange, bool IsOndemandEndpointEnabled, bool isConfigureAwait, bool useCache) { - var fromTier = FindAvailableTier(dataType, ticker, periodRange.From, IsOndemandEndpointEnabled); - var toTier = FindAvailableTier(dataType, ticker, periodRange.To, IsOndemandEndpointEnabled); + var fromTier = FindAvailableTier(dataType, ticker, periodRange.From, IsOndemandEndpointEnabled, isConfigureAwait, useCache); + var toTier = FindAvailableTier(dataType, ticker, periodRange.To, IsOndemandEndpointEnabled, isConfigureAwait, useCache); if (fromTier.Equals(SupportedTier.FixedTier)) { @@ -61,7 +62,7 @@ public IComparablePeriod FindEndOfOndemandPeriod(DataTypeConfig dataType, string var period = periodRange.From.Next(); while (period.CompareTo(periodRange.To) < 0) { - var tier = FindAvailableTier(dataType, ticker, period.Next(), IsOndemandEndpointEnabled); + var tier = FindAvailableTier(dataType, ticker, period.Next(), IsOndemandEndpointEnabled, isConfigureAwait, useCache); if (tier.Equals(SupportedTier.FixedTier)) { break; diff --git a/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs b/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs index bae2b26..45ba784 100644 --- a/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs +++ b/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs @@ -55,7 +55,7 @@ public BuffettCodeApiTaskProcessor UpdateIfNeeded(string apiKey, uint maxDegreeO public IApiResource GetApiResource(DataTypeConfig dataType, string ticker, IPeriod period, bool isConfigureAwait = true, bool useCache = true) { - var useOndemand = taskHelper.ShouldUseOndemandEndpoint(dataType, ticker, period, isOndemandEndpointEnabled); + var useOndemand = taskHelper.ShouldUseOndemandEndpoint(dataType, ticker, period, isOndemandEndpointEnabled, isConfigureAwait, useCache); var json = processor.Process(client.Get(dataType, ticker, period, useOndemand, isConfigureAwait, useCache)); return parser.Parse(dataType, json); } @@ -66,7 +66,7 @@ public IList GetApiResources(DataTypeConfig dataType, string ticke { throw new ArgumentException($"from={from} is more than to={to}"); } - var endOfOndemandPeriod = taskHelper.FindEndOfOndemandPeriod(dataType, ticker, PeriodRange.Create(from, to), isOndemandEndpointEnabled); + var endOfOndemandPeriod = taskHelper.FindEndOfOndemandPeriod(dataType, ticker, PeriodRange.Create(from, to), isOndemandEndpointEnabled, isConfigureAwait, useCache); // use fixed tier from all range if (endOfOndemandPeriod is null) diff --git a/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs b/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs index 4339e0c..e672ee6 100644 --- a/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs +++ b/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs @@ -23,28 +23,28 @@ public PeriodSupportedTierResolver(IBuffettCodeApiClient apiClient, IApiResponse public static PeriodSupportedTierResolver Create(IBuffettCodeApiClient apiClient, IApiResponseParser parser) => new PeriodSupportedTierResolver(apiClient, parser, new SupportedTierDictionary()); - public SupportedTier Resolve(DataTypeConfig dataType, string ticker, IPeriod period) + public SupportedTier Resolve(DataTypeConfig dataType, string ticker, IPeriod period, bool isConfigureAwait, bool useCache) { switch (dataType) { case DataTypeConfig.Quarter: - return ResolveQuarter(ticker, period as FiscalQuarterPeriod); + return ResolveQuarter(ticker, period as FiscalQuarterPeriod, isConfigureAwait, useCache); default: throw new NotSupportedDataTypeException($"dataType={dataType} is not supported."); } } - private Company GetCompany(string ticker) + private Company GetCompany(string ticker, bool isConfigureAwait, bool useCache) { - var json = apiClient.Get(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), false, true, true).Result; + var json = apiClient.Get(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), false, isConfigureAwait, useCache).Result; return parser.Parse(DataTypeConfig.Company, json) as Company; } - public SupportedTier ResolveQuarter(string ticker, FiscalQuarterPeriod period) + private SupportedTier ResolveQuarter(string ticker, FiscalQuarterPeriod period, bool isConfigureAwait, bool useCache) { if (!supportedTierDict.Has(ticker, DataTypeConfig.Quarter)) { - var company = GetCompany(ticker); + var company = GetCompany(ticker, isConfigureAwait, useCache); supportedTierDict.Add(company); } return supportedTierDict.Get(ticker, period); diff --git a/BuffettCodeIOTests/ApiTaskHelperTests.cs b/BuffettCodeIOTests/ApiTaskHelperTests.cs index 90380fc..139e2c2 100644 --- a/BuffettCodeIOTests/ApiTaskHelperTests.cs +++ b/BuffettCodeIOTests/ApiTaskHelperTests.cs @@ -35,22 +35,22 @@ public void FindAvailableTierTest() var helper = new ApiTaskHelper(tierResolver); // enable ondemand endpoint - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest, true)); - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, true)); - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatest, true)); - Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest, true)); - Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, true)); - Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest, true)); - Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, true)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest, true, true, true)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, true, true, true)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatest, true, true, true)); + Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest, true, true, true)); + Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, true, true, true)); + Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest, true, true, true)); + Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, true, true, true)); // disabled ondemand endpoint - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest, false)); - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, false)); - Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatest, false)); - Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest, false)); - Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, false)); - Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest, false)); - Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, false)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest, false, true, true)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, false, true, true)); + Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatest, false, true, true)); + Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest, false, true, true)); + Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, false, true, true)); + Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest, false, true, true)); + Assert.ThrowsException(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, false, true, true)); } [TestMethod()] @@ -60,22 +60,22 @@ public void ShouldUseOndemandEndpointTest() var helper = new ApiTaskHelper(tierResolver); // enable ondemand endpoint - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest, true)); - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, true)); - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedLatest, true)); - Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest, true)); - Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, true)); - Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest, true)); - Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, true)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest, true, true, true)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, true, true, true)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedLatest, true, true, true)); + Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest, true, true, true)); + Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, true, true, true)); + Assert.IsTrue(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest, true, true, true)); + Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, true, true, true)); // disabled ondemand endpoint - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest, false)); - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, false)); - Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedLatest, false)); - Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest, false)); - Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, false)); - Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest, false)); - Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, false)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest, false, true, true)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, false, true, true)); + Assert.IsFalse(helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, fixedLatest, false, true, true)); + Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest, false, true, true)); + Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, false, true, true)); + Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest, false, true, true)); + Assert.ThrowsException(() => helper.ShouldUseOndemandEndpoint(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, false, true, true)); } [TestMethod()] @@ -86,14 +86,14 @@ public void FindEndOfOndemandPeriodTest() // for fixed tier range, return null var tierRange = PeriodRange.Create(fixedOldest, fixedLatest); - Assert.IsNull(helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, true) as FiscalQuarterPeriod); - Assert.IsNull(helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, false) as FiscalQuarterPeriod); + Assert.IsNull(helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, true, true, true) as FiscalQuarterPeriod); + Assert.IsNull(helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, false, true, true) as FiscalQuarterPeriod); // for ondemand range tierRange = PeriodRange.Create(ondemandOldest, fixedLatest); - Assert.AreEqual(FiscalQuarterPeriod.Create(2016, 1), helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, true) as FiscalQuarterPeriod); - Assert.ThrowsException(() => helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, false)); + Assert.AreEqual(FiscalQuarterPeriod.Create(2016, 1), helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, true, true, true) as FiscalQuarterPeriod); + Assert.ThrowsException(() => helper.FindEndOfOndemandPeriod(DataTypeConfig.Quarter, ticker, tierRange, false, true, true)); } } } \ No newline at end of file diff --git a/BuffettCodeIOTests/Resolver/PeriodSupportedTierResolverTests.cs b/BuffettCodeIOTests/Resolver/PeriodSupportedTierResolverTests.cs index 089dc66..7e3d6f2 100644 --- a/BuffettCodeIOTests/Resolver/PeriodSupportedTierResolverTests.cs +++ b/BuffettCodeIOTests/Resolver/PeriodSupportedTierResolverTests.cs @@ -18,22 +18,6 @@ public class PeriodSupportedTierResolverTests private static readonly Company company = Company.Create(ticker, fixedTierRange, ondemandTierRange, PropertyDictionary.Empty(), PropertyDescriptionDictionary.Empty()); - [TestMethod()] - public void ResolveTest() - { - var supportedDict = new SupportedTierDictionary(); - supportedDict.Add(company); - var resolver = new PeriodSupportedTierResolver(null, null, supportedDict); - - Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedOldest)); - Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod)); - Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedLatest)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandOldest)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandLatest)); - Assert.AreEqual(SupportedTier.None, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod)); - } - [TestMethod()] public void ResolveQuarterTest() { @@ -41,13 +25,14 @@ public void ResolveQuarterTest() supportedDict.Add(company); var resolver = new PeriodSupportedTierResolver(null, null, supportedDict); - Assert.AreEqual(SupportedTier.FixedTier, resolver.ResolveQuarter(ticker, fixedOldest)); - Assert.AreEqual(SupportedTier.FixedTier, resolver.ResolveQuarter(ticker, fixedOldest.Next() as FiscalQuarterPeriod)); - Assert.AreEqual(SupportedTier.FixedTier, resolver.ResolveQuarter(ticker, fixedLatest)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.ResolveQuarter(ticker, ondemandOldest)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.ResolveQuarter(ticker, ondemandOldest.Next() as FiscalQuarterPeriod)); - Assert.AreEqual(SupportedTier.OndemandTier, resolver.ResolveQuarter(ticker, ondemandLatest)); - Assert.AreEqual(SupportedTier.None, resolver.ResolveQuarter(ticker, ondemandLatest.Next() as FiscalQuarterPeriod)); + Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedOldest, false, false)); + Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedOldest.Next() as FiscalQuarterPeriod, false, false)); + Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, fixedLatest, false, false)); + Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandOldest, false, false)); + Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandOldest.Next() as FiscalQuarterPeriod, false, false)); + Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandLatest, false, false)); + Assert.AreEqual(SupportedTier.None, resolver.Resolve(DataTypeConfig.Quarter, ticker, ondemandLatest.Next() as FiscalQuarterPeriod, false, false)); } + } } \ No newline at end of file From 36e36932557e2207e92bb217d5197593bc0d338a Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Mon, 1 Nov 2021 19:05:02 +0900 Subject: [PATCH 2/7] remove useless async --- BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs | 2 +- BuffettCodeAddinRibbon/CsvDownloadForm.cs | 4 ++-- .../CsvDownload/ApiResourceGetterTests.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs index cfdb5a1..8c05ada 100644 --- a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs +++ b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs @@ -30,7 +30,7 @@ public static ApiResourceGetter Create() return Create(Configuration.GetInstance()); } - public async Task> GetQuarters(CsvDownloadParameters parameters) + public IEnumerable GetQuarters(CsvDownloadParameters parameters) { // set isConfigureAwait for performance diff --git a/BuffettCodeAddinRibbon/CsvDownloadForm.cs b/BuffettCodeAddinRibbon/CsvDownloadForm.cs index a11fc48..9b6bf4a 100644 --- a/BuffettCodeAddinRibbon/CsvDownloadForm.cs +++ b/BuffettCodeAddinRibbon/CsvDownloadForm.cs @@ -61,7 +61,7 @@ private void ButtonOK_Click(object sender, EventArgs e) Execute(); } - private async void Execute() + private void Execute() { if (!ValidateControls()) { @@ -70,7 +70,7 @@ private async void Execute() try { var parameters = CreateParametersFromFormValues(); - var resources = await apiResourceGetter.GetQuarters(parameters); + var resources = apiResourceGetter.GetQuarters(parameters); var quarters = resources.ToList(); if (quarters.Count == 0) { diff --git a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs index ad66946..33c5f3d 100644 --- a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs +++ b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs @@ -38,7 +38,7 @@ public void GetQuartersTest() var to = FiscalQuarterPeriod.Create(2020, 2); var outputSettings = CsvDownloadOutputSettings.Create(TabularOutputEncoding.SJIS, TabularOutputDestination.NewWorksheet); var parameters = CsvDownloadParameters.Create(ticker, from, to, outputSettings); - var quarters = ApiResourceGetter.Create(Configuration.GetInstance()).GetQuarters(parameters).Result.ToArray(); + var quarters = ApiResourceGetter.Create(Configuration.GetInstance()).GetQuarters(parameters).ToArray(); var quarter2020Q1 = quarters[0]; var quarter2020Q2 = quarters[1]; Assert.AreEqual(2, quarters.Length); From d76f54954ff9784d5fb1f53717e9f78176973d18 Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Mon, 1 Nov 2021 19:29:23 +0900 Subject: [PATCH 3/7] remove parallelism --- .../BuffettCodeApiV2Client.cs | 21 ++++--- .../BuffettCodeApiV3Client.cs | 11 ++-- BuffettCodeAPIClient/IBuffettCodeApiClient.cs | 5 +- .../BuffettCodeApiV2ClientTests.cs | 8 +-- .../BuffettCodeApiV3ClientTests.cs | 2 +- .../CsvDownload/ApiResourceGetter.cs | 9 ++- .../Settings/AddinSettings.cs | 8 +-- .../CsvDownload/ApiResourceGetterTests.cs | 5 -- .../Config/BuffettCodeRegistryConfig.cs | 3 +- BuffettCodeCommon/Configuration.cs | 16 ------ BuffettCodeCommonTests/ConfigurationTests.cs | 12 ---- .../ApiResourceFetcher.cs | 4 +- BuffettCodeIO/BuffettCodeApiTaskProcessor.cs | 26 +++------ BuffettCodeIO/BuffettCodeIO.csproj | 2 - BuffettCodeIO/Processor/ITaskProcessor.cs | 34 ------------ .../Processor/SemaphoreTaskProcessor.cs | 55 ------------------- .../Resolver/PeriodSupportedTierResolver.cs | 2 +- 17 files changed, 41 insertions(+), 182 deletions(-) delete mode 100644 BuffettCodeIO/Processor/ITaskProcessor.cs delete mode 100644 BuffettCodeIO/Processor/SemaphoreTaskProcessor.cs diff --git a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs index 24d1b6d..6b9d893 100644 --- a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs +++ b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs @@ -5,7 +5,6 @@ using Newtonsoft.Json.Linq; using System; using System.Runtime.Caching; -using System.Threading.Tasks; namespace BuffettCodeAPIClient { @@ -25,28 +24,28 @@ private BuffettCodeApiV2Client() ); } - public async Task GetQuarter(string ticker, FiscalQuarterPeriod period, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) + public JObject GetQuarter(string ticker, FiscalQuarterPeriod period, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetQuarterRequest(ticker, period, useOndemand); var response = apiClientCore.Get(request, isConfigureAwait, useCache); return ApiGetResponseBodyParser.Parse(response.Result); } - public async Task GetIndicator(string ticker, bool isConfigureAwait = true, bool useCache = true) + public JObject GetIndicator(string ticker, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetIndicatorRequest (ticker); - var response = await apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response); + var response = apiClientCore.Get(request, isConfigureAwait, useCache); + return ApiGetResponseBodyParser.Parse(response.Result); } - public async Task GetQuarterRange(string ticker, FiscalQuarterPeriod from, FiscalQuarterPeriod to, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) + public JObject GetQuarterRange(string ticker, FiscalQuarterPeriod from, FiscalQuarterPeriod to, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetQuarterRangeRequest(ticker, from, to); - var response = await apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response); + var response = apiClientCore.Get(request, isConfigureAwait, useCache); + return ApiGetResponseBodyParser.Parse(response.Result); } - public async Task GetCompany(string ticker, bool isConfigureAwait = true, bool useCache = true) + public JObject GetCompany(string ticker, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetCompanyRequest(ticker); var response = apiClientCore.Get(request, isConfigureAwait, useCache); @@ -64,7 +63,7 @@ public static BuffettCodeApiV2Client GetInstance(string apiKey) } - public Task Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) + public JObject Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { switch (dataType) { @@ -79,7 +78,7 @@ public Task Get(DataTypeConfig dataType, string ticker, IPeriod period, } } - public Task GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) + public JObject GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { switch (dataType) { diff --git a/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs b/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs index afb7486..f0103a9 100644 --- a/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs +++ b/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs @@ -5,7 +5,6 @@ using BuffettCodeCommon.Validator; using Newtonsoft.Json.Linq; using System.Runtime.Caching; -using System.Threading.Tasks; namespace BuffettCodeAPIClient @@ -27,12 +26,12 @@ private BuffettCodeApiV3Client() } - public async Task GetDaily(string ticker, DayPeriod day, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) + public JObject GetDaily(string ticker, DayPeriod day, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(ticker, day, useOndemand); JpTickerValidator.Validate(ticker); - var response = await apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response); + var response = apiClientCore.Get(request, isConfigureAwait, useCache); + return ApiGetResponseBodyParser.Parse(response.Result); } public void UpdateApiKey(string apiKey) => apiClientCore.UpdateApiKey(apiKey); @@ -45,7 +44,7 @@ public static BuffettCodeApiV3Client GetInstance(string apiKey) return instance; } - public Task Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait, bool useCache) + public JObject Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait, bool useCache) { switch (dataType) { @@ -56,7 +55,7 @@ public Task Get(DataTypeConfig dataType, string ticker, IPeriod period, } } - public Task GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait, bool useCache) + public JObject GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait, bool useCache) { switch (dataType) { diff --git a/BuffettCodeAPIClient/IBuffettCodeApiClient.cs b/BuffettCodeAPIClient/IBuffettCodeApiClient.cs index 81a8f1a..a0e3e67 100644 --- a/BuffettCodeAPIClient/IBuffettCodeApiClient.cs +++ b/BuffettCodeAPIClient/IBuffettCodeApiClient.cs @@ -1,15 +1,14 @@ using BuffettCodeCommon.Config; using BuffettCodeCommon.Period; using Newtonsoft.Json.Linq; -using System.Threading.Tasks; namespace BuffettCodeAPIClient { public interface IBuffettCodeApiClient { - Task Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait, bool useCache); - Task GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait, bool useCache); + JObject Get(DataTypeConfig dataType, string ticker, IPeriod period, bool useOndemand, bool isConfigureAwait, bool useCache); + JObject GetRange(DataTypeConfig dataType, string ticker, IPeriod from, IPeriod to, bool useOndemand, bool isConfigureAwait, bool useCache); string GetApiKey(); void UpdateApiKey(string apiKey); diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs index 86264a0..5fcc0cd 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs @@ -29,7 +29,7 @@ public void GetQuarterTest() Assert.IsNotNull(client.GetQuarter("6501", period, false, true, false)); // test api key can get ticker=xx02 - Assert.ThrowsExceptionAsync(() => client.GetQuarter("6502", period, false, true, false)); + Assert.ThrowsException(() => client.GetQuarter("6502", period, false, true, false)); } [TestMethod()] @@ -39,7 +39,7 @@ public void GetIndicatorTest() Assert.IsNotNull(client.GetIndicator("6501", true)); // test api key can get ticker=xx02 - Assert.ThrowsExceptionAsync(() => client.GetIndicator("6502", true, false)); + Assert.ThrowsException(() => client.GetIndicator("6502", true, false)); } [TestMethod()] @@ -52,7 +52,7 @@ public void GetQuarterRangeTest() Assert.IsNotNull(client.GetQuarterRange("6501", from, to, false, false)); // test api key can get ticker=xx02 - Assert.ThrowsExceptionAsync(() => client.GetQuarterRange("6502", from, to, true, false)); + Assert.ThrowsException(() => client.GetQuarterRange("6502", from, to, true, false)); } [TestMethod()] @@ -62,7 +62,7 @@ public void GetCompanyTest() Assert.IsNotNull(client.GetCompany("6501", true, false)); // test api key can get ticker=xx02 - Assert.ThrowsExceptionAsync(() => client.GetCompany("6502", true, false)); + Assert.ThrowsException(() => client.GetCompany("6502", true, false)); } diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs index 19c697d..2ff9791 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs @@ -28,7 +28,7 @@ public void GetDailyTest() Assert.IsNotNull(client.GetDaily("6501", day, false, true, false)); // test api key can get not 01 - Assert.ThrowsExceptionAsync + Assert.ThrowsException (() => client.GetDaily("6502", day, true, true, false)); } } diff --git a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs index 8c05ada..2dab45d 100644 --- a/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs +++ b/BuffettCodeAddinRibbon/CsvDownload/ApiResourceGetter.cs @@ -1,12 +1,11 @@ -using System.Collections.Generic; -using System.Linq; +using BuffettCodeAddinRibbon.Settings; using BuffettCodeCommon; using BuffettCodeCommon.Config; using BuffettCodeCommon.Period; using BuffettCodeIO; using BuffettCodeIO.Property; -using BuffettCodeAddinRibbon.Settings; -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; namespace BuffettCodeAddinRibbon.CsvDownload { @@ -21,7 +20,7 @@ private ApiResourceGetter(BuffettCodeApiTaskProcessor processor) public static ApiResourceGetter Create(Configuration config) { - var processor = new BuffettCodeApiTaskProcessor(config.ApiVersion, config.ApiKey, config.MaxDegreeOfParallelism, config.IsOndemandEndpointEnabled); + var processor = new BuffettCodeApiTaskProcessor(config.ApiVersion, config.ApiKey, config.IsOndemandEndpointEnabled); return new ApiResourceGetter(processor); } diff --git a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs index 96904e4..bc6b4cb 100644 --- a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs +++ b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs @@ -15,30 +15,28 @@ public class AddinSettings public uint MaxDegreeOfParallelism => maxDegreeOfParallelism; public bool DebugMode => debugMode; - private AddinSettings(string apiKey, bool useOndemandEndpoint, uint maxDegreeOfParallelism, bool debugMode) + private AddinSettings(string apiKey, bool useOndemandEndpoint, bool debugMode) { this.apiKey = apiKey; this.enabledOndemandEndpoint = useOndemandEndpoint; - this.maxDegreeOfParallelism = maxDegreeOfParallelism; this.debugMode = debugMode; } public static AddinSettings Create(string apiKey, bool useOndemandEndpoint, uint maxDegreeOfParallelism, bool debugMode) { ApiKeyValidator.Validate(apiKey); - return new AddinSettings(apiKey, useOndemandEndpoint, maxDegreeOfParallelism, debugMode); + return new AddinSettings(apiKey, useOndemandEndpoint, debugMode); } public static AddinSettings Create(Configuration config) { - return new AddinSettings(config.ApiKey, config.IsOndemandEndpointEnabled, config.MaxDegreeOfParallelism, config.DebugMode); + return new AddinSettings(config.ApiKey, config.IsOndemandEndpointEnabled, config.DebugMode); } public void SaveToConfiguration(Configuration config) { config.ApiKey = apiKey; config.IsOndemandEndpointEnabled = enabledOndemandEndpoint; - config.MaxDegreeOfParallelism = maxDegreeOfParallelism; config.DebugMode = debugMode; } diff --git a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs index 33c5f3d..93e739a 100644 --- a/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs +++ b/BuffettCodeAddinRibbonTests/CsvDownload/ApiResourceGetterTests.cs @@ -1,14 +1,9 @@ -using BuffettCodeAddinRibbon.CsvDownload; using BuffettCodeAddinRibbon.Settings; using BuffettCodeCommon; using BuffettCodeCommon.Config; using BuffettCodeCommon.Period; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace BuffettCodeAddinRibbon.CsvDownload.Tests diff --git a/BuffettCodeCommon/Config/BuffettCodeRegistryConfig.cs b/BuffettCodeCommon/Config/BuffettCodeRegistryConfig.cs index 50302ba..cebcf01 100644 --- a/BuffettCodeCommon/Config/BuffettCodeRegistryConfig.cs +++ b/BuffettCodeCommon/Config/BuffettCodeRegistryConfig.cs @@ -11,7 +11,7 @@ public static string SubKeyBuffettCodeExcelAddinTest = @"Software\BuffettCode\ExcelAddinTest"; public static readonly string NameApiKey = "ApiKey"; - public static readonly string NameMaxDegreeOfParallelism = "MaxDegreeOfParallelism"; + public static readonly string NameIsOndemandEndpointEnabled = "IsOndemandEndpointEnabled"; public static readonly string NameClearCache = "ClearCache"; @@ -27,7 +27,6 @@ public static string SubKeyBuffettCodeExcelAddinTest public static readonly ISet SupportedValueNames = new HashSet { NameApiKey, - NameMaxDegreeOfParallelism, NameIsOndemandEndpointEnabled, NameClearCache, NameDebugMode, diff --git a/BuffettCodeCommon/Configuration.cs b/BuffettCodeCommon/Configuration.cs index 8c79456..881add4 100644 --- a/BuffettCodeCommon/Configuration.cs +++ b/BuffettCodeCommon/Configuration.cs @@ -38,12 +38,6 @@ public static Configuration GetInstance(bool isDev = false) /// private static readonly string ApiKeyDefault = BuffettCodeApiKeyConfig.TestApiKey; - /// - /// 最大同時実行数のデフォルト値 - /// - private static readonly uint MaxDegreeOfParallelismDefault = 8; - - /// /// デフォルトでは Ondemand Endpoint は利用不可 /// @@ -77,15 +71,6 @@ public string ApiKey } - /// - /// APIコールの最大同時実行数 - /// - public uint MaxDegreeOfParallelism - { - get => uint.Parse(registryAccessor.GetRegistryValue(BuffettCodeRegistryConfig.NameMaxDegreeOfParallelism, MaxDegreeOfParallelismDefault).ToString()); - set => registryAccessor.SaveRegistryValue(BuffettCodeRegistryConfig.NameMaxDegreeOfParallelism, value); - } - // Registryには"True" "False" の文字列で書き込まれる private bool IsTrue(string name, bool defaultValue) => registryAccessor.GetRegistryValue(name, defaultValue).ToString().Equals(true.ToString()); @@ -98,7 +83,6 @@ public bool IsOndemandEndpointEnabled set => registryAccessor.SaveRegistryValue(BuffettCodeRegistryConfig.NameIsOndemandEndpointEnabled, value); } - /// /// デバッグモード /// diff --git a/BuffettCodeCommonTests/ConfigurationTests.cs b/BuffettCodeCommonTests/ConfigurationTests.cs index 9b99730..36f8699 100644 --- a/BuffettCodeCommonTests/ConfigurationTests.cs +++ b/BuffettCodeCommonTests/ConfigurationTests.cs @@ -60,18 +60,6 @@ public void ApiKeyTest() } - [TestMethod()] - public void MaxDegreeOfParallelismTest() - { - // check default value - Assert.AreEqual((uint)8, configForTest.MaxDegreeOfParallelism); - - // set and get - var maxDegreeOfParallelism = (uint)randomizer.Next(100); - configForTest.MaxDegreeOfParallelism = maxDegreeOfParallelism; - Assert.AreEqual(maxDegreeOfParallelism, configForTest.MaxDegreeOfParallelism); - } - [TestMethod()] public void UseOndemandEndpointTest() { diff --git a/BuffettCodeExcelFunctions/ApiResourceFetcher.cs b/BuffettCodeExcelFunctions/ApiResourceFetcher.cs index 25bd06f..58eace1 100644 --- a/BuffettCodeExcelFunctions/ApiResourceFetcher.cs +++ b/BuffettCodeExcelFunctions/ApiResourceFetcher.cs @@ -11,7 +11,7 @@ public class ApiResourceFetcher { private static readonly Configuration config = Configuration.GetInstance(); private static readonly IDataTypeResolver resolver = V2DataTypeResolverFactory.Create(); - private static readonly BuffettCodeApiTaskProcessor processor = new BuffettCodeApiTaskProcessor(config.ApiVersion, config.ApiKey, config.MaxDegreeOfParallelism, config.IsOndemandEndpointEnabled + private static readonly BuffettCodeApiTaskProcessor processor = new BuffettCodeApiTaskProcessor(config.ApiVersion, config.ApiKey, config.IsOndemandEndpointEnabled ); public static IApiResource FetchForLegacy @@ -31,7 +31,7 @@ public static IApiResource FetchForLegacy throw new NotSupportedDataTypeException(); } } - public static IApiResource Fetch(DataTypeConfig dataType, string ticker, IPeriod period, bool isConfigureAwait = true, bool useCache = true) => processor.UpdateIfNeeded(config.ApiKey, config.MaxDegreeOfParallelism, config.IsOndemandEndpointEnabled).GetApiResource(dataType, ticker, period, isConfigureAwait, useCache); + public static IApiResource Fetch(DataTypeConfig dataType, string ticker, IPeriod period, bool isConfigureAwait = true, bool useCache = true) => processor.UpdateIfNeeded(config.ApiKey, config.IsOndemandEndpointEnabled).GetApiResource(dataType, ticker, period, isConfigureAwait, useCache); } } \ No newline at end of file diff --git a/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs b/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs index 45ba784..11c0ebd 100644 --- a/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs +++ b/BuffettCodeIO/BuffettCodeApiTaskProcessor.cs @@ -2,10 +2,8 @@ using BuffettCodeCommon.Config; using BuffettCodeCommon.Period; using BuffettCodeIO.Parser; -using BuffettCodeIO.Processor; using BuffettCodeIO.Property; using BuffettCodeIO.Resolver; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; @@ -16,22 +14,20 @@ public class BuffettCodeApiTaskProcessor { private readonly IBuffettCodeApiClient client; private readonly IApiResponseParser parser; - private readonly ITaskProcessor processor; private readonly ApiTaskHelper taskHelper; private bool isOndemandEndpointEnabled; private static readonly object updateLock = new object(); - public BuffettCodeApiTaskProcessor(BuffettCodeApiVersion version, string apiKey, uint maxDegreeOfParallelism, bool useOndemandEndpoint) + public BuffettCodeApiTaskProcessor(BuffettCodeApiVersion version, string apiKey, bool useOndemandEndpoint) { client = ApiClientInstanceGetter.Get(version, apiKey); parser = ApiResponseParserFactory.Create(version); var tierResolver = PeriodSupportedTierResolver.Create(client, parser); taskHelper = new ApiTaskHelper(tierResolver); - processor = new SemaphoreTaskProcessor(maxDegreeOfParallelism); this.isOndemandEndpointEnabled = useOndemandEndpoint; } - public BuffettCodeApiTaskProcessor UpdateIfNeeded(string apiKey, uint maxDegreeOfParallelism, bool useOndemandEndpoint) + public BuffettCodeApiTaskProcessor UpdateIfNeeded(string apiKey, bool useOndemandEndpoint) { lock (updateLock) { @@ -39,15 +35,9 @@ public BuffettCodeApiTaskProcessor UpdateIfNeeded(string apiKey, uint maxDegreeO { client.UpdateApiKey(apiKey); } - if (!processor.GetMaxDegreeOfParallelism().Equals - (maxDegreeOfParallelism)) + if (isOndemandEndpointEnabled != useOndemandEndpoint) { - processor.UpdateMaxDegreeOfParallelism(maxDegreeOfParallelism); - } - - if (this.isOndemandEndpointEnabled != useOndemandEndpoint) - { - this.isOndemandEndpointEnabled = useOndemandEndpoint; + isOndemandEndpointEnabled = useOndemandEndpoint; } } return this; @@ -56,7 +46,7 @@ public BuffettCodeApiTaskProcessor UpdateIfNeeded(string apiKey, uint maxDegreeO public IApiResource GetApiResource(DataTypeConfig dataType, string ticker, IPeriod period, bool isConfigureAwait = true, bool useCache = true) { var useOndemand = taskHelper.ShouldUseOndemandEndpoint(dataType, ticker, period, isOndemandEndpointEnabled, isConfigureAwait, useCache); - var json = processor.Process(client.Get(dataType, ticker, period, useOndemand, isConfigureAwait, useCache)); + var json = client.Get(dataType, ticker, period, useOndemand, isConfigureAwait, useCache); return parser.Parse(dataType, json); } @@ -71,13 +61,13 @@ public IList GetApiResources(DataTypeConfig dataType, string ticke // use fixed tier from all range if (endOfOndemandPeriod is null) { - var json = processor.Process(client.GetRange(dataType, ticker, from, to, false, isConfigureAwait, useCache)); + var json = client.GetRange(dataType, ticker, from, to, false, isConfigureAwait, useCache); return parser.ParseRange(dataType, json); } else { - var ondemandTierJson = processor.Process(client.GetRange(dataType, ticker, from, endOfOndemandPeriod, true, isConfigureAwait, useCache)); - var fixedTierJson = processor.Process(client.GetRange(dataType, ticker, endOfOndemandPeriod.Next(), to, false, isConfigureAwait, useCache)); + var ondemandTierJson = client.GetRange(dataType, ticker, from, endOfOndemandPeriod, true, isConfigureAwait, useCache); + var fixedTierJson = client.GetRange(dataType, ticker, endOfOndemandPeriod.Next(), to, false, isConfigureAwait, useCache); var ondemandResources = parser.ParseRange(dataType, ondemandTierJson); var fixedTierResource = parser.ParseRange(dataType, fixedTierJson); diff --git a/BuffettCodeIO/BuffettCodeIO.csproj b/BuffettCodeIO/BuffettCodeIO.csproj index 5ef180c..99a7a99 100644 --- a/BuffettCodeIO/BuffettCodeIO.csproj +++ b/BuffettCodeIO/BuffettCodeIO.csproj @@ -98,8 +98,6 @@ - - diff --git a/BuffettCodeIO/Processor/ITaskProcessor.cs b/BuffettCodeIO/Processor/ITaskProcessor.cs deleted file mode 100644 index e314541..0000000 --- a/BuffettCodeIO/Processor/ITaskProcessor.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Threading.Tasks; - -namespace BuffettCodeIO.Processor -{ - /// - /// タスクプロセッサインタフェース - /// - /// - /// WebAPIのAPIコールを実行するTaskを一元的に受け取り、同時実行数の制御をするためのインタフェース。 - /// 以下のようなモチベーションで作成してます。 - /// - /// - /// ユーザのPCスペックによってはExcelアドインの実行でPCに負担が掛かるかもしれず、 - /// その場合に負荷を抑えたい - /// - /// - /// バフェットコードのWebAPIには「秒あたりxx件」という形でスロットリングの制限があり、 - /// この制限に引っかかる場合に実行ペースを一定以下にしたい。 - /// - /// - /// - interface ITaskProcessor - { - /// - /// タスクを実行します。 - /// - /// タスク - /// タスクの実行結果 - Type Process(Task task); - void UpdateMaxDegreeOfParallelism(uint maxDegreeOfParallelism); - - uint GetMaxDegreeOfParallelism(); - } -} \ No newline at end of file diff --git a/BuffettCodeIO/Processor/SemaphoreTaskProcessor.cs b/BuffettCodeIO/Processor/SemaphoreTaskProcessor.cs deleted file mode 100644 index 47c6eba..0000000 --- a/BuffettCodeIO/Processor/SemaphoreTaskProcessor.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; - -namespace BuffettCodeIO.Processor -{ - /// - /// を利用した - /// - /// Taskの型 - public class SemaphoreTaskProcessor : ITaskProcessor - { - private SemaphoreSlim semaphore; - private uint maxDegreeOfParallelism; - - /// - /// コンストラクタ - /// - /// 同時に実行可能なタスクの数 - public SemaphoreTaskProcessor(uint maxDegreeOfParallelism) - { - this.maxDegreeOfParallelism = maxDegreeOfParallelism; - semaphore = new SemaphoreSlim((int)maxDegreeOfParallelism); - } - - public void UpdateMaxDegreeOfParallelism(uint maxDegreeOfParallelism) - { - this.maxDegreeOfParallelism = maxDegreeOfParallelism; - semaphore = new SemaphoreSlim((int)maxDegreeOfParallelism); - } - - public uint GetMaxDegreeOfParallelism() => maxDegreeOfParallelism; - - /// - public Type Process(Task task) - { - return CreateWrapperTask(task).Result; - } - - private Task CreateWrapperTask(Task task) - { - return Task.Run(async () => - { - try - { - await semaphore.WaitAsync(); - return task.Result; - } - finally - { - semaphore.Release(); - } - }); - } - } -} \ No newline at end of file diff --git a/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs b/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs index e672ee6..2f94cdb 100644 --- a/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs +++ b/BuffettCodeIO/Resolver/PeriodSupportedTierResolver.cs @@ -36,7 +36,7 @@ public SupportedTier Resolve(DataTypeConfig dataType, string ticker, IPeriod per private Company GetCompany(string ticker, bool isConfigureAwait, bool useCache) { - var json = apiClient.Get(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), false, isConfigureAwait, useCache).Result; + var json = apiClient.Get(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), false, isConfigureAwait, useCache); return parser.Parse(DataTypeConfig.Company, json) as Company; } From 42e15e7f4aad6ec1544045bf64426fce46da5007 Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Mon, 1 Nov 2021 19:39:36 +0900 Subject: [PATCH 4/7] reformat --- .../ApiClientCoreWithCache.cs | 3 +-- .../BuffettCodeApiV2Client.cs | 8 ++++---- .../BuffettCodeApiV3Client.cs | 2 +- .../SettingForm.Designer.cs | 1 - BuffettCodeAddinRibbon/SettingForm.cs | 20 ------------------- .../Settings/AddinSettings.cs | 2 -- 6 files changed, 6 insertions(+), 30 deletions(-) diff --git a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs index 05eae49..9a87737 100644 --- a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs +++ b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.Caching; -using System.Threading.Tasks; namespace BuffettCodeAPIClient { @@ -32,7 +31,7 @@ public void UpdateApiKey(string apikey) public string GetApiKey() => this.apiClientCore.ApiKey; - public async Task Get(ApiGetRequest request, bool isConfigureAwait, bool useCache) + public string Get(ApiGetRequest request, bool isConfigureAwait, bool useCache) { if (useCache && cacheHelper.HasCache(request)) { diff --git a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs index 6b9d893..be68b57 100644 --- a/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs +++ b/BuffettCodeAPIClient/BuffettCodeApiV2Client.cs @@ -28,7 +28,7 @@ public JObject GetQuarter(string ticker, FiscalQuarterPeriod period, bool useOnd { var request = BuffettCodeApiV2RequestCreator.CreateGetQuarterRequest(ticker, period, useOndemand); var response = apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response.Result); + return ApiGetResponseBodyParser.Parse(response); } public JObject GetIndicator(string ticker, bool isConfigureAwait = true, bool useCache = true) @@ -36,20 +36,20 @@ public JObject GetIndicator(string ticker, bool isConfigureAwait = true, bool us var request = BuffettCodeApiV2RequestCreator.CreateGetIndicatorRequest (ticker); var response = apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response.Result); + return ApiGetResponseBodyParser.Parse(response); } public JObject GetQuarterRange(string ticker, FiscalQuarterPeriod from, FiscalQuarterPeriod to, bool useOndemand, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetQuarterRangeRequest(ticker, from, to); var response = apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response.Result); + return ApiGetResponseBodyParser.Parse(response); } public JObject GetCompany(string ticker, bool isConfigureAwait = true, bool useCache = true) { var request = BuffettCodeApiV2RequestCreator.CreateGetCompanyRequest(ticker); var response = apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response.Result); + return ApiGetResponseBodyParser.Parse(response); } public void UpdateApiKey(string apiKey) => apiClientCore.UpdateApiKey(apiKey); diff --git a/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs b/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs index f0103a9..f977c77 100644 --- a/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs +++ b/BuffettCodeAPIClient/BuffettCodeApiV3Client.cs @@ -31,7 +31,7 @@ public JObject GetDaily(string ticker, DayPeriod day, bool useOndemand, bool isC var request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(ticker, day, useOndemand); JpTickerValidator.Validate(ticker); var response = apiClientCore.Get(request, isConfigureAwait, useCache); - return ApiGetResponseBodyParser.Parse(response.Result); + return ApiGetResponseBodyParser.Parse(response); } public void UpdateApiKey(string apiKey) => apiClientCore.UpdateApiKey(apiKey); diff --git a/BuffettCodeAddinRibbon/SettingForm.Designer.cs b/BuffettCodeAddinRibbon/SettingForm.Designer.cs index d0c0183..3830294 100644 --- a/BuffettCodeAddinRibbon/SettingForm.Designer.cs +++ b/BuffettCodeAddinRibbon/SettingForm.Designer.cs @@ -190,7 +190,6 @@ private void InitializeComponent() this.checkParallelism.TabIndex = 7; this.checkParallelism.Text = "APIの実行ペースを制限する"; this.checkParallelism.UseVisualStyleBackColor = true; - this.checkParallelism.CheckedChanged += new System.EventHandler(this.CheckParallelism_CheckedChanged); // // checkDebugMode // diff --git a/BuffettCodeAddinRibbon/SettingForm.cs b/BuffettCodeAddinRibbon/SettingForm.cs index b88efc0..d0ac758 100644 --- a/BuffettCodeAddinRibbon/SettingForm.cs +++ b/BuffettCodeAddinRibbon/SettingForm.cs @@ -13,19 +13,6 @@ public SettingForm(AddinSettings setting) textAPIKey.Text = setting.ApiKey; checkDebugMode.Checked = setting.DebugMode; checkIsOndemandEndpointEnabled.Checked = setting.IsOndemandEndpointEnabled; - var maxDegreeOfParallelism = setting.MaxDegreeOfParallelism; - if (maxDegreeOfParallelism == 0) - { - checkParallelism.Checked = false; - textParallelism.Text = ""; - textParallelism.Enabled = false; - } - else - { - checkParallelism.Checked = true; - textParallelism.Text = maxDegreeOfParallelism.ToString(); - textParallelism.Enabled = true; - } } private void SettingForm_Load(object sender, EventArgs e) @@ -73,13 +60,6 @@ private void ButtonCancel_Click(object sender, EventArgs e) DialogResult = DialogResult.Cancel; Close(); } - - private void CheckParallelism_CheckedChanged(object sender, EventArgs e) - { - textParallelism.Enabled = checkParallelism.Checked; - } - - private void TabAPI_Click(object sender, EventArgs e) { diff --git a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs index bc6b4cb..d759223 100644 --- a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs +++ b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs @@ -7,12 +7,10 @@ public class AddinSettings { private readonly string apiKey; private readonly bool enabledOndemandEndpoint; - private readonly uint maxDegreeOfParallelism; private readonly bool debugMode; public string ApiKey => apiKey; public bool IsOndemandEndpointEnabled => enabledOndemandEndpoint; - public uint MaxDegreeOfParallelism => maxDegreeOfParallelism; public bool DebugMode => debugMode; private AddinSettings(string apiKey, bool useOndemandEndpoint, bool debugMode) From 90c39ac6b20411440d435dd2e346a542390fdfec Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Mon, 1 Nov 2021 19:44:53 +0900 Subject: [PATCH 5/7] remove useless settings form ribbon form design --- BuffettCodeAddinRibbon/Ribbon.cs | 2 +- .../SettingForm.Designer.cs | 39 +------------------ BuffettCodeAddinRibbon/SettingForm.cs | 22 +++-------- .../Settings/AddinSettings.cs | 2 +- 4 files changed, 9 insertions(+), 56 deletions(-) diff --git a/BuffettCodeAddinRibbon/Ribbon.cs b/BuffettCodeAddinRibbon/Ribbon.cs index dc2a878..0612ba9 100644 --- a/BuffettCodeAddinRibbon/Ribbon.cs +++ b/BuffettCodeAddinRibbon/Ribbon.cs @@ -24,7 +24,7 @@ private void SettingButton_Click(object sender, RibbonControlEventArgs e) { try { - var newSettings = AddinSettings.Create(form.GetAPIKey(), form.IsOndemandEndpointEnabled(), form.GetMaxDegreeOfParallelism(), form.IsDebugMode()); + var newSettings = AddinSettings.Create(form.GetAPIKey(), form.IsOndemandEndpointEnabled(), form.IsDebugMode()); newSettings.SaveToConfiguration(config); } catch (AddinConfigurationException) diff --git a/BuffettCodeAddinRibbon/SettingForm.Designer.cs b/BuffettCodeAddinRibbon/SettingForm.Designer.cs index 3830294..5aa38c6 100644 --- a/BuffettCodeAddinRibbon/SettingForm.Designer.cs +++ b/BuffettCodeAddinRibbon/SettingForm.Designer.cs @@ -39,9 +39,6 @@ private void InitializeComponent() this.labelAPIKey = new System.Windows.Forms.Label(); this.textAPIKey = new System.Windows.Forms.TextBox(); this.tabDeveloper = new System.Windows.Forms.TabPage(); - this.textParallelism = new System.Windows.Forms.TextBox(); - this.labelMaxDegreeOfParallelism = new System.Windows.Forms.Label(); - this.checkParallelism = new System.Windows.Forms.CheckBox(); this.checkDebugMode = new System.Windows.Forms.CheckBox(); this.ondemandUsageEntryLink = new System.Windows.Forms.LinkLabel(); this.tabConrtoll.SuspendLayout(); @@ -152,9 +149,6 @@ private void InitializeComponent() // // tabDeveloper // - this.tabDeveloper.Controls.Add(this.textParallelism); - this.tabDeveloper.Controls.Add(this.labelMaxDegreeOfParallelism); - this.tabDeveloper.Controls.Add(this.checkParallelism); this.tabDeveloper.Controls.Add(this.checkDebugMode); this.tabDeveloper.Location = new System.Drawing.Point(4, 28); this.tabDeveloper.Name = "tabDeveloper"; @@ -164,42 +158,16 @@ private void InitializeComponent() this.tabDeveloper.Text = "開発者向けオプション"; this.tabDeveloper.UseVisualStyleBackColor = true; // - // textParallelism - // - this.textParallelism.Location = new System.Drawing.Point(276, 68); - this.textParallelism.Name = "textParallelism"; - this.textParallelism.Size = new System.Drawing.Size(100, 25); - this.textParallelism.TabIndex = 9; - this.textParallelism.Text = "1"; - // - // labelMaxDegreeOfParallelism - // - this.labelMaxDegreeOfParallelism.AutoSize = true; - this.labelMaxDegreeOfParallelism.Location = new System.Drawing.Point(110, 71); - this.labelMaxDegreeOfParallelism.Name = "labelMaxDegreeOfParallelism"; - this.labelMaxDegreeOfParallelism.Size = new System.Drawing.Size(134, 18); - this.labelMaxDegreeOfParallelism.TabIndex = 8; - this.labelMaxDegreeOfParallelism.Text = "最大同時実行数"; - // - // checkParallelism - // - this.checkParallelism.AutoSize = true; - this.checkParallelism.Location = new System.Drawing.Point(30, 39); - this.checkParallelism.Name = "checkParallelism"; - this.checkParallelism.Size = new System.Drawing.Size(234, 22); - this.checkParallelism.TabIndex = 7; - this.checkParallelism.Text = "APIの実行ペースを制限する"; - this.checkParallelism.UseVisualStyleBackColor = true; - // // checkDebugMode // this.checkDebugMode.AutoSize = true; - this.checkDebugMode.Location = new System.Drawing.Point(30, 111); + this.checkDebugMode.Location = new System.Drawing.Point(15, 15); this.checkDebugMode.Name = "checkDebugMode"; this.checkDebugMode.Size = new System.Drawing.Size(223, 22); this.checkDebugMode.TabIndex = 6; this.checkDebugMode.Text = "デバッグモードを有効にする"; this.checkDebugMode.UseVisualStyleBackColor = true; + this.checkDebugMode.CheckedChanged += new System.EventHandler(this.CheckDebugMode_CheckedChanged); // // ondemandUsageEntryLink // @@ -247,9 +215,6 @@ private void InitializeComponent() private System.Windows.Forms.Label labelAPIKey; private System.Windows.Forms.TextBox textAPIKey; private System.Windows.Forms.TabPage tabDeveloper; - private System.Windows.Forms.TextBox textParallelism; - private System.Windows.Forms.Label labelMaxDegreeOfParallelism; - private System.Windows.Forms.CheckBox checkParallelism; private System.Windows.Forms.CheckBox checkDebugMode; private System.Windows.Forms.CheckBox checkIsOndemandEndpointEnabled; private System.Windows.Forms.TextBox ondemandModeDesc; diff --git a/BuffettCodeAddinRibbon/SettingForm.cs b/BuffettCodeAddinRibbon/SettingForm.cs index d0ac758..2568827 100644 --- a/BuffettCodeAddinRibbon/SettingForm.cs +++ b/BuffettCodeAddinRibbon/SettingForm.cs @@ -25,23 +25,6 @@ public string GetAPIKey() return textAPIKey.Text; } - public uint GetMaxDegreeOfParallelism() - { - if (!checkParallelism.Checked) - { - return 0; - } - else if (string.IsNullOrWhiteSpace(textParallelism.Text)) - { - return 0; - } - else if (uint.TryParse(textParallelism.Text, out uint i)) - { - return i; - } - return 0; - } - public bool IsDebugMode() { return checkDebugMode.Checked; @@ -91,5 +74,10 @@ private void OndemandUsageEntryLink_LinkClicked(object sender, LinkLabelLinkClic // open link using a default browser System.Diagnostics.Process.Start(ApiRelatedUrlConfig.ONDEMAND_API_USAGE_ENTRY); } + + private void CheckDebugMode_CheckedChanged(object sender, EventArgs e) + { + + } } } diff --git a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs index d759223..73c0a43 100644 --- a/BuffettCodeAddinRibbon/Settings/AddinSettings.cs +++ b/BuffettCodeAddinRibbon/Settings/AddinSettings.cs @@ -20,7 +20,7 @@ private AddinSettings(string apiKey, bool useOndemandEndpoint, bool debugMode) this.debugMode = debugMode; } - public static AddinSettings Create(string apiKey, bool useOndemandEndpoint, uint maxDegreeOfParallelism, bool debugMode) + public static AddinSettings Create(string apiKey, bool useOndemandEndpoint, bool debugMode) { ApiKeyValidator.Validate(apiKey); return new AddinSettings(apiKey, useOndemandEndpoint, debugMode); From b1717a3f84239a76759f7b8dce2329054044b63b Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Wed, 3 Nov 2021 15:40:01 +0900 Subject: [PATCH 6/7] make IApiClientCore and using mock for ApiClientCoreWithCache --- BuffettCodeAPIClient/ApiClientCore.cs | 18 +++-- .../ApiClientCoreWithCache.cs | 25 +++--- .../BuffettCodeAPIClient.csproj | 1 + BuffettCodeAPIClient/IApiClientCore.cs | 13 +++ .../ApiClientCoreTests.cs | 12 +++ .../ApiClientCoreWithCacheTests.cs | 80 ++++++++++++++++++- .../BuffettCodeApiV2ClientTests.cs | 13 --- .../BuffettCodeApiV3ClientTests.cs | 5 -- 8 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 BuffettCodeAPIClient/IApiClientCore.cs diff --git a/BuffettCodeAPIClient/ApiClientCore.cs b/BuffettCodeAPIClient/ApiClientCore.cs index afda487..76396e9 100644 --- a/BuffettCodeAPIClient/ApiClientCore.cs +++ b/BuffettCodeAPIClient/ApiClientCore.cs @@ -11,15 +11,15 @@ namespace BuffettCodeAPIClient /// BuffettCode API と Http でやり取りする Client のコアクラス /// - public class ApiClientCore : IDisposable + public class ApiClientCore : IDisposable, IApiClientCore { - public string ApiKey { set; get; } + private string apiKey; private readonly Uri baseUri; private static readonly long TimeoutMilliseconds = 5000; private readonly HttpClient httpClient; public ApiClientCore(string apiKey, Uri baseUri) { - this.ApiKey = apiKey; + this.apiKey = apiKey; this.baseUri = baseUri; this.httpClient = NewHttpClient(); } @@ -32,11 +32,11 @@ private HttpClient NewHttpClient() httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); - httpClient.DefaultRequestHeaders.Add("x-api-key", ApiKey); + httpClient.DefaultRequestHeaders.Add("x-api-key", apiKey); return httpClient; } - public static string BuildGetPath(ApiGetRequest request) + private static string BuildGetPath(ApiGetRequest request) { return $"{request.EndPoint}?{new FormUrlEncodedContent(request.Parameters).ReadAsStringAsync().Result}"; } @@ -67,8 +67,14 @@ public void Dispose() { httpClient.Dispose(); } - } + public IApiClientCore SetApiKey(string apikey) + { + apiKey = apikey; + return this; + } + public string GetApiKey() => this.apiKey; + } } \ No newline at end of file diff --git a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs index 9a87737..1a5bc56 100644 --- a/BuffettCodeAPIClient/ApiClientCoreWithCache.cs +++ b/BuffettCodeAPIClient/ApiClientCoreWithCache.cs @@ -5,31 +5,32 @@ namespace BuffettCodeAPIClient { public class ApiClientCoreWithCache { - private readonly ApiClientCore apiClientCore; + private readonly IApiClientCore apiClientCore; private readonly ApiRequestCacheHelper cacheHelper; - private ApiClientCoreWithCache(string apiKey, Uri baseUri, MemoryCache cache) + private ApiClientCoreWithCache(IApiClientCore apiClientCore + , ApiRequestCacheHelper cacheHelper) { - this.apiClientCore = new ApiClientCore(apiKey, baseUri); - this.cacheHelper = new ApiRequestCacheHelper(cache, baseUri); + this.apiClientCore = apiClientCore; + this.cacheHelper = cacheHelper; } public static ApiClientCoreWithCache Create(string apiKey, string baseUrl, MemoryCache cache) { - return new ApiClientCoreWithCache(apiKey, new Uri(baseUrl), cache); + var baseUri = new Uri(baseUrl); + var apiClientCore = new ApiClientCore(apiKey, baseUri); + var cacheHelper = new ApiRequestCacheHelper(cache, baseUri); + return new ApiClientCoreWithCache(apiClientCore, cacheHelper); } - public static ApiClientCoreWithCache Create(string apiKey, Uri baseUrl, MemoryCache cache) + public static ApiClientCoreWithCache Create(IApiClientCore apiClientCore, ApiRequestCacheHelper cacheHelper) { - return new ApiClientCoreWithCache(apiKey, baseUrl, cache); + return new ApiClientCoreWithCache(apiClientCore, cacheHelper); } - public void UpdateApiKey(string apikey) - { - this.apiClientCore.ApiKey = apikey; - } + public void UpdateApiKey(string apiKey) => this.apiClientCore.SetApiKey(apiKey); - public string GetApiKey() => this.apiClientCore.ApiKey; + public string GetApiKey() => this.apiClientCore.GetApiKey(); public string Get(ApiGetRequest request, bool isConfigureAwait, bool useCache) { diff --git a/BuffettCodeAPIClient/BuffettCodeAPIClient.csproj b/BuffettCodeAPIClient/BuffettCodeAPIClient.csproj index fb4b90c..eb2ffea 100644 --- a/BuffettCodeAPIClient/BuffettCodeAPIClient.csproj +++ b/BuffettCodeAPIClient/BuffettCodeAPIClient.csproj @@ -64,6 +64,7 @@ + diff --git a/BuffettCodeAPIClient/IApiClientCore.cs b/BuffettCodeAPIClient/IApiClientCore.cs new file mode 100644 index 0000000..ccfd473 --- /dev/null +++ b/BuffettCodeAPIClient/IApiClientCore.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; + +namespace BuffettCodeAPIClient +{ + public interface IApiClientCore + { + Task Get(ApiGetRequest request, bool isConfigureAwait); + + IApiClientCore SetApiKey(string apikey); + string GetApiKey(); + + } +} \ No newline at end of file diff --git a/BuffettCodeAPIClientTests/ApiClientCoreTests.cs b/BuffettCodeAPIClientTests/ApiClientCoreTests.cs index d915df8..976bf71 100644 --- a/BuffettCodeAPIClientTests/ApiClientCoreTests.cs +++ b/BuffettCodeAPIClientTests/ApiClientCoreTests.cs @@ -32,5 +32,17 @@ var clentCore Assert.AreEqual(1, xApiKeyHeaders.Count); Assert.AreEqual(apiKey, xApiKeyHeaders[0]); } + + [TestMethod()] + public void SetGetApiKeyTest() + { + var initKey = "init_api_key"; + var newKey = "new_api_key"; + var baseUri = new Uri(@"https://example.com"); + var clientCore + = new ApiClientCore(initKey, baseUri); + Assert.AreEqual(initKey, clientCore.GetApiKey()); + Assert.AreEqual(newKey, clientCore.SetApiKey(newKey).GetApiKey()); + } } } \ No newline at end of file diff --git a/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs b/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs index 5462834..82f41df 100644 --- a/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs +++ b/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs @@ -1,17 +1,72 @@ +using BuffettCodeCommon.Exception; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; using System.Runtime.Caching; +using System.Threading.Tasks; namespace BuffettCodeAPIClient.Tests { + class MockApiclientCore : IApiClientCore + { + private string apiKey; + public MockApiclientCore(string apiKey) + { + this.apiKey = apiKey; + } + + + public Task Get(ApiGetRequest request, bool isConfigureAwait) + { + return Task.FromResult(request.ToString()); + } + + public string GetApiKey() => this.apiKey; + public IApiClientCore SetApiKey(string apikey) + { + apiKey = apikey; + return this; + } + } + + class ErrorMockApiClientCore : IApiClientCore + { + private string apiKey; + public ErrorMockApiClientCore(string apikey) + { + this.apiKey = apikey; + } + + + public Task Get(ApiGetRequest request, bool isConfigureAwait) + { + throw new BuffettCodeApiClientException(); + } + + public string GetApiKey() => this.apiKey; + public IApiClientCore SetApiKey(string apikey) + { + apiKey = apikey; + return this; + } + } + [TestClass()] public class ApiClientCoreWithCacheTests { + private static ApiRequestCacheHelper CreateCheHelperForTest() + { + var cacheKey = new Random().Next().GetHashCode().ToString(); + return new ApiRequestCacheHelper(new MemoryCache(cacheKey), new Uri("http://example.com")); + } + [TestMethod()] public void GetAndUpdateApiKeyTest() { var initApiKey = @"init-key"; var newApiKey = @"new-key"; - var client = ApiClientCoreWithCache.Create(initApiKey, @"http://example.com", new MemoryCache(@"GetAndUpdateApiKeyTest")); + var mockApiClientCore = new MockApiclientCore(initApiKey); + var client = ApiClientCoreWithCache.Create(mockApiClientCore, CreateCheHelperForTest()); // test init value Assert.AreEqual(initApiKey, client.GetApiKey()); @@ -19,5 +74,28 @@ public void GetAndUpdateApiKeyTest() client.UpdateApiKey(newApiKey); Assert.AreEqual(newApiKey, client.GetApiKey()); } + + [TestMethod()] + public void GetTest() + { + var mockApiClientCore = new MockApiclientCore("dummy"); + var client = ApiClientCoreWithCache.Create(mockApiClientCore, CreateCheHelperForTest()); + ApiGetRequest request = new ApiGetRequest("dummy endpoint", new Dictionary()); + Assert.AreEqual(request.ToString(), client.Get(request, false, true)); + // use cache + Assert.AreEqual(request.ToString(), client.Get(request, false, true)); + + // don't use cache + Assert.AreEqual(request.ToString(), client.Get(request, false, false)); + } + + [TestMethod()] + public void GetExceptionTest() + { + var mockApiClientCore = new ErrorMockApiClientCore("dummy"); + var client = ApiClientCoreWithCache.Create(mockApiClientCore, CreateCheHelperForTest()); + ApiGetRequest request = new ApiGetRequest("dummy endpoint", new Dictionary()); + Assert.ThrowsException(() => client.Get(request, false, false)); + } } } \ No newline at end of file diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs index 5fcc0cd..23a6433 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV2ClientTests.cs @@ -1,5 +1,4 @@ using BuffettCodeCommon.Config; -using BuffettCodeCommon.Exception; using BuffettCodeCommon.Period; namespace BuffettCodeAPIClient.Tests @@ -27,9 +26,6 @@ public void GetQuarterTest() var period = FiscalQuarterPeriod.Create(2019, 4); // test api key can get ticker=xx01 Assert.IsNotNull(client.GetQuarter("6501", period, false, true, false)); - - // test api key can get ticker=xx02 - Assert.ThrowsException(() => client.GetQuarter("6502", period, false, true, false)); } [TestMethod()] @@ -37,9 +33,6 @@ public void GetIndicatorTest() { // test api key can get ticker=xx01 Assert.IsNotNull(client.GetIndicator("6501", true)); - - // test api key can get ticker=xx02 - Assert.ThrowsException(() => client.GetIndicator("6502", true, false)); } [TestMethod()] @@ -50,9 +43,6 @@ public void GetQuarterRangeTest() // test api key can get ticker=xx01 Assert.IsNotNull(client.GetQuarterRange("6501", from, to, false, false)); - - // test api key can get ticker=xx02 - Assert.ThrowsException(() => client.GetQuarterRange("6502", from, to, true, false)); } [TestMethod()] @@ -60,9 +50,6 @@ public void GetCompanyTest() { // test api key can get ticker=xx01 Assert.IsNotNull(client.GetCompany("6501", true, false)); - - // test api key can get ticker=xx02 - Assert.ThrowsException(() => client.GetCompany("6502", true, false)); } diff --git a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs index 2ff9791..d186d8d 100644 --- a/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs +++ b/BuffettCodeAPIClientTests/BuffettCodeApiV3ClientTests.cs @@ -1,5 +1,4 @@ using BuffettCodeCommon.Config; -using BuffettCodeCommon.Exception; using BuffettCodeCommon.Period; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -26,10 +25,6 @@ public void GetDailyTest() // test api key can get 01 data var day = DayPeriod.Create(2021, 2, 1); Assert.IsNotNull(client.GetDaily("6501", day, false, true, false)); - - // test api key can get not 01 - Assert.ThrowsException - (() => client.GetDaily("6502", day, true, true, false)); } } } \ No newline at end of file From 1f6ce5aac0f41ca47845f505671aa10513019846 Mon Sep 17 00:00:00 2001 From: Shu Suzuki Date: Wed, 3 Nov 2021 16:04:55 +0900 Subject: [PATCH 7/7] fix for review: https://github.com/BuffettCode/buffett-code-api-client-excel/pull/44#discussion_r741654960 --- BuffettCodeAPIClient/ApiClientCore.cs | 4 ++-- BuffettCodeAPIClient/IApiClientCore.cs | 2 +- BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/BuffettCodeAPIClient/ApiClientCore.cs b/BuffettCodeAPIClient/ApiClientCore.cs index 76396e9..8706733 100644 --- a/BuffettCodeAPIClient/ApiClientCore.cs +++ b/BuffettCodeAPIClient/ApiClientCore.cs @@ -68,9 +68,9 @@ public void Dispose() httpClient.Dispose(); } - public IApiClientCore SetApiKey(string apikey) + public IApiClientCore SetApiKey(string apiKey) { - apiKey = apikey; + this.apiKey = apiKey; return this; } diff --git a/BuffettCodeAPIClient/IApiClientCore.cs b/BuffettCodeAPIClient/IApiClientCore.cs index ccfd473..0537c32 100644 --- a/BuffettCodeAPIClient/IApiClientCore.cs +++ b/BuffettCodeAPIClient/IApiClientCore.cs @@ -6,7 +6,7 @@ public interface IApiClientCore { Task Get(ApiGetRequest request, bool isConfigureAwait); - IApiClientCore SetApiKey(string apikey); + IApiClientCore SetApiKey(string apiKey); string GetApiKey(); } diff --git a/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs b/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs index 82f41df..a9ee4ab 100644 --- a/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs +++ b/BuffettCodeAPIClientTests/ApiClientCoreWithCacheTests.cs @@ -22,9 +22,9 @@ public Task Get(ApiGetRequest request, bool isConfigureAwait) } public string GetApiKey() => this.apiKey; - public IApiClientCore SetApiKey(string apikey) + public IApiClientCore SetApiKey(string apiKey) { - apiKey = apikey; + this.apiKey = apiKey; return this; } } @@ -32,9 +32,9 @@ public IApiClientCore SetApiKey(string apikey) class ErrorMockApiClientCore : IApiClientCore { private string apiKey; - public ErrorMockApiClientCore(string apikey) + public ErrorMockApiClientCore(string apiKey) { - this.apiKey = apikey; + this.apiKey = apiKey; }