Package org.moera.lib.jsonrpc
Class JsonRpcClient
java.lang.Object
org.moera.lib.jsonrpc.JsonRpcClient
- Direct Known Subclasses:
MoeraNaming
,PushRelay
Represents a JSON-RPC client capable of sending requests to a server and retrieving responses.
It supports handling of JSON-RPC 2.0 requests and responses.
This class is thread-safe.
-
Constructor Summary
ConstructorsConstructorDescriptionJsonRpcClient
(String url) Constructs a newJsonRpcClient
instance and initializes it with the specified server URL.JsonRpcClient
(Function<JsonRpcRequest, JsonRpcResponse> fetcher) Constructs a new instance ofJsonRpcClient
with the specified fetcher function, which handles sending JSON-RPC requests and receiving responses. -
Method Summary
-
Constructor Details
-
JsonRpcClient
Constructs a newJsonRpcClient
instance and initializes it with the specified server URL. This constructor creates anOkHttpFetcher
for handling the communication with the JSON-RPC server.- Parameters:
url
- the URL of the JSON-RPC server to connect to. It must be a valid HTTP or HTTPS URL.
-
JsonRpcClient
Constructs a new instance ofJsonRpcClient
with the specified fetcher function, which handles sending JSON-RPC requests and receiving responses.- Parameters:
fetcher
- a function that takes aJsonRpcRequest
as input and returns aJsonRpcResponse
. This function is responsible for communicating with the JSON-RPC server.
-
-
Method Details
-
fetch
Performs a JSON-RPC call to invoke a method on a remote server. Constructs a JSON-RPC request, sends it using the pre-configured fetcher, and processes the response. If the response contains an error, it throws aJsonRpcApiException
. If the response is successful, the result is converted to the specified type and returned.- Type Parameters:
T
- the type of the result object expected from the remote method- Parameters:
result
- the class type of the result object. If the expected result isnull
or the remote method does not return a value, the method will returnnull
.method
- the name of the remote method to invokeparameters
- the parameters to pass to the remote method- Returns:
- the result of the remote method call, converted to the specified type, or
null
if the remote method does not return a value - Throws:
JsonRpcApiException
- if the method call returned an error
-