sendRequest method

Future sendRequest(
  1. String method, [
  2. dynamic parameters
])

Implementation

Future<dynamic> sendRequest(String method, [dynamic parameters]) async {
  if (parameters is Iterable) parameters = parameters.toList();
  if (parameters is! Map && parameters is! List && parameters != null) {
    throw ArgumentError('Only maps and lists can be used as JSON-RPC '
        'parameters, was "$parameters".');
  }

  var message = <String, dynamic>{
    "jsonrpc": rpcVersion,
    "method": method,
    "id": _id
  };
  _id++;

  if (parameters != null) {
    message["params"] = parameters;
  }

  final dioOptions = Options(
    sendTimeout: options?.sendTimeout,
    receiveTimeout: options?.receiveTimeout,
    headers: options?.headers
  );

  http.options.connectTimeout = options?.connectTimeout ?? const Duration(seconds: 60);

  var data = (await http.post(
    url,
    data: message,
    options: dioOptions,
  )).data;

  if (data is String) {
    if (data.isEmpty) return data;
    data = jsonDecode(data);
  }

  if (data.containsKey("error") && data["error"] != null) {
    final error = data["error"];

    throw RPCError(
      RPCErrorRequest(method, parameters),
      error["code"],
      error["message"],
      error["data"],
    );
  } else {
    return data["result"];
  }
}