12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import 'package:fis_jsonrpc/request.dart';
- import 'exception.dart';
- abstract class JsonRpcInterceptor {
- Future<JsonRpcRequest> onRequestTransmit(JsonRpcRequest request) async {
- return request;
- }
- Future<Map<String, dynamic>> onResponse(Map<String, dynamic> response) async {
- return response;
- }
- Future<dynamic> onResponseResult(dynamic result) async {
- return result;
- }
- Future<JsonRpcServerError> onResponseError(JsonRpcServerError error) async {
- return error;
- }
- Future<JsonRpcNetworkException> onHttpRequestError(
- JsonRpcNetworkException error) async {
- return error;
- }
- }
- class _JsonRpcInterceptHost implements JsonRpcInterceptor {
- final _interceptors = <JsonRpcInterceptor>[];
- void addInterceptor(JsonRpcInterceptor interceptor) {
- _interceptors.add(interceptor);
- }
- @override
- Future<JsonRpcRequest> onRequestTransmit(JsonRpcRequest request) async {
- var pipeVal = request;
- for (var interceptor in _interceptors) {
- pipeVal = await interceptor.onRequestTransmit(pipeVal);
- }
- return pipeVal;
- }
- @override
- Future<Map<String, dynamic>> onResponse(Map<String, dynamic> response) async {
- var pipeVal = response;
- for (var interceptor in _interceptors) {
- pipeVal = await interceptor.onResponse(pipeVal);
- }
- return pipeVal;
- }
- @override
- Future<JsonRpcServerError> onResponseError(JsonRpcServerError error) async {
- var pipeVal = error;
- for (var interceptor in _interceptors) {
- pipeVal = await interceptor.onResponseError(pipeVal);
- }
- return pipeVal;
- }
- @override
- Future<JsonRpcNetworkException> onHttpRequestError(
- JsonRpcNetworkException error) async {
- var pipeVal = error;
- for (var interceptor in _interceptors) {
- pipeVal = await interceptor.onHttpRequestError(pipeVal);
- }
- return pipeVal;
- }
- @override
- Future onResponseResult(result) async {
- var pipeVal = result;
- for (var interceptor in _interceptors) {
- pipeVal = await interceptor.onResponseResult(pipeVal);
- }
- return pipeVal;
- }
- }
- final jsonRpcInterceptHost = _JsonRpcInterceptHost();
|