From 37439e372adda18b0a9a929a7bdfbe314dc5b97a Mon Sep 17 00:00:00 2001 From: Filip Andonie Date: Tue, 21 May 2024 11:12:06 +0100 Subject: [PATCH 1/2] chore(user-agent): add runtime info --- .../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..01d9984 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..59719fb 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..b5f129c 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, From 13162a65d10a2cf4760114fbfd3efc1c1180c4d4 Mon Sep 17 00:00:00 2001 From: Filip Andonie Date: Tue, 21 May 2024 11:40:57 +0100 Subject: [PATCH 2/2] chore: format --- src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs | 4 ++-- src/FactSet.SDK.Utils/Authentication/Constants.cs | 2 +- .../Authentication/ConfidentialClientTests.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs b/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs index 01d9984..629c84a 100644 --- a/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs +++ b/src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs @@ -123,9 +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 59719fb..9df4c26 100644 --- a/src/FactSet.SDK.Utils/Authentication/Constants.cs +++ b/src/FactSet.SDK.Utils/Authentication/Constants.cs @@ -30,7 +30,7 @@ public static class Constants #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")) diff --git a/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs b/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs index b5f129c..f14b19f 100644 --- a/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs +++ b/tests/FactSet.SDK.Utils.Tests/Authentication/ConfidentialClientTests.cs @@ -243,7 +243,7 @@ public async Task HttpClient_UserAgent_ContainsCorrectAgent() Path.Join(_resourcesPath, "validConfigGeneratedSample.txt"), mockClient ); - + Assert.That(_capturedRequest.Headers.UserAgent.ToString(), Is.EqualTo(Constants.USER_AGENT)); }