network_module.dart 4.2 KB

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