Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(user-agent): add runtime info #136

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})";

Filip1x9 marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading