pretty_http.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:dio/dio.dart';
  2. import 'package:dio_cookie_manager/dio_cookie_manager.dart';
  3. import 'package:eye_video/framework/network/cookie/pretty_cookie.dart';
  4. import 'package:eye_video/framework/network/dio_client.dart';
  5. import 'package:eye_video/framework/network/interceptor/log_interceptor.dart';
  6. class PrettyHttp {
  7. static const String GET = 'get';
  8. static const String POST = 'post';
  9. static const String DELETE = 'delete';
  10. static const String PUT = 'put';
  11. static const String HEAD = 'head';
  12. static const String PATCH = 'patch';
  13. static const String BASE_URL = "http://www.youkmi.cn:7300/";
  14. static Interceptor logInterceptor = PrettyLogInterceptor();
  15. static CookieManager cookieManager = CookieManager(CookieJarImpl());
  16. static Future<Map<String, dynamic>> http(String method, String pathUrl,
  17. {String baseUrl,
  18. Map<String, dynamic> reqParams,
  19. Map<String, dynamic> headers,
  20. Function(Map<String, dynamic>) onSuccess,
  21. Function(String) onError}) async {
  22. reqParams = reqParams ?? {};
  23. try {
  24. BaseOptions options = DioHttpClient.createOptions(baseUrl,
  25. headers: headers);
  26. var dioClient = DioHttpClient.getInstance().getClient(baseUrl,
  27. newOptions: options, interceptors: []);
  28. Response<Map<String, dynamic>> response;
  29. switch (method) {
  30. case GET:
  31. response = await dioClient.get(pathUrl, queryParameters: reqParams);
  32. break;
  33. case POST:
  34. response = await dioClient.post(pathUrl, queryParameters: reqParams);
  35. break;
  36. case PUT:
  37. response = await dioClient.put(pathUrl, queryParameters: reqParams);
  38. break;
  39. case DELETE:
  40. response =
  41. await dioClient.delete(pathUrl, queryParameters: reqParams);
  42. break;
  43. case PATCH:
  44. response = await dioClient.patch(pathUrl, queryParameters: reqParams);
  45. break;
  46. case HEAD:
  47. response = await dioClient.head(pathUrl, queryParameters: reqParams);
  48. break;
  49. default:
  50. response = await dioClient.get(pathUrl, queryParameters: reqParams);
  51. break;
  52. }
  53. if (onSuccess != null) {
  54. onSuccess(response.data);
  55. }
  56. return Future.value(response.data);
  57. } catch (e) {
  58. if (onError != null) {
  59. onError(e.toString());
  60. }
  61. return Future.error(e);
  62. }
  63. }
  64. static Future<Map<String, dynamic>> get(String pathUrl,
  65. {String baseUrl,
  66. Map<String, dynamic> reqParams,
  67. Map<String, dynamic> headers,
  68. Function(Map<String, dynamic>) onSuccess,
  69. Function(String) onError}) async {
  70. reqParams = reqParams ?? {};
  71. baseUrl = baseUrl ?? BASE_URL;
  72. return http(GET, pathUrl,
  73. baseUrl: baseUrl,
  74. reqParams: reqParams,
  75. headers: headers,
  76. onSuccess: onSuccess,
  77. onError: onError);
  78. }
  79. static Future<Map<String, dynamic>> post(String pathUrl,
  80. {String baseUrl,
  81. Map<String, dynamic> reqParams,
  82. Map<String, dynamic> headers,
  83. Function(Map<String, dynamic>) onSuccess,
  84. Function(String) onError}) async {
  85. reqParams = reqParams ?? {};
  86. baseUrl = baseUrl ?? BASE_URL;
  87. return http(POST, pathUrl,
  88. baseUrl: baseUrl,
  89. reqParams: reqParams,
  90. headers: headers,
  91. onSuccess: onSuccess,
  92. onError: onError);
  93. }
  94. static Future<Map<String, dynamic>> put(String pathUrl,
  95. {String baseUrl,
  96. Map<String, dynamic> reqParams,
  97. Map<String, dynamic> headers,
  98. Function(Map<String, dynamic>) onSuccess,
  99. Function(String) onError}) async {
  100. reqParams = reqParams ?? {};
  101. baseUrl = baseUrl ?? BASE_URL;
  102. return http(PUT, pathUrl,
  103. baseUrl: baseUrl,
  104. reqParams: reqParams,
  105. headers: headers,
  106. onSuccess: onSuccess,
  107. onError: onError);
  108. }
  109. static Future<Map<String, dynamic>> delete(String pathUrl,
  110. {String baseUrl,
  111. Map<String, dynamic> reqParams,
  112. Map<String, dynamic> headers,
  113. Function(Map<String, dynamic>) onSuccess,
  114. Function(String) onError}) async {
  115. reqParams = reqParams ?? {};
  116. baseUrl = baseUrl ?? BASE_URL;
  117. return http(DELETE, pathUrl,
  118. baseUrl: baseUrl,
  119. reqParams: reqParams,
  120. headers: headers,
  121. onSuccess: onSuccess,
  122. onError: onError);
  123. }
  124. static Future<Map<String, dynamic>> patch(String pathUrl,
  125. {String baseUrl,
  126. Map<String, dynamic> reqParams,
  127. Map<String, dynamic> headers,
  128. Function(Map<String, dynamic>) onSuccess,
  129. Function(String) onError}) async {
  130. reqParams = reqParams ?? {};
  131. baseUrl = baseUrl ?? BASE_URL;
  132. return http(PATCH, pathUrl,
  133. baseUrl: baseUrl,
  134. reqParams: reqParams,
  135. headers: headers,
  136. onSuccess: onSuccess,
  137. onError: onError);
  138. }
  139. static Future<Map<String, dynamic>> head(String pathUrl,
  140. {String baseUrl,
  141. Map<String, dynamic> reqParams,
  142. Map<String, dynamic> headers,
  143. Function(Map<String, dynamic>) onSuccess,
  144. Function(String) onError}) async {
  145. reqParams = reqParams ?? {};
  146. baseUrl = baseUrl ?? BASE_URL;
  147. return http(HEAD, pathUrl,
  148. baseUrl: baseUrl,
  149. reqParams: reqParams,
  150. headers: headers,
  151. onSuccess: onSuccess,
  152. onError: onError);
  153. }
  154. }