Class JsonRpcClient

java.lang.Object
org.moera.lib.jsonrpc.JsonRpcClient
Direct Known Subclasses:
MoeraNaming, PushRelay

public class JsonRpcClient extends Object
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 Details

    • JsonRpcClient

      public JsonRpcClient(String url)
      Constructs a new JsonRpcClient instance and initializes it with the specified server URL. This constructor creates an OkHttpFetcher 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

      public JsonRpcClient(Function<JsonRpcRequest,JsonRpcResponse> fetcher)
      Constructs a new instance of JsonRpcClient with the specified fetcher function, which handles sending JSON-RPC requests and receiving responses.
      Parameters:
      fetcher - a function that takes a JsonRpcRequest as input and returns a JsonRpcResponse. This function is responsible for communicating with the JSON-RPC server.
  • Method Details

    • fetch

      protected <T> T fetch(Class<T> result, String method, Object... parameters)
      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 a JsonRpcApiException. 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 is null or the remote method does not return a value, the method will return null.
      method - the name of the remote method to invoke
      parameters - 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