Add access token support
This commit is contained in:
parent
e7c67dd415
commit
86d899ff6b
@ -1,5 +1,7 @@
|
||||
package org.bdware.bdledger.api.grpc;
|
||||
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.stub.MetadataUtils;
|
||||
import org.bdware.bdledger.api.grpc.pb.*;
|
||||
import org.bdware.bdledger.api.grpc.pb.Common.TransactionType;
|
||||
import org.bdware.bdledger.api.grpc.pb.LedgerOuterClass.*;
|
||||
@ -30,32 +32,58 @@ public class Client {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Client.class.getName());
|
||||
|
||||
private final ManagedChannel channel;
|
||||
private final NodeGrpc.NodeFutureStub nodeFutureStub;
|
||||
private final NodeGrpc.NodeBlockingStub nodeBlockingStub;
|
||||
private final LedgerGrpc.LedgerFutureStub ledgerFutureStub;
|
||||
private final LedgerGrpc.LedgerBlockingStub ledgerBlockingStub;
|
||||
private final QueryGrpc.QueryFutureStub queryFutureStub;
|
||||
private final QueryGrpc.QueryBlockingStub queryBlockingStub;
|
||||
private ManagedChannel channel;
|
||||
private NodeGrpc.NodeFutureStub nodeFutureStub;
|
||||
private NodeGrpc.NodeBlockingStub nodeBlockingStub;
|
||||
private LedgerGrpc.LedgerFutureStub ledgerFutureStub;
|
||||
private LedgerGrpc.LedgerBlockingStub ledgerBlockingStub;
|
||||
private QueryGrpc.QueryFutureStub queryFutureStub;
|
||||
private QueryGrpc.QueryBlockingStub queryBlockingStub;
|
||||
// private final ManagedChannel channel;
|
||||
// private final NodeGrpc.NodeFutureStub nodeFutureStub;
|
||||
// private final NodeGrpc.NodeBlockingStub nodeBlockingStub;
|
||||
// private final LedgerGrpc.LedgerFutureStub ledgerFutureStub;
|
||||
// private final LedgerGrpc.LedgerBlockingStub ledgerBlockingStub;
|
||||
// private final QueryGrpc.QueryFutureStub queryFutureStub;
|
||||
// private final QueryGrpc.QueryBlockingStub queryBlockingStub;
|
||||
|
||||
/**
|
||||
* 构造客户端来访问{@code host:port}的事务账本服务。
|
||||
* 构造客户端来向{@code host:port} BDLedger节点发请求。
|
||||
*/
|
||||
public Client(String host, int port) {
|
||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用已有的{@link io.grpc.Channel}对象构造客户端来访问事务账本服务。
|
||||
* 构造客户端,使用{@code token}作access token来向{@code host:port} BDLedger节点发请求。
|
||||
*/
|
||||
public Client(ManagedChannelBuilder<?> channelBuilder) {
|
||||
public Client(String host, int port, String token) {
|
||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(), token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用已有的{@link io.grpc.Channel}对象构造客户端来向BDLedger节点发请求。
|
||||
*/
|
||||
public Client(ManagedChannelBuilder<?> channelBuilder, String token) {
|
||||
channel = channelBuilder.build();
|
||||
|
||||
nodeFutureStub = NodeGrpc.newFutureStub(channel);
|
||||
nodeBlockingStub = NodeGrpc.newBlockingStub(channel);
|
||||
ledgerFutureStub = LedgerGrpc.newFutureStub(channel);
|
||||
ledgerBlockingStub = LedgerGrpc.newBlockingStub(channel);
|
||||
queryFutureStub = QueryGrpc.newFutureStub(channel);
|
||||
queryBlockingStub = QueryGrpc.newBlockingStub(channel);
|
||||
|
||||
if (token != null) {
|
||||
Metadata fixedHeaders = new Metadata();
|
||||
fixedHeaders.put(Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER), token);
|
||||
nodeFutureStub = MetadataUtils.attachHeaders(nodeFutureStub, fixedHeaders);
|
||||
nodeBlockingStub = MetadataUtils.attachHeaders(nodeBlockingStub, fixedHeaders);
|
||||
ledgerFutureStub = MetadataUtils.attachHeaders(ledgerFutureStub, fixedHeaders);
|
||||
ledgerBlockingStub = MetadataUtils.attachHeaders(ledgerBlockingStub, fixedHeaders);
|
||||
queryFutureStub = MetadataUtils.attachHeaders(queryFutureStub, fixedHeaders);
|
||||
queryBlockingStub = MetadataUtils.attachHeaders(queryBlockingStub, fixedHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user