Skip to content
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
51 changes: 34 additions & 17 deletions src/Weaviate.Client.Tests/Unit/TestRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,25 +375,42 @@ await client.Collections.Create(
Assert.All(handler.Requests, req => req.ShouldHaveMethod(HttpMethod.Post));
}

// Helper class for typed data test
/// <summary>
/// The product data class
/// Tests that the X-Weaviate-Client header is set with the correct UserAgent value
/// </summary>
private class ProductData
[Fact]
public async Task Client_SetsUserAgentHeader()
{
/// <summary>
/// Gets or sets the value of the name
/// </summary>
public string Name { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the value of the price
/// </summary>
public double Price { get; set; }

/// <summary>
/// Gets or sets the value of the in stock
/// </summary>
public bool InStock { get; set; }
// Arrange
var (client, handler) = MockWeaviateClient.CreateWithMockHandler();

// Mock a simple response
var mockResponse = new Dto.Class { Class1 = "TestCollection", Vectorizer = "none" };

handler.AddJsonResponse(mockResponse);

var config = new CollectionCreateParams { Name = "TestCollection" };

// Act
await client.Collections.Create(config, TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(handler.LastRequest);

// Verify the X-Weaviate-Client header is present
Assert.True(
handler.LastRequest!.Headers.Contains("X-Weaviate-Client"),
"X-Weaviate-Client header should be present"
);

// Verify the header value matches the UserAgent
var headerValues = handler.LastRequest.Headers.GetValues("X-Weaviate-Client");
var actualUserAgent = headerValues.FirstOrDefault();

Assert.NotNull(actualUserAgent);
Assert.Equal(WeaviateDefaults.UserAgent, actualUserAgent);

// Verify the format includes version
Assert.Matches(@"^weaviate-client-csharp/\d+\.\d+\.\d+", actualUserAgent);
}
}
1 change: 1 addition & 0 deletions src/Weaviate.Client/Weaviate.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<AdditionalFiles Include="PublicAPI.Unshipped.txt" />
</ItemGroup>
<PropertyGroup>
<MinVerDefaultPreReleaseIdentifiers>pre</MinVerDefaultPreReleaseIdentifiers>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Weaviate.Client/WeaviateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ ILogger<WeaviateClient> logger
);
}

httpClient.DefaultRequestHeaders.Add("X-Weaviate-Client", WeaviateDefaults.UserAgent);

if (config.Headers != null)
{
foreach (var header in config.Headers)
Expand Down
7 changes: 7 additions & 0 deletions src/Weaviate.Client/WeaviateDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public static class WeaviateDefaults
/// Default retry policy applied when a client does not specify one explicitly.
/// </summary>
public static RetryPolicy DefaultRetryPolicy { get; set; } = RetryPolicy.Default;

/// <summary>
/// Gets or sets the default User-Agent headers sent by the client.
/// This can be used to identify the client or provide additional metadata in requests.
/// </summary>
public static string UserAgent =>
$"weaviate-client-csharp/{typeof(WeaviateClient).Assembly.GetName().Version}";
}
2 changes: 2 additions & 0 deletions src/Weaviate.Client/gRPC/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ internal WeaviateGrpcClient(
_defaultHeaders = new Metadata();
}

_defaultHeaders.Add("X-Weaviate-Client", WeaviateDefaults.UserAgent);

foreach (var header in headers)
{
_defaultHeaders.Add(header.Key, header.Value);
Expand Down
Loading