Skip to content

Commit f16154b

Browse files
authored
v1.18.1 (#119)
Signed-off-by: Anush008 <mail@anush.sh>
1 parent d6061ea commit f16154b

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

src/Qdrant.Client/Grpc/QdrantChannel.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,19 @@ public override CallInvoker CreateCallInvoker()
3333
if (Disposed)
3434
throw new ObjectDisposedException(nameof(QdrantChannel));
3535

36-
var hasApiKey = _configuration.ApiKey is not null;
37-
var hasHeaders = _configuration.Headers.Count > 0;
38-
39-
if (!hasApiKey && !hasHeaders)
40-
return _channel.CreateCallInvoker();
41-
4236
return _channel.Intercept(metadata =>
4337
{
44-
if (hasApiKey)
45-
metadata.Add("api-key", _configuration.ApiKey!);
38+
if (_configuration.ApiKey is not null)
39+
metadata.Add("api-key", _configuration.ApiKey);
4640

4741
foreach (var header in _configuration.Headers)
4842
metadata.Add(header.Key, header.Value);
4943

44+
var requestHeaders = RequestHeaders.Current;
45+
if (requestHeaders is not null)
46+
foreach (var header in requestHeaders)
47+
metadata.Add(header.Key, header.Value);
48+
5049
return metadata;
5150
});
5251
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Qdrant.Client;
2+
3+
/// <summary>
4+
/// Utilities for attaching per-request headers to gRPC calls.
5+
/// </summary>
6+
public static class RequestHeaders
7+
{
8+
private static readonly AsyncLocal<IReadOnlyDictionary<string, string>?> _headers = new();
9+
10+
internal static IReadOnlyDictionary<string, string>? Current => _headers.Value;
11+
12+
/// <summary>
13+
/// Sets key and value as metadata on requests made within the returned scope.
14+
/// </summary>
15+
public static IDisposable Use(string key, string value) =>
16+
Use(new Dictionary<string, string> { [key] = value });
17+
18+
/// <summary>
19+
/// Sets all entries of headers as metadata on requests made within the returned scope.
20+
/// </summary>
21+
public static IDisposable Use(IDictionary<string, string> headers)
22+
{
23+
var previous = _headers.Value;
24+
var merged = new Dictionary<string, string>();
25+
if (previous is not null)
26+
foreach (var header in previous)
27+
merged[header.Key] = header.Value;
28+
foreach (var header in headers)
29+
merged[header.Key] = header.Value;
30+
_headers.Value = merged;
31+
return new Scope(() => _headers.Value = previous);
32+
}
33+
34+
private sealed class Scope : IDisposable
35+
{
36+
private readonly Action _restore;
37+
38+
internal Scope(Action restore) => _restore = restore;
39+
40+
public void Dispose() => _restore();
41+
}
42+
}

0 commit comments

Comments
 (0)