Skip to content

Commit

Permalink
chore(user-agent): add runtime info (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip1x9 authored Oct 31, 2024
1 parent a3e3690 commit 78227aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/FactSet.SDK.Utils/Authentication/ConfidentialClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ private HttpClient InitClient(HttpClient httpClient)
{
return new HttpClient();
}

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Constants.USER_AGENT);

return httpClient;
}

Expand Down
17 changes: 16 additions & 1 deletion src/FactSet.SDK.Utils/Authentication/Constants.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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];
}

}
}
2 changes: 1 addition & 1 deletion src/FactSet.SDK.Utils/FactSet.SDK.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>FactSet Research Systems</Authors>
<Company>FactSet Research Systems</Company>
<Description>Utilities for interacting with FactSet APIs.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ConfidentialClientTests
private HttpClient _testHttpClientEmptyRes;
private HttpClient _testHttpClientValidRes;
private string _resourcesPath;
private static HttpRequestMessage _capturedRequest;

[SetUp]
public void Setup()
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -473,6 +488,7 @@ private static HttpClient CreateMockHttp(string getAsyncResponseFilePath)
"SendAsync",
ItExpr.Is<HttpRequestMessage>(r => r.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.Callback<HttpRequestMessage, CancellationToken>((r, c) => _capturedRequest = r)
.ReturnsAsync(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Expand Down

0 comments on commit 78227aa

Please sign in to comment.