Skip to content

Commit

Permalink
Support custom headers. (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoEight authored Oct 16, 2024
1 parent 14b8c4f commit fa8117b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.grpc.Metadata;

import java.util.Map;

class ConnectionMetadata {
private Metadata metadata;

Expand All @@ -27,6 +29,13 @@ public ConnectionMetadata requiresLeader() {
return this;
}

public ConnectionMetadata headers(Map<String, String> headers) {
for (Map.Entry<String, String> entry : headers.entrySet())
this.metadata.put(Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER), entry.getValue());

return this;
}

public Metadata build() {
return this.metadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ static public <S extends AbstractAsyncStub<S>, O> S configureStub(S stub, EventS
metadata.requiresLeader();
}

metadata.headers(options.getHeaders());

return finalStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata.build()));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.eventstore.dbclient;

import java.util.HashMap;
import java.util.Map;

class OptionsBase<T> {
private Long deadline;
private final OperationKind kind;
private UserCredentials credentials;
private boolean requiresLeader;
private Map<String, String> headers = new HashMap<>();

protected OptionsBase() {
this(OperationKind.Regular);
Expand Down Expand Up @@ -83,6 +87,15 @@ public T deadline(long durationInMs) {
return (T)this;
}

/**
* Adds a custom HTTP header that will be added to the request.
*/
@SuppressWarnings("unchecked")
public T header(String key, String value) {
headers.put(key, value);
return (T)this;
}

Long getDeadline() {
return deadline;
}
Expand All @@ -98,4 +111,8 @@ boolean isLeaderRequired() {
UserCredentials getCredentials() {
return this.credentials;
}

Map<String, String> getHeaders() {
return this.headers;
}
}

0 comments on commit fa8117b

Please sign in to comment.