|
| 1 | +package io.qdrant.client; |
| 2 | + |
| 3 | +import io.grpc.CallOptions; |
| 4 | +import io.grpc.Channel; |
| 5 | +import io.grpc.ClientCall; |
| 6 | +import io.grpc.ClientInterceptor; |
| 7 | +import io.grpc.Context; |
| 8 | +import io.grpc.ForwardingClientCall; |
| 9 | +import io.grpc.Metadata; |
| 10 | +import io.grpc.MethodDescriptor; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +/** |
| 16 | + * Utilities for attaching per-request headers to gRPC calls. |
| 17 | + * |
| 18 | + * <pre>{@code |
| 19 | + * Context ctx = RequestHeaders.withHeader(Context.current(), "x-request-id", "abc-123"); |
| 20 | + * ctx.run(() -> client.listCollectionsAsync()); |
| 21 | + * }</pre> |
| 22 | + */ |
| 23 | +public final class RequestHeaders { |
| 24 | + |
| 25 | + static final Context.Key<Map<String, String>> HEADERS_KEY = |
| 26 | + Context.key("qdrant-request-headers"); |
| 27 | + |
| 28 | + private RequestHeaders() {} |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns a new {@link Context} derived from {@code ctx} that carries {@code key}/{@code value} |
| 32 | + * as a gRPC metadata header on every request started within that context. |
| 33 | + * |
| 34 | + * @param ctx the parent context |
| 35 | + * @param key the header name |
| 36 | + * @param value the header value |
| 37 | + * @return a child context with the header attached |
| 38 | + */ |
| 39 | + public static Context withHeader(Context ctx, String key, String value) { |
| 40 | + return withHeaders(ctx, Collections.singletonMap(key, value)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns a new {@link Context} derived from {@code ctx} that carries all entries of {@code |
| 45 | + * headers} as gRPC metadata on every request started within that context. |
| 46 | + * |
| 47 | + * @param ctx the parent context |
| 48 | + * @param headers the headers to attach |
| 49 | + * @return a child context with the headers attached |
| 50 | + */ |
| 51 | + public static Context withHeaders(Context ctx, Map<String, String> headers) { |
| 52 | + if (headers == null || headers.isEmpty()) { |
| 53 | + return ctx; |
| 54 | + } |
| 55 | + Map<String, String> merged = new HashMap<>(); |
| 56 | + Map<String, String> current = HEADERS_KEY.get(ctx); |
| 57 | + if (current != null) merged.putAll(current); |
| 58 | + merged.putAll(headers); |
| 59 | + return ctx.withValue(HEADERS_KEY, merged); |
| 60 | + } |
| 61 | + |
| 62 | + /** Returns a {@link ClientInterceptor} that injects per-request headers from the context. */ |
| 63 | + static ClientInterceptor newInterceptor() { |
| 64 | + return new ClientInterceptor() { |
| 65 | + @Override |
| 66 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 67 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 68 | + return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>( |
| 69 | + next.newCall(method, callOptions)) { |
| 70 | + @Override |
| 71 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 72 | + Map<String, String> extra = HEADERS_KEY.get(); |
| 73 | + if (extra != null) { |
| 74 | + for (Map.Entry<String, String> entry : extra.entrySet()) { |
| 75 | + headers.put( |
| 76 | + Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER), |
| 77 | + entry.getValue()); |
| 78 | + } |
| 79 | + } |
| 80 | + super.start(responseListener, headers); |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | +} |
0 commit comments