call method
Future<RPCResponse>
call(
- String function,
- String path, {
- CancelToken? cancelToken,
- Map<
String, dynamic> ? headers, - Map<
String, dynamic> ? extra, - ValidateStatus? validateStatus,
- ProgressCallback? onSendProgress,
- ProgressCallback? onReceiveProgress,
- List<
Map< ? params,String, dynamic> >
override
Performs an RPC request, asking the server to execute the function with the given name and the associated parameters, which need to be encodable with the json class of dart:convert.
When the request is successful, an RPCResponse with the request id and the data from the server will be returned. If not, an RPCError will be thrown. Other errors might be thrown if an IO-Error occurs.
Implementation
@override
Future<RPCResponse> call(String function, String path,
{CancelToken? cancelToken,
Map<String, dynamic>? headers,
Map<String, dynamic>? extra,
ValidateStatus? validateStatus,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
List<Map<String, dynamic>>? params}) async {
params ??= [];
final requestPayload = {
'jsonrpc': '2.0',
'method': function,
'params': params,
'id': (_currentRequestId).toString(),
};
final _options = Options(
method: r'POST',
headers: <String, dynamic>{
...?headers,
},
extra: <String, dynamic>{'secure': <Map<String, String>>[], ...?extra},
contentType: [
'application/json',
].first,
validateStatus: validateStatus);
final response = await dio.request(path,
options: _options,
data: json.encode(requestPayload),
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress);
final data = response.data!;
final id = int.parse(data['id'] as String);
if (data.containsKey('error') as bool) {
final error = data['error'];
final code = error['code'] as int;
final message = error['message'] as String;
final errorData = error['data'];
throw RPCError(code, message, errorData);
}
final result = data['result'];
_currentRequestId++;
return RPCResponse(id, result);
}