From 5115e2e89e80ff8026c12719fd84e913cb8fc48c Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 29 Jun 2022 15:57:23 -0600 Subject: [PATCH 1/5] - Backport OS details in User-Agent --- CHANGELOG.md | 4 ++ EasyPost/Client.cs | 26 ++++----- EasyPost/EasyPost.csproj | 1 + EasyPost/RuntimeInfo.cs | 115 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 EasyPost/RuntimeInfo.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index c1f9e28e9..9be4a52dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/EasyPost/Client.cs b/EasyPost/Client.cs index 64f8efe50..a8c53a78c 100644 --- a/EasyPost/Client.cs +++ b/EasyPost/Client.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Net; -using System.Reflection; using Newtonsoft.Json; using RestSharp; @@ -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; @@ -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}"; /// /// Constructor for the EasyPost client. @@ -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; @@ -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; } /// diff --git a/EasyPost/EasyPost.csproj b/EasyPost/EasyPost.csproj index 5008f4868..219c60f26 100644 --- a/EasyPost/EasyPost.csproj +++ b/EasyPost/EasyPost.csproj @@ -98,6 +98,7 @@ + diff --git a/EasyPost/RuntimeInfo.cs b/EasyPost/RuntimeInfo.cs new file mode 100644 index 000000000..ee6016320 --- /dev/null +++ b/EasyPost/RuntimeInfo.cs @@ -0,0 +1,115 @@ +using System; +using System.Diagnostics; +using System.Reflection; + +namespace EasyPost +{ + internal static class RuntimeInfo + { + internal struct ApplicationInfo + { + /// + /// Get the version of the application as a string. + /// + /// The version of the application as a string. + 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"; + } + } + } + + /// + /// Get the .NET framework version as a string. + /// + /// The .NET framework version as a string. + internal static string DotNetVersion + { + get + { + 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"; + } + + return dotNetVersion; + } + } + } + + internal struct OperationSystemInfo + { + /// + /// Get details about the operating system. + /// + /// Details about the operating system. + private static OperatingSystem OperatingSystem + { + get { return Environment.OSVersion; } + } + + /// + /// Get the name of the operating system. + /// + /// Name of the operating system. + 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"; + } + } + } + + /// + /// Get the version of the operating system. + /// + /// Version of the operating system. + internal static string Version + { + get { return OperatingSystem.Version.ToString(); } + } + + /// + /// Get the architecture of the operating system. + /// + /// Architecture of the operating system. + internal static string Architecture + { + get + { + // Sorry, Windows ARM users (if you exist), best we can do is determine if we are running on a 64-bit or 32-bit + return Environment.Is64BitOperatingSystem ? "x64" : "x86"; + } + } + } + } +} From 13d387a7dde1840188d3a1cdcb29ec105896f1a7 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 29 Jun 2022 16:14:21 -0600 Subject: [PATCH 2/5] - Fix references to source file - Can't calculate architecture in older frameworks reliably --- EasyPost.Net35/EasyPost.Net35.csproj | 3 +++ EasyPost.Net40/EasyPost.Net40.csproj | 3 +++ EasyPost.NetCore20/EasyPost.NetCore20.csproj | 3 +++ EasyPost.NetCore31/EasyPost.NetCore31.csproj | 3 +++ EasyPost/RuntimeInfo.cs | 6 +++--- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/EasyPost.Net35/EasyPost.Net35.csproj b/EasyPost.Net35/EasyPost.Net35.csproj index 85a112fe6..419597e74 100644 --- a/EasyPost.Net35/EasyPost.Net35.csproj +++ b/EasyPost.Net35/EasyPost.Net35.csproj @@ -164,6 +164,9 @@ Resource.cs + + RuntimeInfo.cs + Message.cs diff --git a/EasyPost.Net40/EasyPost.Net40.csproj b/EasyPost.Net40/EasyPost.Net40.csproj index 638f34a32..cc8658d44 100644 --- a/EasyPost.Net40/EasyPost.Net40.csproj +++ b/EasyPost.Net40/EasyPost.Net40.csproj @@ -235,6 +235,9 @@ Error.cs + + RuntimeInfo.cs + diff --git a/EasyPost.NetCore20/EasyPost.NetCore20.csproj b/EasyPost.NetCore20/EasyPost.NetCore20.csproj index 3f11bf9e0..b8263f1eb 100644 --- a/EasyPost.NetCore20/EasyPost.NetCore20.csproj +++ b/EasyPost.NetCore20/EasyPost.NetCore20.csproj @@ -205,6 +205,9 @@ Error.cs + + RuntimeInfo.cs + VersionInfo.cs diff --git a/EasyPost.NetCore31/EasyPost.NetCore31.csproj b/EasyPost.NetCore31/EasyPost.NetCore31.csproj index a51e46d48..ca381364a 100644 --- a/EasyPost.NetCore31/EasyPost.NetCore31.csproj +++ b/EasyPost.NetCore31/EasyPost.NetCore31.csproj @@ -208,6 +208,9 @@ VersionInfo.cs + + RuntimeInfo.cs + diff --git a/EasyPost/RuntimeInfo.cs b/EasyPost/RuntimeInfo.cs index ee6016320..29384aad7 100644 --- a/EasyPost/RuntimeInfo.cs +++ b/EasyPost/RuntimeInfo.cs @@ -4,7 +4,7 @@ namespace EasyPost { - internal static class RuntimeInfo + public static class RuntimeInfo { internal struct ApplicationInfo { @@ -106,8 +106,8 @@ internal static string Architecture { get { - // Sorry, Windows ARM users (if you exist), best we can do is determine if we are running on a 64-bit or 32-bit - return Environment.Is64BitOperatingSystem ? "x64" : "x86"; + // 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"; } } } From 63fd87f16426e1a14181a385476f43fd6638330d Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Wed, 29 Jun 2022 16:20:20 -0600 Subject: [PATCH 3/5] Use "4.6+" instead of "4.6 or higher" to remove spaces --- EasyPost/RuntimeInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EasyPost/RuntimeInfo.cs b/EasyPost/RuntimeInfo.cs index 29384aad7..5a9a2eaa9 100644 --- a/EasyPost/RuntimeInfo.cs +++ b/EasyPost/RuntimeInfo.cs @@ -45,7 +45,7 @@ internal static string DotNetVersion * 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 = "4.6+"; } return dotNetVersion; From f84b4a65aea4f84fb92831a17d631a07849d3c32 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Thu, 30 Jun 2022 18:28:10 -0600 Subject: [PATCH 4/5] - Account for pre-4.0 .NET Framework versions --- EasyPost/RuntimeInfo.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/EasyPost/RuntimeInfo.cs b/EasyPost/RuntimeInfo.cs index 5a9a2eaa9..7d6796e99 100644 --- a/EasyPost/RuntimeInfo.cs +++ b/EasyPost/RuntimeInfo.cs @@ -37,18 +37,27 @@ internal static string DotNetVersion { get { - string dotNetVersion = Environment.Version.ToString(); - if (dotNetVersion == "4.0.30319.42000") + 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 */ - dotNetVersion = "4.6+"; + versionString = "4.6+"; } - return dotNetVersion; + return versionString; } } } From 250a7506508609cc8b3c7e1b330ee2e2792a16eb Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 1 Jul 2022 10:34:47 -0600 Subject: [PATCH 5/5] - Disable CI tests since GitHub Actions is not set up for non-VCR tests --- .github/workflows/ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc9991cfd..90e54b9de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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