Browse Source

JsonRpcInterceptor.onRequestTransmit

melon.yin 3 years ago
parent
commit
73d61d986f
2 changed files with 17 additions and 1 deletions
  1. 2 1
      lib/client_base.dart
  2. 15 0
      lib/interceptor.dart

+ 2 - 1
lib/client_base.dart

@@ -55,7 +55,8 @@ class JsonRpcClientBase {
   }
 
   Future<dynamic> _transmit(JsonRpcRequest request) async {
-    String package = jsonEncode(request.toJson());
+    final req = await jsonRpcInterceptHost.onRequestTransmit(request);
+    String package = jsonEncode(req.toJson());
     var response = await _postRequest(package);
     if (response == null) throw JsonRpcException(message: "Response Empty");
 

+ 15 - 0
lib/interceptor.dart

@@ -1,6 +1,12 @@
+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;
   }
@@ -21,6 +27,15 @@ class _JsonRpcInterceptHost implements JsonRpcInterceptor {
     _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;