Skip to content

Commit 10498f1

Browse files
authored
feat!: Add required user agent details (#36)
1 parent b52d7aa commit 10498f1

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/Octopus.OpenFeature.Provider.Tests/OctopusFeatureContextProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Octopus.OpenFeature.Provider.Tests;
88

99
public class OctopusFeatureContextProviderTests
1010
{
11-
readonly OctopusFeatureConfiguration configuration = new("identifier")
11+
readonly OctopusFeatureConfiguration configuration = new("identifier", new ProductMetadata("test-agent"))
1212
{
1313
CacheDuration = TimeSpan.FromSeconds(1)
1414
};

src/Octopus.OpenFeature.Provider/OctopusFeatureClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using System.Net.Http.Headers;
23
using System.Text.Json;
34
using Microsoft.Extensions.Logging;
45

@@ -44,6 +45,7 @@ public async Task<bool> HaveFeaturesChanged(byte[] contentHash, CancellationToke
4445
{
4546
BaseAddress = configuration.ServerUri
4647
};
48+
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(configuration.ProductMetadata.ProductHeaderValue));
4749

4850
FeatureCheck? hash = null;
4951
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {configuration.ClientIdentifier}");
@@ -86,6 +88,7 @@ class FeatureCheck(byte[] contentHash)
8688
{
8789
BaseAddress = configuration.ServerUri
8890
};
91+
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(configuration.ProductMetadata.ProductHeaderValue));
8992

9093
if (configuration.ReleaseVersionOverride is not null)
9194
{

src/Octopus.OpenFeature.Provider/OctopusFeatureConfiguration.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ public class OctopusFeatureConfiguration
77
{
88
const string DefaultServerUri = "https://features.octopus.com";
99

10-
public OctopusFeatureConfiguration(string clientIdentifier)
10+
public OctopusFeatureConfiguration(string clientIdentifier, ProductMetadata productMetadata)
1111
{
1212
ClientIdentifier = clientIdentifier;
13+
ProductMetadata = productMetadata;
1314
var serverUri = Environment.GetEnvironmentVariable("OctoToggle__Url");
1415
ServerUri = serverUri is not null ? new Uri(serverUri) : new Uri(DefaultServerUri);
1516
}
@@ -26,6 +27,11 @@ public OctopusFeatureConfiguration(string clientIdentifier)
2627
/// </summary>
2728
public string ClientIdentifier { get; set; }
2829

30+
/// <summary>
31+
/// Metadata about the product using the OpenFeature provider
32+
/// </summary>
33+
public ProductMetadata ProductMetadata { get; set; }
34+
2935
/// <summary>
3036
/// The amount of time between checks to see if new feature toggles are available
3137
/// The cache will be refreshed if new feature toggles are available
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Net.Http.Headers;
2+
3+
namespace Octopus.OpenFeature.Provider;
4+
5+
/// <summary>
6+
/// Metadata about the application using the OpenFeature provider. Used to build
7+
/// the User-Agent sent in HTTP requests.
8+
/// </summary>
9+
/// <param name="productName">The name of the product</param>
10+
/// <param name="productVersion">The version of the product</param>
11+
public class ProductMetadata
12+
{
13+
public ProductMetadata(string productName)
14+
{
15+
ProductHeaderValue = new ProductHeaderValue(productName);
16+
}
17+
18+
public ProductMetadata(string productName, string productVersion)
19+
{
20+
ProductHeaderValue = new ProductHeaderValue(productName, productVersion);
21+
}
22+
23+
public ProductHeaderValue ProductHeaderValue { get; }
24+
}

0 commit comments

Comments
 (0)