Skip to content

Commit

Permalink
GUARD-2926-add-shipstation-logging (#10)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Victoria-SV and maxim-saltanov-skuvault authored May 4, 2023
1 parent 10d57ac commit bcab09a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ task NuGet Package, Version, {
<owners>Agile Harbor</owners>
<projectUrl>https://github.com/agileharbor/$project_name</projectUrl>
<licenseUrl>https://raw.github.com/agileharbor/$project_name/master/License.txt</licenseUrl>
<repository type="git" url="https://github.com/skuvault-integrations/shipStationAccess.git" />
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<copyright>Copyright (C) Agile Harbor, LLC</copyright>
<summary>$text</summary>
Expand Down Expand Up @@ -112,4 +113,4 @@ task NuGet Package, Version, {
}
}

task . Init, Build, Package, Zip, NuGet
task . Init, Build, Package, NuGet
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ _ReSharper*/
[Tt]est[Rr]esult*
*.ReSharper
*.docstates
.idea/
.vs/

#Ignore common build files
build
Expand Down
2 changes: 1 addition & 1 deletion src/Global/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
// [assembly: AssemblyVersion("1.0.*")]

// Keep in track with CA API version
[ assembly : AssemblyVersion( "1.4.12.0" ) ]
[ assembly : AssemblyVersion( "1.5.1" ) ]
14 changes: 14 additions & 0 deletions src/ShipStationAccess/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics;
using System.Reflection;

namespace ShipStationAccess
{
/// <summary>
/// Project-wise constants.
/// </summary>
internal static class Constants
{
public static readonly string VersionInfo = FileVersionInfo.GetVersionInfo( Assembly.GetExecutingAssembly().Location ).FileVersion;
public const string IntegrationName = "shipstation";
}
}
1 change: 1 addition & 0 deletions src/ShipStationAccess/ShipStationAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="..\Global\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Constants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ShipStationFactory.cs" />
<Compile Include="V2\Exceptions\ShipStationUnrecoverableException.cs" />
Expand Down
36 changes: 28 additions & 8 deletions src/ShipStationAccess/V2/Services/WebRequestServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit bcab09a

Please sign in to comment.