network_module.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import 'package:dio/dio.dart';
  2. import 'package:electronic_assistant/utils/stream_dio_log_interceptor.dart';
  3. import 'package:pretty_dio_logger/pretty_dio_logger.dart';
  4. import '../consts/Constants.dart';
  5. class _NetworkModule {
  6. static Dio _createDefaultDio() {
  7. Dio dio = Dio(BaseOptions(
  8. /// 发送数据的超时设置。
  9. ///
  10. /// 超时时会抛出类型为 [DioExceptionType.sendTimeout] 的
  11. /// [DioException]。
  12. ///
  13. /// `null` 或 `Duration.zero` 即不设置超时。
  14. //Duration ? sendTimeout;
  15. /// 接收数据的超时设置。
  16. ///
  17. /// 这里的超时对应的时间是:
  18. /// - 在建立连接和第一次收到响应数据事件之前的超时。
  19. /// - 每个数据事件传输的间隔时间,而不是接收的总持续时间。
  20. ///
  21. /// 超时时会抛出类型为 [DioExceptionType.receiveTimeout] 的
  22. /// [DioException]。
  23. ///
  24. /// `null` 或 `Duration.zero` 即不设置超时。
  25. //Duration ? receiveTimeout;
  26. /// 可以在 [Interceptor]、[Transformer] 和
  27. /// [Response.requestOptions] 中获取到的自定义对象。
  28. //Map<String, dynamic> ? extra;
  29. /// HTTP 请求头。
  30. ///
  31. /// 请求头的键是否相等的判断大小写不敏感的。
  32. /// 例如:`content-type` 和 `Content-Type` 会视为同样的请求头键。
  33. //Map<String, dynamic> ? headers;
  34. /// 是否保留请求头的大小写。
  35. ///
  36. /// 默认值为 false。
  37. ///
  38. /// 该选项在以下场景无效:
  39. /// - XHR 不支持直接处理。
  40. /// - 按照 HTTP/2 的标准,只支持小写请求头键。
  41. //bool ? preserveHeaderCase;
  42. /// 表示 [Dio] 处理请求响应数据的类型。
  43. ///
  44. /// 默认值为 [ResponseType.json]。
  45. /// [Dio] 会在请求响应的 content-type
  46. /// 为 [Headers.jsonContentType] 时自动将响应字符串处理为 JSON 对象。
  47. ///
  48. /// 在以下情况时,分别使用:
  49. /// - `plain` 将数据处理为 `String`;
  50. /// - `bytes` 将数据处理为完整的 bytes。
  51. /// - `stream` 将数据处理为流式返回的二进制数据;
  52. //ResponseType? responseType;
  53. /// 请求的 content-type。
  54. ///
  55. /// 请求默认的 `content-type` 会由 [ImplyContentTypeInterceptor]
  56. /// 根据发送数据的类型推断。它可以通过
  57. /// [Interceptors.removeImplyContentTypeInterceptor] 移除。
  58. //String? contentType;
  59. /// 判断当前返回的状态码是否可以视为请求成功。
  60. //ValidateStatus? validateStatus;
  61. /// 是否在请求失败时仍然获取返回数据内容。
  62. ///
  63. /// 默认为 true。
  64. //bool? receiveDataWhenStatusError;
  65. /// 参考 [HttpClientRequest.followRedirects]。
  66. ///
  67. /// 默认为 true。
  68. //bool? followRedirects;
  69. /// 当 [followRedirects] 为 true 时,指定的最大重定向次数。
  70. /// 如果请求超出了重定向次数上线,会抛出 [RedirectException]。
  71. ///
  72. /// 默认为 5。
  73. //int? maxRedirects;
  74. /// 参考 [HttpClientRequest.persistentConnection]。
  75. ///
  76. /// 默认为 true。
  77. //bool? persistentConnection;
  78. /// 对请求内容进行自定义编码转换。
  79. ///
  80. /// 默认为 [Utf8Encoder]。
  81. //RequestEncoder? requestEncoder;
  82. /// 对请求响应内容进行自定义解码转换。
  83. ///
  84. /// 默认为 [Utf8Decoder]。
  85. //ResponseDecoder? responseDecoder;
  86. /// 当请求参数以 `x-www-url-encoded` 方式发送时,如何处理集合参数。
  87. ///
  88. /// 默认为 [ListFormat.multi]。
  89. //ListFormat? listFormat;
  90. ));
  91. dio.interceptors.add(PrettyDioLogger(
  92. requestHeader: true,
  93. requestBody: true,
  94. responseBody: true,
  95. responseHeader: true,
  96. enabled: Constants.env != Constants.envProd,
  97. ));
  98. return dio;
  99. }
  100. static Dio _createStreamDio() {
  101. Dio streamDio = Dio(BaseOptions(
  102. responseType: ResponseType.stream,
  103. ));
  104. streamDio.interceptors.add(StreamDioLogInterceptor());
  105. return streamDio;
  106. }
  107. static Dio _createFileDio() {
  108. Dio dio = Dio(BaseOptions(
  109. sendTimeout: const Duration(seconds: 60 * 30), //半个小时
  110. ));
  111. dio.interceptors.add(PrettyDioLogger(
  112. requestHeader: true,
  113. requestBody: true,
  114. responseBody: true,
  115. responseHeader: true,
  116. enabled: Constants.env != Constants.envProd,
  117. ));
  118. return dio;
  119. }
  120. }
  121. final defaultDio = _NetworkModule._createDefaultDio();
  122. final streamDio = _NetworkModule._createStreamDio();
  123. final fileDio = _NetworkModule._createFileDio();