Skip to content

Commit

Permalink
JsonRpcClientJavaNet: minor cleanup
Browse files Browse the repository at this point in the history
* make sendRequestForResponseString async and package-private
* Remove unused methods
* Make log methods instance methods
* Improve JavaDoc
  • Loading branch information
msgilligan committed Jul 25, 2023
1 parent 5d5eae0 commit 95e1554
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.consensusj.jsonrpc;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,7 +18,7 @@

// TODO: Add ability to pass an sslSocketFactory (or equivalent) in the constructor.
/**
* Incubating JSON-RPC client using java.net.http.
* Incubating JSON-RPC client using {@link java.net.http.HttpClient}
* Synchronous API only for now (internal implementation is async), will add async API later.
*/
public class JsonRpcClientJavaNet extends AbstractRpcClient {
Expand Down Expand Up @@ -54,10 +53,15 @@ public <R> JsonRpcResponse<R> sendRequestForResponse(JsonRpcRequest request, Jav

}

public String sendRequestForResponseString(JsonRpcRequest request) throws IOException, JsonRpcStatusException {
HttpRequest httpRequest = buildJsonRpcPostRequest(request);
return sendForStringAsync(httpRequest).join();

// For testing only
CompletableFuture<String> sendRequestForResponseStringAsync(JsonRpcRequest request) {
log.debug("Send aysnc: {}", request);
try {
HttpRequest httpRequest = buildJsonRpcPostRequest(request);
return sendAsyncCommon(httpRequest);
} catch (JsonProcessingException e) {
return CompletableFuture.failedFuture(e);
}
}

/**
Expand All @@ -79,36 +83,13 @@ private <R> CompletableFuture<JsonRpcResponse<R>> sendForResponseAsync(HttpReque
.thenApply(mappingFunc(responseType));
}

private CompletableFuture<String> sendForStringAsync(HttpRequest request) {
log.debug("Send aysnc: {}", request);
return sendAsyncCommon(request);
}

private <R> CompletableFuture<R> sendAsync(HttpRequest request, Class<R> clazz) {
log.debug("Send aysnc: {}", request);
return sendAsyncCommon(request)
.thenApply(mappingFunc(clazz));
}

private <R> CompletableFuture<R> sendAsync(HttpRequest request, JavaType responseType) {
log.debug("Send aysnc: {}", request);
return sendAsyncCommon(request)
.thenApply(mappingFunc(responseType));
}

private <R> CompletableFuture<R> sendAsync(HttpRequest request, TypeReference<R> typeReference) {
log.debug("Send aysnc: {}", request);
return sendAsyncCommon(request)
.thenApply(mappingFunc(typeReference));
}

private CompletableFuture<String> sendAsyncCommon(HttpRequest request) {
log.debug("Send aysnc: {}", request);
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.whenComplete(JsonRpcClientJavaNet::log)
.whenComplete(this::log)
.thenComposeAsync(this::handleStatusError)
.thenApply(HttpResponse::body)
.whenComplete(JsonRpcClientJavaNet::log);
.whenComplete(this::log);
}

private CompletableFuture<HttpResponse<String>> handleStatusError(HttpResponse<String> response) {
Expand Down Expand Up @@ -143,27 +124,30 @@ private HttpRequest buildJsonRpcPostRequest(String requestString) throws JsonPro
.build();
}


private <R> MappingFunction<R> mappingFunc(Class<R> clazz) {
return s -> mapper.readValue(s, clazz);
}

private <R> MappingFunction<R> mappingFunc(JavaType responseType) {
return s -> mapper.readValue(s, responseType);
}

private <R> MappingFunction<R> mappingFunc(TypeReference<R> typeReference) {
return s -> mapper.readValue(s, typeReference);
}

/**
* Map a response string to Java object
* @param <R> Desired type of Java object
*/
@FunctionalInterface
interface MappingFunction<R> extends ThrowingFunction<String, R> {}
protected interface MappingFunction<R> extends ThrowingFunction<String, R> {}

/**
* Utility interface for declaring functions that throw checked exceptions and wrapping
* them in a {@link Function} that will throw {@link RuntimeException} if the underlying
* {@link #applyThrows(Object)} method throws a checked {@link Exception}.
* @param <T> input type
* @param <R> result type
*/
@FunctionalInterface
interface ThrowingFunction<T,R> extends Function<T, R> {
protected interface ThrowingFunction<T,R> extends Function<T, R> {

/**
* Gets a result wrapping checked Exceptions with {@link RuntimeException}
* Gets a result. Wraps checked Exceptions with {@link RuntimeException}
* @param t input
* @return a result
*/
@Override
Expand All @@ -176,23 +160,24 @@ default R apply(T t) {
}

/**
* Gets a result.
* Gets a result and may throw a checked exception.
*
* @param t input
* @return a result
* @throws Exception Any checked Exception
*/
R applyThrows(T t) throws Exception;
}

private static void log(HttpResponse<String> httpResponse, Throwable t) {
private void log(HttpResponse<String> httpResponse, Throwable t) {
if ((httpResponse != null)) {
log.info("log data string: {}", httpResponse);
} else {
log.error("exception: ", t);
}
}

private static void log(String s, Throwable t) {
private void log(String s, Throwable t) {
if ((s != null)) {
log.info("log data string: {}", s.substring(0 ,Math.min(100, s.length())));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JsonRpcClientJavaNetSpec extends Specification {
when:
def client = new JsonRpcClientJavaNet(testServerUri, user, pass)
JsonRpcRequest req = new JsonRpcRequest("getblockcount");
String blockcount = client.sendRequestForResponseString(req)
String blockcount = client.sendRequestForResponseStringAsync(req).join()

then:
blockcount instanceof String
Expand Down

0 comments on commit 95e1554

Please sign in to comment.