|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert' as convert; |
| 3 | +import 'package:dio/dio.dart' as diox; |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:streamskit_mobile/core/app/constant/constants.dart'; |
| 6 | +import 'package:streamskit_mobile/core/types/http_status_code.dart'; |
| 7 | +import 'package:streamskit_mobile/core/types/service_method.dart'; |
| 8 | +import 'package:streamskit_mobile/core/util/logger.dart'; |
| 9 | +import 'package:streamskit_mobile/core/util/stop_watch_api.dart'; |
| 10 | + |
| 11 | +class BaseRemoteData { |
| 12 | + var dio = diox.Dio(diox.BaseOptions( |
| 13 | + baseUrl: serviceBaseEndpoint, |
| 14 | + connectTimeout: connectTimeOut, |
| 15 | + receiveTimeout: receiveTimeOut, |
| 16 | + sendTimeout: receiveTimeOut, |
| 17 | + )); // with default Options |
| 18 | + |
| 19 | + Future<diox.Response<dynamic>> downloadFile( |
| 20 | + String url, String path, Function onReceive) async { |
| 21 | + var response = await dio.download( |
| 22 | + url, |
| 23 | + path, |
| 24 | + options: getOptions(), |
| 25 | + onReceiveProgress: (received, total) { |
| 26 | + onReceive(received, total); |
| 27 | + }, |
| 28 | + ); |
| 29 | + return response; |
| 30 | + } |
| 31 | + |
| 32 | + Future<diox.Response<dynamic>> postFormData( |
| 33 | + String gateway, |
| 34 | + diox.FormData formData, |
| 35 | + ) async { |
| 36 | + try { |
| 37 | + var response = await dio.post( |
| 38 | + gateway, |
| 39 | + data: formData, |
| 40 | + options: getOptions(), |
| 41 | + onSendProgress: (send, total) {}, |
| 42 | + onReceiveProgress: (received, total) {}, |
| 43 | + ); |
| 44 | + |
| 45 | + return response; |
| 46 | + } on diox.DioError catch (exception) { |
| 47 | + return catchDioError(exception: exception, gateway: gateway); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Future<diox.Response<dynamic>> putFormData( |
| 52 | + String gateway, |
| 53 | + diox.FormData formData, |
| 54 | + ) async { |
| 55 | + try { |
| 56 | + var response = await dio.put( |
| 57 | + gateway, |
| 58 | + data: formData, |
| 59 | + options: getOptions(), |
| 60 | + onSendProgress: (send, total) {}, |
| 61 | + onReceiveProgress: (received, total) {}, |
| 62 | + ); |
| 63 | + return response; |
| 64 | + } on diox.DioError catch (exception) { |
| 65 | + return catchDioError(exception: exception, gateway: gateway); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Future<diox.Response<dynamic>> postRoute( |
| 70 | + String gateway, Map<String, dynamic> body, |
| 71 | + {String? query}) async { |
| 72 | + try { |
| 73 | + Map<String, String> paramsObject = {}; |
| 74 | + if (query != null) { |
| 75 | + query.split('&').forEach((element) { |
| 76 | + paramsObject[element.split('=')[0].toString()] = |
| 77 | + element.split('=')[1].toString(); |
| 78 | + }); |
| 79 | + } |
| 80 | + var response = kDebugMode |
| 81 | + ? await StopWatch.stopWatchApi( |
| 82 | + () => dio.post( |
| 83 | + gateway, |
| 84 | + data: convert.jsonEncode(body), |
| 85 | + options: getOptions(), |
| 86 | + queryParameters: query == null ? null : paramsObject, |
| 87 | + ), |
| 88 | + ServiceMethod.post.methodName, |
| 89 | + gateway) |
| 90 | + : await dio.post( |
| 91 | + gateway, |
| 92 | + data: convert.jsonEncode(body), |
| 93 | + options: getOptions(), |
| 94 | + queryParameters: query == null ? null : paramsObject, |
| 95 | + ); |
| 96 | + return response; |
| 97 | + } on diox.DioError catch (exception) { |
| 98 | + return catchDioError(exception: exception, gateway: gateway); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + Future<diox.Response<dynamic>> putRoute( |
| 103 | + String gateway, |
| 104 | + Map<String, dynamic> body, |
| 105 | + ) async { |
| 106 | + try { |
| 107 | + var response = kDebugMode |
| 108 | + ? await StopWatch.stopWatchApi( |
| 109 | + () => dio.put( |
| 110 | + gateway, |
| 111 | + data: convert.jsonEncode(body), |
| 112 | + options: getOptions(), |
| 113 | + ), |
| 114 | + ServiceMethod.put.methodName, |
| 115 | + gateway) |
| 116 | + : await dio.put( |
| 117 | + gateway, |
| 118 | + data: convert.jsonEncode(body), |
| 119 | + options: getOptions(), |
| 120 | + ); |
| 121 | + return response; |
| 122 | + } on diox.DioError catch (exception) { |
| 123 | + return catchDioError(exception: exception, gateway: gateway); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Future<diox.Response<dynamic>> patchRoute( |
| 128 | + String gateway, { |
| 129 | + String? query, |
| 130 | + Map<String, dynamic>? body, |
| 131 | + }) async { |
| 132 | + try { |
| 133 | + Map<String, String> paramsObject = {}; |
| 134 | + if (query != null) { |
| 135 | + query.split('&').forEach((element) { |
| 136 | + paramsObject[element.split('=')[0].toString()] = |
| 137 | + element.split('=')[1].toString(); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + var response = kDebugMode |
| 142 | + ? await StopWatch.stopWatchApi( |
| 143 | + () => dio.patch( |
| 144 | + gateway, |
| 145 | + data: body == null ? null : convert.jsonEncode(body), |
| 146 | + options: getOptions(), |
| 147 | + queryParameters: query == null ? null : paramsObject, |
| 148 | + ), |
| 149 | + ServiceMethod.patch.methodName, |
| 150 | + gateway) |
| 151 | + : await dio.patch( |
| 152 | + gateway, |
| 153 | + data: body == null ? null : convert.jsonEncode(body), |
| 154 | + options: getOptions(), |
| 155 | + queryParameters: query == null ? null : paramsObject, |
| 156 | + ); |
| 157 | + return response; |
| 158 | + } on diox.DioError catch (exception) { |
| 159 | + return catchDioError(exception: exception, gateway: gateway); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + Future<diox.Response<dynamic>> getRoute( |
| 164 | + String gateway, { |
| 165 | + String? params, |
| 166 | + String? query, |
| 167 | + }) async { |
| 168 | + try { |
| 169 | + Map<String, String> paramsObject = {}; |
| 170 | + if (query != null) { |
| 171 | + query.split('&').forEach((element) { |
| 172 | + paramsObject[element.split('=')[0].toString()] = |
| 173 | + element.split('=')[1].toString(); |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + var response = kDebugMode |
| 178 | + ? await StopWatch.stopWatchApi( |
| 179 | + () => dio.get( |
| 180 | + gateway, |
| 181 | + options: getOptions(), |
| 182 | + queryParameters: query == null ? null : paramsObject, |
| 183 | + ), |
| 184 | + ServiceMethod.get.methodName, |
| 185 | + gateway) |
| 186 | + : await dio.get( |
| 187 | + gateway, |
| 188 | + options: getOptions(), |
| 189 | + queryParameters: query == null ? null : paramsObject, |
| 190 | + ); |
| 191 | + return response; |
| 192 | + } on diox.DioError catch (exception) { |
| 193 | + return catchDioError(exception: exception, gateway: gateway); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + Future<diox.Response<dynamic>> deleteRoute( |
| 198 | + String gateway, { |
| 199 | + String? params, |
| 200 | + String? query, |
| 201 | + Map<String, dynamic>? body, |
| 202 | + diox.FormData? formData, |
| 203 | + }) async { |
| 204 | + try { |
| 205 | + Map<String, String> paramsObject = {}; |
| 206 | + if (query != null) { |
| 207 | + query.split('&').forEach((element) { |
| 208 | + paramsObject[element.split('=')[0].toString()] = |
| 209 | + element.split('=')[1].toString(); |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + var response = kDebugMode |
| 214 | + ? await StopWatch.stopWatchApi( |
| 215 | + () => dio.delete( |
| 216 | + gateway, |
| 217 | + data: formData ?? |
| 218 | + (body == null ? null : convert.jsonEncode(body)), |
| 219 | + options: getOptions(), |
| 220 | + queryParameters: query == null ? null : paramsObject, |
| 221 | + ), |
| 222 | + ServiceMethod.delete.methodName, |
| 223 | + gateway) |
| 224 | + : await dio.delete( |
| 225 | + gateway, |
| 226 | + data: |
| 227 | + formData ?? (body == null ? null : convert.jsonEncode(body)), |
| 228 | + options: getOptions(), |
| 229 | + queryParameters: query == null ? null : paramsObject, |
| 230 | + ); |
| 231 | + return response; |
| 232 | + } on diox.DioError catch (exception) { |
| 233 | + return catchDioError(exception: exception, gateway: gateway); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + diox.Response catchDioError({ |
| 238 | + required diox.DioError exception, |
| 239 | + required String gateway, |
| 240 | + }) { |
| 241 | + return diox.Response( |
| 242 | + requestOptions: diox.RequestOptions(path: gateway), |
| 243 | + data: null, |
| 244 | + statusCode: StatusCode.badGateway, |
| 245 | + statusMessage: "CATCH EXCEPTION DIO", |
| 246 | + ); |
| 247 | + } |
| 248 | + |
| 249 | + diox.Options getOptions() { |
| 250 | + return diox.Options( |
| 251 | + validateStatus: (status) { |
| 252 | + // if (status == StatusCode.unauthorized && |
| 253 | + // UserLocal().getAccessToken().isNotEmpty) { |
| 254 | + // UserLocal().clearAccessToken(); |
| 255 | + // showDialogLoading(); |
| 256 | + // AppBloc.authBloc.add(LogOutEvent()); |
| 257 | + // } |
| 258 | + return true; |
| 259 | + }, |
| 260 | + headers: getHeaders(), |
| 261 | + ); |
| 262 | + } |
| 263 | + |
| 264 | + getHeaders() { |
| 265 | + return { |
| 266 | + 'Authorization': 'Bearer ', |
| 267 | + 'Content-Type': 'application/json; charset=UTF-8', |
| 268 | + 'Connection': 'keep-alive', |
| 269 | + 'Accept': '*/*', |
| 270 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 271 | + }; |
| 272 | + } |
| 273 | + |
| 274 | + printEndpoint(String method, String endpoint) { |
| 275 | + UtilLogger.log('${method.toUpperCase()}: $endpoint'); |
| 276 | + } |
| 277 | + |
| 278 | + printResponse(diox.Response<dynamic> response) { |
| 279 | + UtilLogger.log('StatusCode: ${response.statusCode}'); |
| 280 | + UtilLogger.log('Body: ${response.data.toString()}'); |
| 281 | + } |
| 282 | +} |
0 commit comments