interceptor.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:fis_jsonrpc/request.dart';
  2. import 'exception.dart';
  3. abstract class JsonRpcInterceptor {
  4. Future<JsonRpcRequest> onRequestTransmit(JsonRpcRequest request) async {
  5. return request;
  6. }
  7. Future<Map<String, dynamic>> onResponse(Map<String, dynamic> response) async {
  8. return response;
  9. }
  10. Future<dynamic> onResponseResult(dynamic result) async {
  11. return result;
  12. }
  13. Future<JsonRpcServerError> onResponseError(JsonRpcServerError error) async {
  14. return error;
  15. }
  16. Future<JsonRpcNetworkException> onHttpRequestError(
  17. JsonRpcNetworkException error) async {
  18. return error;
  19. }
  20. }
  21. class _JsonRpcInterceptHost implements JsonRpcInterceptor {
  22. final _interceptors = <JsonRpcInterceptor>[];
  23. void addInterceptor(JsonRpcInterceptor interceptor) {
  24. _interceptors.add(interceptor);
  25. }
  26. @override
  27. Future<JsonRpcRequest> onRequestTransmit(JsonRpcRequest request) async {
  28. var pipeVal = request;
  29. for (var interceptor in _interceptors) {
  30. pipeVal = await interceptor.onRequestTransmit(pipeVal);
  31. }
  32. return pipeVal;
  33. }
  34. @override
  35. Future<Map<String, dynamic>> onResponse(Map<String, dynamic> response) async {
  36. var pipeVal = response;
  37. for (var interceptor in _interceptors) {
  38. pipeVal = await interceptor.onResponse(pipeVal);
  39. }
  40. return pipeVal;
  41. }
  42. @override
  43. Future<JsonRpcServerError> onResponseError(JsonRpcServerError error) async {
  44. var pipeVal = error;
  45. for (var interceptor in _interceptors) {
  46. pipeVal = await interceptor.onResponseError(pipeVal);
  47. }
  48. return pipeVal;
  49. }
  50. @override
  51. Future<JsonRpcNetworkException> onHttpRequestError(
  52. JsonRpcNetworkException error) async {
  53. var pipeVal = error;
  54. for (var interceptor in _interceptors) {
  55. pipeVal = await interceptor.onHttpRequestError(pipeVal);
  56. }
  57. return pipeVal;
  58. }
  59. @override
  60. Future onResponseResult(result) async {
  61. var pipeVal = result;
  62. for (var interceptor in _interceptors) {
  63. pipeVal = await interceptor.onResponseResult(pipeVal);
  64. }
  65. return pipeVal;
  66. }
  67. }
  68. final jsonRpcInterceptHost = _JsonRpcInterceptHost();