EasyHttpClient<T> constructor
EasyHttpClient<T>(
- T initData, {
- String localCacheKey = "",
- int timeout = 100000,
})
Implementation
EasyHttpClient(this.initData, {this.localCacheKey = "", this.timeout = 100000}) {
dio = Dio();
dio.options.connectTimeout = timeout;
dio.options.receiveTimeout = timeout;
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) {
log("Found cache ${T.toString()} request url = ${options.uri}");
_httpData.value = cache;
} else {
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;
if (localCacheKey.isNotEmpty) {
EasyHttp.config.cacheRunner.writeCache(localCacheKey, _httpData.value);
log("Updated Cache ${T.toString()} request url = ${response.realUri}");
}
} catch (e, s) {
log("cacheSerializer error by type ${T.toString()} request url = ${response.realUri}}", error: e, stackTrace: s);
}
}
return handler.next(response); // continue
}, onError: (DioError e, handler) {
return handler.next(e); //continue
}));
}