irita.sdk.client.GrpcBSNInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of irita-sdk Show documentation
Show all versions of irita-sdk Show documentation
Irita open alliance chain SDK (java)
package irita.sdk.client;
import io.grpc.*;
import irita.sdk.config.OpbConfig;
import java.util.Optional;
public class GrpcBSNInterceptor implements ClientInterceptor {
private final OpbConfig opbConfig;
private final Metadata.Key PROJECT_ID_HEADER = Metadata.Key.of("projectIdHeader", Metadata.ASCII_STRING_MARSHALLER);
private final Metadata.Key PROJECT_KEY_HEADER = Metadata.Key.of("projectKeyHeader", Metadata.ASCII_STRING_MARSHALLER);
private final Metadata.Key CHAIN_ACCOUNT_ADDRESS_HEADER = Metadata.Key.of("chainAccountAddressHeader", Metadata.ASCII_STRING_MARSHALLER);
public GrpcBSNInterceptor(OpbConfig opbConfig) {
this.opbConfig = opbConfig;
}
@Override
public ClientCall interceptCall(MethodDescriptor methodDescriptor, CallOptions callOptions, Channel channel) {
return new ForwardingClientCall.SimpleForwardingClientCall(channel.newCall(methodDescriptor, callOptions)) {
@Override
public void start(Listener responseListener, Metadata headers) {
if (opbConfig != null) {
headers.put(PROJECT_ID_HEADER, Optional.ofNullable(opbConfig.getProjectID()).orElse(""));
headers.put(PROJECT_KEY_HEADER, Optional.ofNullable(opbConfig.getProjectKey()).orElse(""));
headers.put(CHAIN_ACCOUNT_ADDRESS_HEADER, Optional.ofNullable(opbConfig.getChainAccountAddr()).orElse(""));
}
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener(responseListener) {
@Override
public void onHeaders(Metadata headers) {
super.onHeaders(headers);
}
}, headers);
}
};
}
}