Package org.consensusj.jsonrpc
Class DefaultRpcClient
java.lang.Object
org.consensusj.jsonrpc.DefaultRpcClient
- All Implemented Interfaces:
AutoCloseable
,AsyncSupport
,JsonRpcClient<JavaType>
,JsonRpcTransport<JavaType>
- Direct Known Subclasses:
BitcoinClient
,EthereumClient
A strongly-typed, Jackson-based JSON-RPC client.
JsonRpcClient
provides many convenience send `default` methods in a
JSON-library-independent way. DefaultRpcClient provides the needed implementation support for Jackson. This class implements
the constructors, static fields, and getters, but delegates the core
JsonRpcTransport.sendRequestForResponseAsync(JsonRpcRequest, Type)
method to a JsonRpcTransport
implementation component.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface for ignoring what are considered "transient" errors.static interface
Functional interface for creating JsonRpcTransport instances.Nested classes/interfaces inherited from interface org.consensusj.jsonrpc.AsyncSupport
AsyncSupport.ThrowingSupplier<T>, AsyncSupport.TransientErrorFilter
-
Field Summary
Modifier and TypeFieldDescriptionprotected final JsonRpcMessage.Version
protected final ObjectMapper
-
Constructor Summary
ConstructorDescriptionDefaultRpcClient
(URI server, String rpcUser, String rpcPassword) DefaultRpcClient
(SSLContext sslContext, JsonRpcMessage.Version jsonRpcVersion, URI server, String rpcUser, String rpcPassword) DefaultRpcClient
(DefaultRpcClient.TransportFactory transportFactory, JsonRpcMessage.Version jsonRpcVersion) DefaultRpcClient
(JsonRpcMessage.Version jsonRpcVersion, URI server, String rpcUser, String rpcPassword) -
Method Summary
Modifier and TypeMethodDescriptioncollectionTypeForClasses
(Class<? extends Collection> collection, JavaType type) collectionTypeForClasses
(Class<? extends Collection> collection, Class<?> clazz) Return the JSON-RPC version used by the implementationGet the URI of the remote serverprotected <T> CompletableFuture
<JsonRpcResponse<T>> identityTransientErrorMapper
(JsonRpcRequest request, JsonRpcResponse<T> response, Throwable t) Transient error mapper that is a no-op, i.e.<T> CompletableFuture
<JsonRpcResponse<T>> pollOnce
(JsonRpcRequest request, JavaType resultType, DefaultRpcClient.TransientErrorMapper<T> errorMapper) responseTypeFor
(JavaType resultType) responseTypeFor
(Class<?> resultType) Convenience method for requesting an asynchronous response with aJsonNode
for the result.<R> CompletableFuture
<JsonRpcResponse<R>> sendRequestForResponseAsync
(JsonRpcRequest request, JavaType responseType) Send aJsonRpcRequest
for aJsonRpcResponse
asynchronously.protected String
statusFromErrorResponse
(JsonRpcResponse<?> response) protected <T> JsonRpcResponse
<T> temporarilyUnavailableResponse
(JsonRpcRequest request, Throwable t) typeForClass
(Class<?> clazz) <T> CompletableFuture
<T> waitForServer
(Duration timeout, Duration retry, Supplier<JsonRpcRequest> requestSupplier, JavaType resultType, DefaultRpcClient.TransientErrorMapper<T> errorMapper) A wait-for-server routine that is agnostic about which RPC methods the server supports.<T> CompletableFuture
<T> waitForServer
(Duration timeout, Duration retry, Supplier<JsonRpcRequest> requestSupplier, DefaultRpcClient.TransientErrorMapper<T> errorMapper) A wait-for-server routine that is agnostic about which RPC methods the server supports.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.consensusj.jsonrpc.AsyncSupport
getDefaultAsyncExecutor, supplyAsync, supplyAsync
Methods inherited from interface org.consensusj.jsonrpc.JsonRpcClient
buildJsonRequest, buildJsonRequest, close, send, send, send, send, send, send, sendAsync, sendAsync, sendAsync, sendAsync, sendAsync
Methods inherited from interface org.consensusj.jsonrpc.JsonRpcTransport
sendRequestForResponse, syncGet
-
Field Details
-
jsonRpcVersion
-
mapper
-
-
Constructor Details
-
DefaultRpcClient
-
DefaultRpcClient
public DefaultRpcClient(JsonRpcMessage.Version jsonRpcVersion, URI server, String rpcUser, String rpcPassword) -
DefaultRpcClient
public DefaultRpcClient(SSLContext sslContext, JsonRpcMessage.Version jsonRpcVersion, URI server, String rpcUser, String rpcPassword) -
DefaultRpcClient
public DefaultRpcClient(DefaultRpcClient.TransportFactory transportFactory, JsonRpcMessage.Version jsonRpcVersion)
-
-
Method Details
-
getJsonRpcVersion
Description copied from interface:JsonRpcClient
Return the JSON-RPC version used by the implementation- Specified by:
getJsonRpcVersion
in interfaceJsonRpcClient<JavaType>
- Returns:
- JSON-RPC version
-
getServerURI
Description copied from interface:JsonRpcTransport
Get the URI of the remote server- Specified by:
getServerURI
in interfaceJsonRpcTransport<JavaType>
- Returns:
- URI of remote server
-
sendRequestForResponseAsync
public <R> CompletableFuture<JsonRpcResponse<R>> sendRequestForResponseAsync(JsonRpcRequest request, JavaType responseType) Description copied from interface:JsonRpcTransport
Send aJsonRpcRequest
for aJsonRpcResponse
asynchronously.- Specified by:
sendRequestForResponseAsync
in interfaceJsonRpcTransport<JavaType>
- Type Parameters:
R
- Type of result object- Parameters:
request
- The request to sendresponseType
- The response type expected (used by Jackson for conversion)- Returns:
- A future JSON RPC Response with `result` of type `R`
-
sendRequestForResponseAsync
public CompletableFuture<JsonRpcResponse<JsonNode>> sendRequestForResponseAsync(JsonRpcRequest request) Convenience method for requesting an asynchronous response with aJsonNode
for the result.- Parameters:
request
- The request to send- Returns:
- A future JSON RPC Response with `result` of type
JsonNode
-
getMapper
-
defaultType
- Specified by:
defaultType
in interfaceJsonRpcClient<JavaType>
-
responseTypeFor
- Specified by:
responseTypeFor
in interfaceJsonRpcClient<JavaType>
-
responseTypeFor
- Specified by:
responseTypeFor
in interfaceJsonRpcClient<JavaType>
-
typeForClass
- Specified by:
typeForClass
in interfaceJsonRpcClient<JavaType>
-
collectionTypeForClasses
- Specified by:
collectionTypeForClasses
in interfaceJsonRpcClient<JavaType>
-
collectionTypeForClasses
- Specified by:
collectionTypeForClasses
in interfaceJsonRpcClient<JavaType>
-
pollOnce
public <T> CompletableFuture<JsonRpcResponse<T>> pollOnce(JsonRpcRequest request, JavaType resultType, DefaultRpcClient.TransientErrorMapper<T> errorMapper) -
waitForServer
public <T> CompletableFuture<T> waitForServer(Duration timeout, Duration retry, Supplier<JsonRpcRequest> requestSupplier, DefaultRpcClient.TransientErrorMapper<T> errorMapper) A wait-for-server routine that is agnostic about which RPC methods the server supports. In addition to twoDuration
parameters, there are 2 lambda parameters to enable this method to work with any JSON-RPC server. This version will tell the JSON-RPC Mapper to return anJsonRpcResponse
with a result of typeObject
. If you need more precise control over the type, usewaitForServer(Duration, Duration, Supplier, JavaType, TransientErrorMapper)
.- Type Parameters:
T
- The desired result type to be returned when the server is running- Parameters:
timeout
- how long to waitretry
- delay between retriesrequestSupplier
- supplier of requests (needs to increment request ID at the very least)errorMapper
- function that maps non-fatal errors (i.e. cases to keep polling)- Returns:
- A future that returns a successful
-
waitForServer
public <T> CompletableFuture<T> waitForServer(Duration timeout, Duration retry, Supplier<JsonRpcRequest> requestSupplier, JavaType resultType, DefaultRpcClient.TransientErrorMapper<T> errorMapper) A wait-for-server routine that is agnostic about which RPC methods the server supports. In addition to twoDuration
parameters, there are 3 parameters (2 functions and a generic type specifier) to enable this method to work with any JSON-RPC server.- Type Parameters:
T
- The desired result type to be returned when the server is running- Parameters:
timeout
- how long to waitretry
- delay between retriesrequestSupplier
- supplier of requests (needs to increment request ID at the very least)resultType
- the result type for the responseerrorMapper
- function that maps non-fatal errors (i.e. cases to keep polling)- Returns:
- A future that returns a successful
-
identityTransientErrorMapper
protected <T> CompletableFuture<JsonRpcResponse<T>> identityTransientErrorMapper(JsonRpcRequest request, JsonRpcResponse<T> response, Throwable t) Transient error mapper that is a no-op, i.e. it passes all errors through unchanged. -
statusFromErrorResponse
- Parameters:
response
- A response wheregetResult() == null
- Returns:
- An error status string suitable for log messages
-