Skip to content

Commit a1ad09b

Browse files
authored
Merge pull request #284 from EasyPost/backport_user_agent
Backport User-Agent to V2
2 parents ce139b0 + 250a750 commit a1ad09b

File tree

9 files changed

+151
-19
lines changed

9 files changed

+151
-19
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,3 @@ jobs:
4141
# Build the test project
4242
- name: Build Solution
4343
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
44-
# Run the tests
45-
- name: Run Tests
46-
run: vstest.console.exe ${{ steps.test_project.outputs.test_file }}\bin\Test\${{ steps.test_project.outputs.test_file }}.dll

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v2.8.3 (TBD)
4+
5+
* Backport improved User-Agent to collect OS details.
6+
37
## v2.8.2 (2022-02-25)
48

59
* 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

EasyPost.Net35/EasyPost.Net35.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
<Compile Include="..\EasyPost\Resource.cs">
165165
<Link>Resource.cs</Link>
166166
</Compile>
167+
<Compile Include="..\EasyPost\RuntimeInfo.cs">
168+
<Link>RuntimeInfo.cs</Link>
169+
</Compile>
167170
<Compile Include="..\EasyPost\Message.cs">
168171
<Link>Message.cs</Link>
169172
</Compile>

EasyPost.Net40/EasyPost.Net40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@
235235
<Compile Include="..\EasyPost\Error.cs">
236236
<Link>Error.cs</Link>
237237
</Compile>
238+
<Compile Include="..\EasyPost\RuntimeInfo.cs">
239+
<Link>RuntimeInfo.cs</Link>
240+
</Compile>
238241
</ItemGroup>
239242
<ItemGroup>
240243
<None Include="..\EasyPost\EasyPost.pfx">

EasyPost.NetCore20/EasyPost.NetCore20.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@
205205
<Compile Include="..\EasyPost\Error.cs">
206206
<Link>Error.cs</Link>
207207
</Compile>
208+
<Compile Include="..\EasyPost\RuntimeInfo.cs">
209+
<Link>RuntimeInfo.cs</Link>
210+
</Compile>
208211
<Compile Include="..\EasyPost\VersionInfo.cs">
209212
<Link>VersionInfo.cs</Link>
210213
</Compile>

EasyPost.NetCore31/EasyPost.NetCore31.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@
208208
<Compile Include="..\EasyPost\VersionInfo.cs">
209209
<Link>VersionInfo.cs</Link>
210210
</Compile>
211+
<Compile Include="..\EasyPost\RuntimeInfo.cs">
212+
<Link>RuntimeInfo.cs</Link>
213+
</Compile>
211214
</ItemGroup>
212215
<ItemGroup>
213216
<None Include="..\EasyPost\EasyPost.pfx">

EasyPost/Client.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Net;
5-
using System.Reflection;
64
using Newtonsoft.Json;
75
using RestSharp;
86

@@ -17,6 +15,9 @@ public class Client
1715

1816
private readonly string _dotNetVersion;
1917
private readonly string _libraryVersion;
18+
private readonly string _osArch;
19+
private readonly string _osName;
20+
private readonly string _osVersion;
2021

2122
private readonly RestClient _restClient;
2223
private int? _connectTimeoutMilliseconds;
@@ -34,7 +35,7 @@ public int RequestTimeoutMilliseconds
3435
set => _requestTimeoutMilliseconds = value;
3536
}
3637

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

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

49+
_libraryVersion = RuntimeInfo.ApplicationInfo.ApplicationVersion;
50+
_dotNetVersion = RuntimeInfo.ApplicationInfo.DotNetVersion;
51+
_osName = RuntimeInfo.OperationSystemInfo.Name;
52+
_osVersion = RuntimeInfo.OperationSystemInfo.Version;
53+
_osArch = RuntimeInfo.OperationSystemInfo.Architecture;
54+
4855
_restClient = new RestClient(clientConfiguration.ApiBase);
4956
_restClient.Timeout = ConnectTimeoutMilliseconds;
5057

@@ -57,19 +64,6 @@ public Client(ClientConfiguration clientConfiguration)
5764
{
5865
_libraryVersion = "Unknown";
5966
}
60-
61-
string dotNetVersion = Environment.Version.ToString();
62-
if (dotNetVersion == "4.0.30319.42000")
63-
{
64-
/*
65-
* We're on a v4.6+ version (or pre-.NET Core 3.0, which we don't support),
66-
* but we can't get the exact version.
67-
* See: https://docs.microsoft.com/en-us/dotnet/api/system.environment.version?view=net-6.0#remarks
68-
*/
69-
dotNetVersion = "4.6 or higher";
70-
}
71-
72-
_dotNetVersion = dotNetVersion;
7367
}
7468

7569
/// <summary>

EasyPost/EasyPost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Report.cs" />
9999
<Compile Include="ReportList.cs" />
100100
<Compile Include="Resource.cs" />
101+
<Compile Include="RuntimeInfo.cs"/>
101102
<Compile Include="Smartrate.cs" />
102103
<Compile Include="TimeInTransit.cs" />
103104
<Compile Include="TrackerList.cs" />

EasyPost/RuntimeInfo.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Reflection;
4+
5+
namespace EasyPost
6+
{
7+
public static class RuntimeInfo
8+
{
9+
internal struct ApplicationInfo
10+
{
11+
/// <summary>
12+
/// Get the version of the application as a string.
13+
/// </summary>
14+
/// <returns>The version of the application as a string.</returns>
15+
internal static string ApplicationVersion
16+
{
17+
get
18+
{
19+
try
20+
{
21+
Assembly assembly = typeof(ApplicationInfo).Assembly;
22+
FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
23+
return info.FileVersion ?? "Unknown";
24+
}
25+
catch (Exception)
26+
{
27+
return "Unknown";
28+
}
29+
}
30+
}
31+
32+
/// <summary>
33+
/// Get the .NET framework version as a string.
34+
/// </summary>
35+
/// <returns>The .NET framework version as a string.</returns>
36+
internal static string DotNetVersion
37+
{
38+
get
39+
{
40+
Version dotNetVersion = Environment.Version;
41+
if (dotNetVersion == null)
42+
{
43+
/*
44+
* We're on a pre-4.0 .NET Framework version, where Environment.Version is null.
45+
*/
46+
return "3.5-";
47+
}
48+
49+
string versionString = dotNetVersion.ToString();
50+
if (versionString == "4.0.30319.42000")
51+
{
52+
/*
53+
* We're on a v4.6+ version (or pre-.NET Core 3.0, which we don't support),
54+
* but we can't get the exact version.
55+
* See: https://docs.microsoft.com/en-us/dotnet/api/system.environment.version?view=net-6.0#remarks
56+
*/
57+
versionString = "4.6+";
58+
}
59+
60+
return versionString;
61+
}
62+
}
63+
}
64+
65+
internal struct OperationSystemInfo
66+
{
67+
/// <summary>
68+
/// Get details about the operating system.
69+
/// </summary>
70+
/// <returns>Details about the operating system.</returns>
71+
private static OperatingSystem OperatingSystem
72+
{
73+
get { return Environment.OSVersion; }
74+
}
75+
76+
/// <summary>
77+
/// Get the name of the operating system.
78+
/// </summary>
79+
/// <returns>Name of the operating system.</returns>
80+
internal static string Name
81+
{
82+
get
83+
{
84+
switch (OperatingSystem.Platform)
85+
{
86+
case PlatformID.Win32S:
87+
case PlatformID.Win32Windows:
88+
case PlatformID.Win32NT:
89+
case PlatformID.WinCE:
90+
return "Windows";
91+
case PlatformID.Unix:
92+
return "Linux";
93+
case PlatformID.MacOSX: // in newer versions, Mac OS X is PlatformID.Unix unfortunately
94+
return "Darwin";
95+
default:
96+
return "Unknown";
97+
}
98+
}
99+
}
100+
101+
/// <summary>
102+
/// Get the version of the operating system.
103+
/// </summary>
104+
/// <returns>Version of the operating system.</returns>
105+
internal static string Version
106+
{
107+
get { return OperatingSystem.Version.ToString(); }
108+
}
109+
110+
/// <summary>
111+
/// Get the architecture of the operating system.
112+
/// </summary>
113+
/// <returns>Architecture of the operating system.</returns>
114+
internal static string Architecture
115+
{
116+
get
117+
{
118+
// Because of how this library uses parent files, we can't easily target different frameworks and use different functions to get the architecture.
119+
return "Unknown";
120+
}
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)