Skip to content

Commit fa8117b

Browse files
authored
Support custom headers. (#289)
1 parent 14b8c4f commit fa8117b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

db-client-java/src/main/java/com/eventstore/dbclient/ConnectionMetadata.java

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.grpc.Metadata;
44

5+
import java.util.Map;
6+
57
class ConnectionMetadata {
68
private Metadata metadata;
79

@@ -27,6 +29,13 @@ public ConnectionMetadata requiresLeader() {
2729
return this;
2830
}
2931

32+
public ConnectionMetadata headers(Map<String, String> headers) {
33+
for (Map.Entry<String, String> entry : headers.entrySet())
34+
this.metadata.put(Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER), entry.getValue());
35+
36+
return this;
37+
}
38+
3039
public Metadata build() {
3140
return this.metadata;
3241
}

db-client-java/src/main/java/com/eventstore/dbclient/GrpcUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ static public <S extends AbstractAsyncStub<S>, O> S configureStub(S stub, EventS
150150
metadata.requiresLeader();
151151
}
152152

153+
metadata.headers(options.getHeaders());
154+
153155
return finalStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata.build()));
154156
}
155157
}

db-client-java/src/main/java/com/eventstore/dbclient/OptionsBase.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.eventstore.dbclient;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
class OptionsBase<T> {
47
private Long deadline;
58
private final OperationKind kind;
69
private UserCredentials credentials;
710
private boolean requiresLeader;
11+
private Map<String, String> headers = new HashMap<>();
812

913
protected OptionsBase() {
1014
this(OperationKind.Regular);
@@ -83,6 +87,15 @@ public T deadline(long durationInMs) {
8387
return (T)this;
8488
}
8589

90+
/**
91+
* Adds a custom HTTP header that will be added to the request.
92+
*/
93+
@SuppressWarnings("unchecked")
94+
public T header(String key, String value) {
95+
headers.put(key, value);
96+
return (T)this;
97+
}
98+
8699
Long getDeadline() {
87100
return deadline;
88101
}
@@ -98,4 +111,8 @@ boolean isLeaderRequired() {
98111
UserCredentials getCredentials() {
99112
return this.credentials;
100113
}
114+
115+
Map<String, String> getHeaders() {
116+
return this.headers;
117+
}
101118
}

0 commit comments

Comments
 (0)