From bcab09a22ec8f48554b9d8f75d2a85ac13914172 Mon Sep 17 00:00:00 2001 From: Victoria Felice <79600432+Victoria-SV@users.noreply.github.com> Date: Thu, 4 May 2023 09:43:50 -0400 Subject: [PATCH] GUARD-2926-add-shipstation-logging (#10) * GUARD-2926: add logging prior to throwing ShipStationUnauthorizedException * GUARD-2926: remove duplicate * GUARD-2926 improved logs in the ThrowIfError method, added the logging of the version number (channel name and version moved to the Constants) * GUARD-2926 updated package version * GUARD-2926 updated build.ps1 with correct repositoryUrl * GUARD-2926 renaming PR fixes * GUARD-2926: add truncated key and destruct * GUARD-2926: increment version * GUARD-2926: remove alpha * GUARD-2926: change version --------- Co-authored-by: maxim.saltanov --- .build.ps1 | 3 +- .gitignore | 2 ++ src/Global/GlobalAssemblyInfo.cs | 2 +- src/ShipStationAccess/Constants.cs | 14 ++++++++ .../ShipStationAccess.csproj | 1 + .../V2/Services/WebRequestServices.cs | 36 ++++++++++++++----- 6 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 src/ShipStationAccess/Constants.cs diff --git a/.build.ps1 b/.build.ps1 index e15a2cd..f8bc35c 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -81,6 +81,7 @@ task NuGet Package, Version, { Agile Harbor https://github.com/agileharbor/$project_name https://raw.github.com/agileharbor/$project_name/master/License.txt + false Copyright (C) Agile Harbor, LLC $text @@ -112,4 +113,4 @@ task NuGet Package, Version, { } } -task . Init, Build, Package, Zip, NuGet \ No newline at end of file +task . Init, Build, Package, NuGet \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9065a35..7d49b95 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,8 @@ _ReSharper*/ [Tt]est[Rr]esult* *.ReSharper *.docstates +.idea/ +.vs/ #Ignore common build files build diff --git a/src/Global/GlobalAssemblyInfo.cs b/src/Global/GlobalAssemblyInfo.cs index d56860e..c52f6c9 100644 --- a/src/Global/GlobalAssemblyInfo.cs +++ b/src/Global/GlobalAssemblyInfo.cs @@ -23,4 +23,4 @@ // [assembly: AssemblyVersion("1.0.*")] // Keep in track with CA API version -[ assembly : AssemblyVersion( "1.4.12.0" ) ] \ No newline at end of file +[ assembly : AssemblyVersion( "1.5.1" ) ] \ No newline at end of file diff --git a/src/ShipStationAccess/Constants.cs b/src/ShipStationAccess/Constants.cs new file mode 100644 index 0000000..fe045bc --- /dev/null +++ b/src/ShipStationAccess/Constants.cs @@ -0,0 +1,14 @@ +using System.Diagnostics; +using System.Reflection; + +namespace ShipStationAccess +{ + /// + /// Project-wise constants. + /// + internal static class Constants + { + public static readonly string VersionInfo = FileVersionInfo.GetVersionInfo( Assembly.GetExecutingAssembly().Location ).FileVersion; + public const string IntegrationName = "shipstation"; + } +} \ No newline at end of file diff --git a/src/ShipStationAccess/ShipStationAccess.csproj b/src/ShipStationAccess/ShipStationAccess.csproj index d47fbe0..079af71 100644 --- a/src/ShipStationAccess/ShipStationAccess.csproj +++ b/src/ShipStationAccess/ShipStationAccess.csproj @@ -81,6 +81,7 @@ Properties\GlobalAssemblyInfo.cs + diff --git a/src/ShipStationAccess/V2/Services/WebRequestServices.cs b/src/ShipStationAccess/V2/Services/WebRequestServices.cs index f3a20f2..2e6b1ad 100644 --- a/src/ShipStationAccess/V2/Services/WebRequestServices.cs +++ b/src/ShipStationAccess/V2/Services/WebRequestServices.cs @@ -268,16 +268,36 @@ private async Task< string > PostRawDataAsync( string url, string payload, Cance private void ThrowIfError( string url, HttpResponseMessage responseMessage, string responseContent ) { var serverStatusCode = responseMessage.StatusCode; - - if ( serverStatusCode == HttpStatusCode.Unauthorized ) - throw new ShipStationUnauthorizedException(); - - if ( IsRequestThrottled( responseMessage, responseContent, out int rateResetInSeconds ) ) + var apiKey = this._credentials.ApiKey; + const int maxApiKeyChars = 10; + + if( serverStatusCode == HttpStatusCode.Unauthorized ) { - ShipStationLogger.Log.Info( "[shipstation]\tResponse for apiKey '{apiKey}' and url '{uri}':\n{resetInSeconds} - {isThrottled}\n{response}", - this._credentials.ApiKey, url, rateResetInSeconds, true, responseContent ); - throw new ShipStationThrottleException( rateResetInSeconds ); + // All ApiKey information will be removed later in GUARD-2930. For now, we only have this field to identify the account. + var apiKeyFirstTen = apiKey?.Length > 10 ? apiKey.Substring(0, maxApiKeyChars) + "..." : apiKey; + ShipStationLogger.Log.Info( "[{IntegrationName}] [{Version}] [{TruncatedApiKey}]\tRequest to '{Url}' returned HTTP Error with the response content: '{ResponseContent}'. Request Headers: {@RequestMessageHeaders}, Response Headers: {@ResponseMessageHeaders}", + Constants.IntegrationName, + Constants.VersionInfo, + apiKeyFirstTen, + url, + responseContent, + responseMessage?.RequestMessage?.Headers, + responseMessage?.Headers ); + throw new ShipStationUnauthorizedException(); } + + if( !this.IsRequestThrottled( responseMessage, responseContent, out int rateResetInSeconds ) ) + return; + + ShipStationLogger.Log.Info( "[{IntegrationName}] [{Version}]\tResponse for apiKey '{ApiKey}' and url '{Uri}':\n{ResetInSeconds} - {IsThrottled}\n{Response}", + Constants.IntegrationName, + Constants.VersionInfo, + this._credentials.ApiKey, + url, + rateResetInSeconds, + true, + responseContent ); + throw new ShipStationThrottleException( rateResetInSeconds ); } private bool IsRequestThrottled( HttpResponseMessage responseMessage, string responseContent, out int rateResetInSeconds )