Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BuffettCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuffettCodeAddinRibbonTests", "BuffettCodeAddinRibbonTests\BuffettCodeAddinRibbonTests.csproj", "{00A6A593-A87D-4DD2-8B06-8A36CFC6C3AA}"
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "SetupAddinRibbon32", "SetupAddinRibbon32\SetupAddinRibbon32.wixproj", "{E1016C59-0448-4BFA-B89D-BB3C960F1F16}"
ProjectSection(ProjectDependencies) = postProject
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build が不安定だったので(成果物がないって言われる)、 Setup 系の build は各テストが終わってからにします

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見てる感じ、これでだいぶ安定した(なんでや・・・

{00A6A593-A87D-4DD2-8B06-8A36CFC6C3AA} = {00A6A593-A87D-4DD2-8B06-8A36CFC6C3AA}
EndProjectSection
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "SetupExcelFunctions32", "SetupExcelFunctions32\SetupExcelFunctions32.wixproj", "{9D5AFF0E-0EA3-4318-8BE4-941FC1DAF261}"
ProjectSection(ProjectDependencies) = postProject
{AD8A3F1E-584E-46C1-ACEE-A749433B99B2} = {AD8A3F1E-584E-46C1-ACEE-A749433B99B2}
EndProjectSection
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "BuffettCodeExcelAddin32Installer", "BuffettCodeExcelAddin32Installer\BuffettCodeExcelAddin32Installer.wixproj", "{4D7EB0F3-E548-42EF-A99E-6C059800C654}"
EndProject
Expand Down
14 changes: 1 addition & 13 deletions BuffettCodeAPIClient/ApiClientCore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BuffettCodeCommon.Exception;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,17 +46,7 @@ public async Task<string> Get(ApiGetRequest request, bool isConfigureAwait)
{
if (!response.IsSuccessStatusCode)
{
switch ((int)response.StatusCode)
{
case (int)HttpStatusCode.Forbidden:
throw new InvalidAPIKeyException($"request={request}");
case (int)HttpStatusCode.NotFound:
throw new ResourceNotFoundException($"request={request}");
case 429: // Quota Error
throw new QuotaException($"request={request}");
default:
throw new BuffettCodeApiClientException($"request={request}");
}
GetRequestErrorHandler.Handle(request, response);
}
var content = response.Content.ReadAsStringAsync().Result;
return content;
Expand Down
2 changes: 2 additions & 0 deletions BuffettCodeAPIClient/BuffettCodeAPIClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL" />
Expand All @@ -61,6 +62,7 @@
<Compile Include="ApiRequestCacheHelper.cs" />
<Compile Include="ApiClientCoreWithCache.cs" />
<Compile Include="ApiGetResponseValidator.cs" />
<Compile Include="GetRequestErrorHandler.cs" />
<Compile Include="TickerEmptyPeriodParameter.cs" />
<Compile Include="TickerPeriodRangeParameter.cs" />
<Compile Include="TickerDayParameter.cs" />
Expand Down
76 changes: 76 additions & 0 deletions BuffettCodeAPIClient/GetRequestErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using BuffettCodeCommon.Exception;
using System.Collections.Immutable;
using System.Net;
using System.Net.Http;

namespace BuffettCodeAPIClient
{
enum GetHttpStatusErrorCode
Comment thread
akiomik marked this conversation as resolved.
{
Forbidden = HttpStatusCode.Forbidden,
NotFound = HttpStatusCode.NotFound,
BadRequest = HttpStatusCode.BadRequest,
TooManyRequests = 429,
}

static class TestApiTokenErrorMessage
{
const string ExceedingTestingApiTickerLimist = @"{""message"":""Testing Apikey is only allowed to ticker ending with \""01\""""}";
const string ExceedingTraialApiTickerLimit = "{\"message\":\"Trial request only supports ticker ending '01'\"}";
const string TestingApiKeyIsNotAllowed = "{\"message\":\"Testing apikey is not allowed\"}";
public static ImmutableHashSet<string> KnownErrorMessages = ImmutableHashSet.Create<string>(new string[] { ExceedingTestingApiTickerLimist, ExceedingTraialApiTickerLimit, TestingApiKeyIsNotAllowed });
}

static class InvalidAPIKeyErrorMessage
{
public const string ApiGatewayDefault = "{\"message\":\"Forbidden\"}";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前にまよったけど、一番現実に即した名前を付けてあります


}


public class GetRequestErrorHandler
{
public static void Handle(ApiGetRequest request, HttpResponseMessage errorResponse)
{
switch ((int)errorResponse.StatusCode)
{
case (int)GetHttpStatusErrorCode.Forbidden:
throw CreateExceptionForForbidden(request, errorResponse);
case (int)GetHttpStatusErrorCode.NotFound:
throw new ResourceNotFoundException($"request={request}");
case (int)GetHttpStatusErrorCode.TooManyRequests:
throw new DailyQuotaException($"request={request}");
case (int)GetHttpStatusErrorCode.BadRequest:
throw CreateExceptionforBadRequest(request,
errorResponse);
default:
throw new BuffettCodeApiClientException($"request={request}");
}
}

private static BuffettCodeApiClientException CreateExceptionForForbidden(ApiGetRequest request, HttpResponseMessage errorResponse)
{
switch (errorResponse.Content.ReadAsStringAsync().Result)
{
case InvalidAPIKeyErrorMessage.ApiGatewayDefault:
return new InvalidAPIKeyException($"request={request}");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ネストしてるやつは return するようにしたのね

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ですです

default:
return new ApiMonthlyLimitExceededException($"request={request}");
}
}

private static BuffettCodeApiClientException CreateExceptionforBadRequest(ApiGetRequest request, HttpResponseMessage errorResponse)
Comment thread
akiomik marked this conversation as resolved.
{
if (TestApiTokenErrorMessage.KnownErrorMessages.Contains(errorResponse.Content.ReadAsStringAsync().Result))
{
return new TestAPIConstraintException($"request={request}");
}
else
{
return new BuffettCodeApiClientException($"request={request}");
Comment on lines +66 to +70
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こっちも return するようにしてあります


}

}
}
}
1 change: 1 addition & 0 deletions BuffettCodeAPIClientTests/BuffettCodeAPIClientTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="BuffettCodeApiV3RequestCreatorTests.cs" />
<Compile Include="ApiRequestCacheHelperTests.cs" />
<Compile Include="CacheKeyCreatorTests.cs" />
<Compile Include="GetRequestErrorHandlerTests.cs" />
<Compile Include="TickerDayParameterTests.cs" />
<Compile Include="TickerEmptyPeriodParameterTests.cs" />
<Compile Include="ErrorMockApiClientCore.cs" />
Expand Down
68 changes: 68 additions & 0 deletions BuffettCodeAPIClientTests/GetRequestErrorHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using BuffettCodeCommon.Exception;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;

namespace BuffettCodeAPIClient.Tests
{
[TestClass()]
public class GetRequestErrorHandlerTests
{

private readonly ApiGetRequest Request = new ApiGetRequest("dummy", new Dictionary<string, string>());

private static HttpResponseMessage CreateResponseMessage(HttpStatusCode statusCode, string content)
{
var response = new HttpResponseMessage(statusCode);
response.Content = new StringContent(content);
return response;
}

[TestMethod()]
public void HandleInvalidApiKeyError()
{
var response = CreateResponseMessage(HttpStatusCode.Forbidden, "{\"message\":\"Forbidden\"}");
Assert.ThrowsException<InvalidAPIKeyException>(() => GetRequestErrorHandler.Handle(Request, response));
}

[TestMethod()]
public void HandleInvalidApiMonthlyLimitExceededException()
{
var response = CreateResponseMessage(HttpStatusCode.Forbidden, "dummy");
Assert.ThrowsException<ApiMonthlyLimitExceededException>(() => GetRequestErrorHandler.Handle(Request, response));
}

[TestMethod()]
public void HandleResourceNotFound()
{
var response = CreateResponseMessage(HttpStatusCode.NotFound, "");
Assert.ThrowsException<ResourceNotFoundException>(() => GetRequestErrorHandler.Handle(Request, response));
}


[TestMethod()]
public void HandleTestAPIConstraintException()
{
var response = CreateResponseMessage(HttpStatusCode.BadRequest, "{\"message\":\"Testing apikey is not allowed\"}");
Assert.ThrowsException<TestAPIConstraintException>(() => GetRequestErrorHandler.Handle(Request, response));
}

[TestMethod()]
public void HandleTestBadRequest()
{
var response = CreateResponseMessage(HttpStatusCode.BadRequest, "unknown error");
Assert.ThrowsException<BuffettCodeApiClientException>(() => GetRequestErrorHandler.Handle(Request, response));
}

[TestMethod()]
public void HandleTestDefaultException()
{
var response = CreateResponseMessage(HttpStatusCode.InternalServerError, "unknown error");
Assert.ThrowsException<BuffettCodeApiClientException>(() => GetRequestErrorHandler.Handle(Request, response));
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static string ToMessageBoxString(BaseBuffettCodeException exception)
{
return "テスト用のAPIキーでは末尾が01の銘柄コードのみ使用できます。";
}
else if (exception is QuotaException)
else if (exception is DailyQuotaException)
{
return "APIの実行回数が上限に達しました。";
return "一日当たりのAPIの実行回数が上限に達しました。";
}
else if (exception is InvalidAPIKeyException)
{
Expand All @@ -40,6 +40,10 @@ public static string ToMessageBoxString(BaseBuffettCodeException exception)
{
return $"存在しないデータにアクセスしようとしています。";
}
else if (exception is ApiMonthlyLimitExceededException)
{
return "今月のAPIの実行回数が上限に達しました。";
Comment thread
akiomik marked this conversation as resolved.
}
else
{
return $"データの取得中にエラーが発生しました。";
Expand Down
2 changes: 1 addition & 1 deletion BuffettCodeAddinRibbon/CsvDownload/TabularWriterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ITabularWriter<T> Build()
case TabularOutputDestination.NewCsvFile:
return BuildCsvFileTabluarWriter(true);
case TabularOutputDestination.NewWorksheet:
Worksheet worksheet = Globals.ThisAddIn.Application.Worksheets.Add();
Worksheet worksheet = Globals.ThisAddIn.Application.Worksheets.Add();
return BuildWorksheetTabularWriter(worksheet);

// for unit testing
Expand Down
1 change: 1 addition & 0 deletions BuffettCodeCommon/Config/BuffettCodeApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public static class ApiRelatedUrlConfig
public static readonly string API_SPECIAL_NOTES = "https://www.buffett-code.com/legal/web_api/special_notes";
public static readonly string ONDEMAND_API_USAGE_ENTRY = "https://blog.buffett-code.com/entry/ondemand_api_usage";
}

}
18 changes: 14 additions & 4 deletions BuffettCodeCommon/Exception/BuffettCodeApiClientException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public InvalidAPIKeyException(string message) : base(message) { }
public InvalidAPIKeyException(string message, Exception inner) : base(message, inner) { }
}

public class QuotaException : BuffettCodeApiClientException
public class DailyQuotaException : BuffettCodeApiClientException
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS Gateway の daily quota によるものなので、 DailyQuotaException に rename しました

{
public QuotaException() : base() { }
public DailyQuotaException() : base() { }

public QuotaException(string message) : base(message) { }
public DailyQuotaException(string message) : base(message) { }

public QuotaException(string message, Exception inner) : base(message, inner) { }
public DailyQuotaException(string message, Exception inner) : base(message, inner) { }

}

Expand All @@ -50,4 +50,14 @@ public ResourceNotFoundException(string message, Exception inner) : base(message

}

public class ApiMonthlyLimitExceededException : BuffettCodeApiClientException
{
public ApiMonthlyLimitExceededException() : base() { }

public ApiMonthlyLimitExceededException(string message) : base(message) { }

public ApiMonthlyLimitExceededException(string message, Exception inner) : base(message, inner) { }

}

}
8 changes: 6 additions & 2 deletions BuffettCodeExcelFunctions/BCodeFunctionErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static string ToErrorMessage(Exception e, string propertyName, bool isDeb
{
message = $"指定された項目が見つかりません:{propertyName}";
}
else if (bce is QuotaException)
else if (bce is DailyQuotaException)
{
message = "APIの実行回数が上限に達しました";
message = "一日当たりのAPIの実行回数が上限に達しました";
}
else if (bce is InvalidAPIKeyException)
{
Expand All @@ -51,6 +51,10 @@ public static string ToErrorMessage(Exception e, string propertyName, bool isDeb
{
message = $"存在しないデータにアクセスしようとしています。::{bce.Message}";
}
else if (bce is ApiMonthlyLimitExceededException)
Comment thread
akiomik marked this conversation as resolved.
{
message = $"今月のAPIの実行回数が上限に達しました::{bce.Message}";
}
else if (bce is BuffettCodeApiClientException)
{
message = "APIの呼び出しでエラーが発生しました";
Expand Down
4 changes: 4 additions & 0 deletions SetupAddinRibbon32/HeatGeneratedAddinRibbon.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Component Id="cmp58DACAD6E5ECA27D45E862422C36391A" Guid="*">
<File Id="fil85D2AAC1FD1C3BCA4D56A89865C93BF7" KeyPath="yes" Source="$(var.RibbonHarvestPath)\System.Buffers.xml" />
</Component>
<Component Id="cmpFB525AB8CB9FAB0057B5359FFF6543A1" Guid="*">
<File Id="fil5088F94E71D948D60D111B42E87143D6" KeyPath="yes" Source="$(var.RibbonHarvestPath)\System.Collections.Immutable.dll" />
</Component>
<Component Id="cmp5A904851F9DE7369E243943F4E5027FA" Guid="*">
<File Id="fil5EC8BD617ECC4CAA66A5310AFD181E04" KeyPath="yes" Source="$(var.RibbonHarvestPath)\System.Memory.dll" />
</Component>
Expand Down Expand Up @@ -110,6 +113,7 @@
<ComponentRef Id="cmp196F1D763CBB036DA160606D8D0E27DA" />
<ComponentRef Id="cmpAD568283CFD6A75645ED0ECDD761D85E" />
<ComponentRef Id="cmp58DACAD6E5ECA27D45E862422C36391A" />
<ComponentRef Id="cmpFB525AB8CB9FAB0057B5359FFF6543A1" />
<ComponentRef Id="cmp5A904851F9DE7369E243943F4E5027FA" />
<ComponentRef Id="cmp72CFC8F9FE5280FECF3FC56AB41DA067" />
<ComponentRef Id="cmpB5468F5DF6425D31E8036A019997ED81" />
Expand Down
16 changes: 16 additions & 0 deletions SetupExcelFunctions32/HeatGeneratedExcelFunctions.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
<Component Id="cmp196F1D763CBB036DA160606D8D0E27DA" Guid="*">
<File Id="fil83D9BCFD839F35A59CAF8AB3AB656AE9" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\Newtonsoft.Json.xml" />
</Component>
<Component Id="cmpAD568283CFD6A75645ED0ECDD761D85E" Guid="*">
<File Id="filA12D9DE081467DACFD3B4C1025434C4C" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\System.Buffers.dll" />
</Component>
<Component Id="cmpFB525AB8CB9FAB0057B5359FFF6543A1" Guid="*">
<File Id="fil5088F94E71D948D60D111B42E87143D6" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\System.Collections.Immutable.dll" />
</Component>
<Component Id="cmp5A904851F9DE7369E243943F4E5027FA" Guid="*">
<File Id="fil5EC8BD617ECC4CAA66A5310AFD181E04" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\System.Memory.dll" />
</Component>
<Component Id="cmpB5468F5DF6425D31E8036A019997ED81" Guid="*">
<File Id="filB997D5ACF1187050162A1467E8B6D9B7" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\System.Numerics.Vectors.dll" />
</Component>
<Component Id="cmp676B532506E78F84084C12B1FAE054E3" Guid="*">
<File Id="fil9B24D665A20C2F5F03044136BBBE054F" KeyPath="yes" Source="$(var.FunctionsHarvestPath)\System.ValueTuple.dll" />
</Component>
Expand Down Expand Up @@ -112,6 +124,10 @@
<ComponentRef Id="cmp2068870D6EA8EBA19E43D6369C5CF0CA" />
<ComponentRef Id="cmp6616CB17E4922802E5C84B9C16935052" />
<ComponentRef Id="cmp196F1D763CBB036DA160606D8D0E27DA" />
<ComponentRef Id="cmpAD568283CFD6A75645ED0ECDD761D85E" />
<ComponentRef Id="cmpFB525AB8CB9FAB0057B5359FFF6543A1" />
<ComponentRef Id="cmp5A904851F9DE7369E243943F4E5027FA" />
<ComponentRef Id="cmpB5468F5DF6425D31E8036A019997ED81" />
<ComponentRef Id="cmp676B532506E78F84084C12B1FAE054E3" />
<ComponentRef Id="cmp693DF999232B83CFADE3AF4AFB0A0D94" />
</ComponentGroup>
Expand Down