Skip to content

Commit

Permalink
BaseJsonRpcTool: Use new JsonRpcClient.sendRequestForResponseAsync() …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
msgilligan committed Sep 23, 2023
1 parent f24c3ad commit 9541f12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.consensusj.jsonrpc.AbstractRpcClient;
import org.consensusj.jsonrpc.CompositeTrustManager;
import org.consensusj.jsonrpc.JsonRpcClientJavaNet;
import org.consensusj.jsonrpc.JsonRpcException;
import org.consensusj.jsonrpc.JsonRpcMessage;
import org.consensusj.jsonrpc.JsonRpcRequest;
import org.consensusj.jsonrpc.JsonRpcResponse;
Expand All @@ -31,6 +30,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -92,12 +92,13 @@ public void run(CommonsCLICall call) {
JsonRpcRequest request = parser.parse(args);
JsonRpcResponse<JsonNode> response;
try {
response = client.sendRequestForResponse(request);
} catch (JsonRpcException e) {
log.error("send exception: ", e);
throw new ToolException(1, e.getMessage());
} catch (IOException e) {
log.error("send exception: ", e);
response = client.sendRequestForResponseAsync(request).get();
} catch (ExecutionException ee) {
log.error("send execution exception: ", ee);
Throwable t = ee.getCause() != null ? ee.getCause(): ee;
throw new ToolException(1, t.getMessage());
} catch (InterruptedException e) {
log.error("send interrupted exception: ", e);
throw new ToolException(1, e.getMessage());
}
String resultForPrinting = formatResponse(response, client.getMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ default <R> JsonRpcResponse<R> sendRequestForResponse(JsonRpcRequest request, Ja
* @throws JsonRpcStatusException JSON RPC status error
*/
default JsonRpcResponse<JsonNode> sendRequestForResponse(JsonRpcRequest request) throws IOException, JsonRpcStatusException {
return syncGet(sendRequestForResponseAsync(request, responseTypeFor(JsonNode.class)));
return syncGet(sendRequestForResponseAsync(request));
}

/**
* Convenience method for requesting an asynchronous response with a {@link JsonNode} for the result.
* @param request The request to send
* @return A future JSON RPC Response with `result` of type {@code JsonNode}
*/
default CompletableFuture<JsonRpcResponse<JsonNode>> sendRequestForResponseAsync(JsonRpcRequest request) {
return sendRequestForResponseAsync(request, responseTypeFor(JsonNode.class));
}

private <R> CompletableFuture<R> sendRequestForResultAsync(JsonRpcRequest request, JavaType resultType) {
Expand Down

0 comments on commit 9541f12

Please sign in to comment.