Skip to content

Commit

Permalink
Merge pull request #284 from EasyPost/backport_user_agent
Browse files Browse the repository at this point in the history
Backport User-Agent to V2
  • Loading branch information
Justintime50 authored Jul 1, 2022
2 parents ce139b0 + 250a750 commit a1ad09b
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 19 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,3 @@ jobs:
# Build the test project
- name: Build Solution
run: msbuild ${{ steps.test_project.outputs.test_file }}\${{ steps.test_project.outputs.test_file }}.csproj /p:platform="Any CPU" /p:configuration="Test" /p:outputPath="bin/Test" /p:target="Rebuild" -restore
# Run the tests
- name: Run Tests
run: vstest.console.exe ${{ steps.test_project.outputs.test_file }}\bin\Test\${{ steps.test_project.outputs.test_file }}.dll
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v2.8.3 (TBD)

* Backport improved User-Agent to collect OS details.

## v2.8.2 (2022-02-25)

* Fixes a bug where failure to retrieve Assembly information to populate the `User-Agent` header on some platforms/versions would result in the inability to make HTTP requests
Expand Down
3 changes: 3 additions & 0 deletions EasyPost.Net35/EasyPost.Net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@
<Compile Include="..\EasyPost\Resource.cs">
<Link>Resource.cs</Link>
</Compile>
<Compile Include="..\EasyPost\RuntimeInfo.cs">
<Link>RuntimeInfo.cs</Link>
</Compile>
<Compile Include="..\EasyPost\Message.cs">
<Link>Message.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions EasyPost.Net40/EasyPost.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
<Compile Include="..\EasyPost\Error.cs">
<Link>Error.cs</Link>
</Compile>
<Compile Include="..\EasyPost\RuntimeInfo.cs">
<Link>RuntimeInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="..\EasyPost\EasyPost.pfx">
Expand Down
3 changes: 3 additions & 0 deletions EasyPost.NetCore20/EasyPost.NetCore20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@
<Compile Include="..\EasyPost\Error.cs">
<Link>Error.cs</Link>
</Compile>
<Compile Include="..\EasyPost\RuntimeInfo.cs">
<Link>RuntimeInfo.cs</Link>
</Compile>
<Compile Include="..\EasyPost\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions EasyPost.NetCore31/EasyPost.NetCore31.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
<Compile Include="..\EasyPost\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="..\EasyPost\RuntimeInfo.cs">
<Link>RuntimeInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="..\EasyPost\EasyPost.pfx">
Expand Down
26 changes: 10 additions & 16 deletions EasyPost/Client.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using Newtonsoft.Json;
using RestSharp;

Expand All @@ -17,6 +15,9 @@ public class Client

private readonly string _dotNetVersion;
private readonly string _libraryVersion;
private readonly string _osArch;
private readonly string _osName;
private readonly string _osVersion;

private readonly RestClient _restClient;
private int? _connectTimeoutMilliseconds;
Expand All @@ -34,7 +35,7 @@ public int RequestTimeoutMilliseconds
set => _requestTimeoutMilliseconds = value;
}

private string UserAgent => $"EasyPost/v2 CSharpClient/{_libraryVersion} .NET/{_dotNetVersion}";
private string UserAgent => $"EasyPost/v2 CSharpClient/{_libraryVersion} .NET/{_dotNetVersion} OS/{_osName} OSVersion/{_osVersion} OSArch/{_osArch}";

/// <summary>
/// Constructor for the EasyPost client.
Expand All @@ -45,6 +46,12 @@ public Client(ClientConfiguration clientConfiguration)
ServicePointManager.SecurityProtocol |= Security.GetProtocol();
_configuration = clientConfiguration ?? throw new ArgumentNullException("clientConfiguration");

_libraryVersion = RuntimeInfo.ApplicationInfo.ApplicationVersion;
_dotNetVersion = RuntimeInfo.ApplicationInfo.DotNetVersion;
_osName = RuntimeInfo.OperationSystemInfo.Name;
_osVersion = RuntimeInfo.OperationSystemInfo.Version;
_osArch = RuntimeInfo.OperationSystemInfo.Architecture;

_restClient = new RestClient(clientConfiguration.ApiBase);
_restClient.Timeout = ConnectTimeoutMilliseconds;

Expand All @@ -57,19 +64,6 @@ public Client(ClientConfiguration clientConfiguration)
{
_libraryVersion = "Unknown";
}

string dotNetVersion = Environment.Version.ToString();
if (dotNetVersion == "4.0.30319.42000")
{
/*
* We're on a v4.6+ version (or pre-.NET Core 3.0, which we don't support),
* but we can't get the exact version.
* See: https://docs.microsoft.com/en-us/dotnet/api/system.environment.version?view=net-6.0#remarks
*/
dotNetVersion = "4.6 or higher";
}

_dotNetVersion = dotNetVersion;
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions EasyPost/EasyPost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Report.cs" />
<Compile Include="ReportList.cs" />
<Compile Include="Resource.cs" />
<Compile Include="RuntimeInfo.cs"/>
<Compile Include="Smartrate.cs" />
<Compile Include="TimeInTransit.cs" />
<Compile Include="TrackerList.cs" />
Expand Down
124 changes: 124 additions & 0 deletions EasyPost/RuntimeInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Diagnostics;
using System.Reflection;

namespace EasyPost
{
public static class RuntimeInfo
{
internal struct ApplicationInfo
{
/// <summary>
/// Get the version of the application as a string.
/// </summary>
/// <returns>The version of the application as a string.</returns>
internal static string ApplicationVersion
{
get
{
try
{
Assembly assembly = typeof(ApplicationInfo).Assembly;
FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
return info.FileVersion ?? "Unknown";
}
catch (Exception)
{
return "Unknown";
}
}
}

/// <summary>
/// Get the .NET framework version as a string.
/// </summary>
/// <returns>The .NET framework version as a string.</returns>
internal static string DotNetVersion
{
get
{
Version dotNetVersion = Environment.Version;
if (dotNetVersion == null)
{
/*
* We're on a pre-4.0 .NET Framework version, where Environment.Version is null.
*/
return "3.5-";
}

string versionString = dotNetVersion.ToString();
if (versionString == "4.0.30319.42000")
{
/*
* We're on a v4.6+ version (or pre-.NET Core 3.0, which we don't support),
* but we can't get the exact version.
* See: https://docs.microsoft.com/en-us/dotnet/api/system.environment.version?view=net-6.0#remarks
*/
versionString = "4.6+";
}

return versionString;
}
}
}

internal struct OperationSystemInfo
{
/// <summary>
/// Get details about the operating system.
/// </summary>
/// <returns>Details about the operating system.</returns>
private static OperatingSystem OperatingSystem
{
get { return Environment.OSVersion; }
}

/// <summary>
/// Get the name of the operating system.
/// </summary>
/// <returns>Name of the operating system.</returns>
internal static string Name
{
get
{
switch (OperatingSystem.Platform)
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
return "Windows";
case PlatformID.Unix:
return "Linux";
case PlatformID.MacOSX: // in newer versions, Mac OS X is PlatformID.Unix unfortunately
return "Darwin";
default:
return "Unknown";
}
}
}

/// <summary>
/// Get the version of the operating system.
/// </summary>
/// <returns>Version of the operating system.</returns>
internal static string Version
{
get { return OperatingSystem.Version.ToString(); }
}

/// <summary>
/// Get the architecture of the operating system.
/// </summary>
/// <returns>Architecture of the operating system.</returns>
internal static string Architecture
{
get
{
// Because of how this library uses parent files, we can't easily target different frameworks and use different functions to get the architecture.
return "Unknown";
}
}
}
}
}

0 comments on commit a1ad09b

Please sign in to comment.