call method
Future
call(
- dynamic methodName, [
- dynamic params
])
Implementation
Future<dynamic> call(var methodName, [var params]) async {
// init values
if (_dioClient == null) {
_headers = {
'Content-Type': 'application/json',
'authorization':
'Basic ${base64.encode(utf8.encode('$username:$password'))}'
};
_url = getConnectionString();
_dioClient = Dio();
_dioClient!.interceptors.add(
RetryInterceptor(
dio: _dioClient!,
logPrint: null,
retries: 5,
),
);
}
var body = {
'jsonrpc': '2.0',
'method': methodName,
'params': params ?? [],
'id': '1'
};
try {
var response = await _dioClient!.post(
_url,
data: body,
options: Options(
headers: _headers,
),
);
if (response.statusCode == HttpStatus.ok) {
var body = response.data;
if (body['error'] != null) {
var error = body['error']['message'];
throw RPCException(
errorCode: error['code'],
errorMsg: error['message'],
method: methodName,
params: params,
);
}
return body['result'];
}
} on DioException catch (e) {
if (e.type == DioExceptionType.badResponse) {
var errorResponseBody = e.response!.data;
switch (e.response!.statusCode) {
case 401:
throw HTTPException(
code: 401,
message: 'Unauthorized',
);
case 404:
if (errorResponseBody['error'] != null) {
var error = errorResponseBody['error'];
throw RPCException(
errorCode: error['code'],
errorMsg: error['message'],
method: methodName,
params: params ?? [],
);
}
throw HTTPException(
code: 500,
message: 'Internal Server Error',
);
default:
if (errorResponseBody['error'] != null) {
var error = errorResponseBody['error'];
throw RPCException(
errorCode: error['code'],
errorMsg: error['message'],
method: methodName,
params: params,
);
}
throw HTTPException(
code: 500,
message: 'Internal Server Error',
);
}
} else if (e.type == DioExceptionType.connectionError) {
throw HTTPException(
code: 500,
message: e.message ?? 'Connection Error',
);
}
throw HTTPException(
code: 500,
message: e.message ?? 'Unknown Error',
);
}
}