EasyHttpClient<T> constructor
EasyHttpClient<T> (})
Implementation
EasyHttpClient(
this.initData, {
this.localCacheKey = "",
this.connectTimeout = const Duration(seconds: 10),
this.receiveTimeout = const Duration(seconds: 10),
Function? onSuccessCallback,
Function(T cache)? onCacheCallback,
Function(T newData)? onNewDataCallback,
}) {
dio = Dio();
dio.options.connectTimeout = connectTimeout;
dio.options.receiveTimeout = receiveTimeout;
dio.interceptors.clear();
dio.interceptors.addAll(EasyHttp.interceptor);
dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
if (localCacheKey.isNotEmpty) {
final cache = EasyHttp.config.cacheRunner.readCache<T>(localCacheKey);
if (cache != null) {
if (kDebugMode) {
log("Found cache ${T.toString()} request url = ${options.uri}");
}
_httpData.value = cache;
onSuccessCallback?.call();
onCacheCallback?.call(cache);
} else {
if (kDebugMode) {
log("Cache Not Found ${T.toString()} request url = ${options.uri}");
}
}
}
return handler.next(options);
}, onResponse: (response, handler) {
if (response.data != null) {
try {
_httpData.value = EasyHttp.config.cacheSerializer<T>(response.data) ?? initData;
onSuccessCallback?.call();
onNewDataCallback?.call(_httpData.value);
if (localCacheKey.isNotEmpty) {
EasyHttp.config.cacheRunner.writeCache(localCacheKey, _httpData.value);
if (kDebugMode) {
log("Updated Cache ${T.toString()} request url = ${response.realUri}");
}
}
} catch (e, s) {
if (kDebugMode) {
log("cacheSerializer error by type ${T.toString()} request url = ${response.realUri}}", error: e, stackTrace: s);
}
}
}
return handler.next(response); // continue
}, onError: (DioException e, handler) {
return handler.next(e); //continue
}));
}