handler method
Future<Response?>
handler(
- Request request
)
Implementation
Future<Response?> handler(Request request) async {
final body = await request.readAsString();
final rq = json.decode(body) as Map<String, dynamic>;
final method = rq['method'] as String?;
final params = rq['params'];
final id = rq['id'];
if (!_methods.containsKey(method)) {
return null;
}
try {
final rs = await _methods[method!]!(params);
return Response(
200,
body: json.encode({
if (!_omitRpcVersion) 'jsonrpc': '2.0',
if (id != null) 'id': id,
'result': rs,
}),
headers: {
'Content-Type': 'application/json',
},
);
} catch (e, _) {
final ex = e is RpcException ? e : ServerException(e.toString());
final error = {
'code': ex.code,
'message': ex.message,
if (ex.data != null) 'data': ex.data,
};
return Response(
400,
body: json.encode({
if (!_omitRpcVersion) 'jsonrpc': '2.0',
if (id != null) 'id': id,
'error': error,
}),
headers: {
'Content-Type': 'application/json',
},
);
}
}