All Downloads are FREE. Search and download functionalities are using the official Maven repository.

irita.sdk.client.GrpcBSNInterceptor Maven / Gradle / Ivy

There is a newer version: 0.13
Show newest version
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);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy