From 78227aae696954e3a79b61440b188953a4896504 Mon Sep 17 00:00:00 2001 From: Filip Andonie <47191006+Filip1x9@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:11:33 +0000 Subject: [PATCH] chore(user-agent): add runtime info (#136) --- .../Authentication/ConfidentialClient.cs | 3 +++ .../Authentication/Constants.cs | 17 ++++++++++++++++- src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj | 2 +- .../Authentication/ConfidentialClientTests.cs | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs b/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs index 83ccc89..629c84a 100644 --- a/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs +++ b/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs @@ -123,6 +123,9 @@ private HttpClient InitClient(HttpClient httpClient) { return new HttpClient(); } + + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Constants.USER_AGENT); + return httpClient; } diff --git a/src/FactSet.SDK.Utils/Authentication/Constants.cs b/src/FactSet.SDK.Utils/Authentication/Constants.cs index 28d2fc9..9df4c26 100644 --- a/src/FactSet.SDK.Utils/Authentication/Constants.cs +++ b/src/FactSet.SDK.Utils/Authentication/Constants.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.Immutable; +using System.Runtime.InteropServices; namespace FactSet.SDK.Utils.Authentication { @@ -21,10 +23,23 @@ public static class Constants "kty", "alg", "use", "kid", "n", "e", "d", "p", "q", "dp", "dq", "qi" }.ToImmutableHashSet(); + public static readonly String USER_AGENT = + $"fds-sdk/dotnet/utils/1.0.1 ({GetOSType()}; {RuntimeInformation.FrameworkDescription})"; + // Default values #pragma warning disable S1075 // URIs should not be hardcoded public static readonly string FACTSET_WELL_KNOWN_URI = "https://auth.factset.com/.well-known/openid-configuration"; #pragma warning restore S1075 // URIs should not be hardcoded + private static String GetOSType() + { + if (RuntimeInformation.OSDescription.StartsWith("Microsoft Windows")) + { + return "Windows"; + } + + return RuntimeInformation.OSDescription.Split(' ')[0]; + } + } } diff --git a/src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj b/src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj index c75f700..db5e086 100644 --- a/src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj +++ b/src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 1.0.0 + 1.0.1 FactSet Research Systems FactSet Research Systems Utilities for interacting with FactSet APIs. diff --git a/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs b/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs index 5bf7e44..f14b19f 100644 --- a/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs +++ b/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs @@ -17,6 +17,7 @@ class ConfidentialClientTests private HttpClient _testHttpClientEmptyRes; private HttpClient _testHttpClientValidRes; private string _resourcesPath; + private static HttpRequestMessage _capturedRequest; [SetUp] public void Setup() @@ -232,6 +233,20 @@ await ConfidentialClient.CreateAsync( } } + [Test] + public async Task HttpClient_UserAgent_ContainsCorrectAgent() + { + HttpClient mockClient = + CreateMockHttp(Path.Join(_resourcesPath, "exampleResponseWellKnownUri.txt")); + + ConfidentialClient confidentialClient = await ConfidentialClient.CreateAsync( + Path.Join(_resourcesPath, "validConfigGeneratedSample.txt"), + mockClient + ); + + Assert.That(_capturedRequest.Headers.UserAgent.ToString(), Is.EqualTo(Constants.USER_AGENT)); + } + [Test] public async Task CreateAsync_PassingInvalidWellKnownUri_ThrowsWellKnownUriException() { @@ -473,6 +488,7 @@ private static HttpClient CreateMockHttp(string getAsyncResponseFilePath) "SendAsync", ItExpr.Is(r => r.Method == HttpMethod.Get), ItExpr.IsAny()) + .Callback((r, c) => _capturedRequest = r) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK,