|
|
@ -7,14 +7,6 @@ class RequestManager { |
|
|
|
|
|
|
|
|
|
|
|
final Cache? cache; |
|
|
|
final Cache? cache; |
|
|
|
|
|
|
|
|
|
|
|
final bool _enablePrinting = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void _print(final String input) { |
|
|
|
|
|
|
|
if (_enablePrinting) { |
|
|
|
|
|
|
|
debugPrint(input); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stream<Result<T>> wrapWithoutCache<T>( |
|
|
|
Stream<Result<T>> wrapWithoutCache<T>( |
|
|
|
final Future<T> Function() call, { |
|
|
|
final Future<T> Function() call, { |
|
|
|
final bool disableTimeout = false, |
|
|
|
final bool disableTimeout = false, |
|
|
@ -36,68 +28,6 @@ class RequestManager { |
|
|
|
required final T? previousData, |
|
|
|
required final T? previousData, |
|
|
|
final bool disableTimeout = false, |
|
|
|
final bool disableTimeout = false, |
|
|
|
}) async* { |
|
|
|
}) async* { |
|
|
|
yield* _wrap<T, R>( |
|
|
|
|
|
|
|
clientID, |
|
|
|
|
|
|
|
k, |
|
|
|
|
|
|
|
(final s) => json.encode(serialize<R>(s)), |
|
|
|
|
|
|
|
(final d) => deserialize<R>(json.decode(d)), |
|
|
|
|
|
|
|
call, |
|
|
|
|
|
|
|
unwrap: unwrap, |
|
|
|
|
|
|
|
previousData: previousData, |
|
|
|
|
|
|
|
disableTimeout: disableTimeout, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stream<Result<Uint8List>> wrapBytes( |
|
|
|
|
|
|
|
final String clientID, |
|
|
|
|
|
|
|
final String k, |
|
|
|
|
|
|
|
final Future<Uint8List> Function() call, { |
|
|
|
|
|
|
|
final bool preferCache = false, |
|
|
|
|
|
|
|
final bool disableTimeout = false, |
|
|
|
|
|
|
|
final Uint8List? previousData, |
|
|
|
|
|
|
|
}) => |
|
|
|
|
|
|
|
_wrap<Uint8List, Uint8List>( |
|
|
|
|
|
|
|
clientID, |
|
|
|
|
|
|
|
k, |
|
|
|
|
|
|
|
(final s) => base64.encode(s), |
|
|
|
|
|
|
|
(final d) => base64.decode(d), |
|
|
|
|
|
|
|
call, |
|
|
|
|
|
|
|
preferCache: preferCache, |
|
|
|
|
|
|
|
previousData: previousData, |
|
|
|
|
|
|
|
disableTimeout: disableTimeout, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stream<Result<String>> wrapString( |
|
|
|
|
|
|
|
final String clientID, |
|
|
|
|
|
|
|
final String k, |
|
|
|
|
|
|
|
final Future<String> Function() call, { |
|
|
|
|
|
|
|
final bool preferCache = false, |
|
|
|
|
|
|
|
final bool disableTimeout = false, |
|
|
|
|
|
|
|
final String? previousData, |
|
|
|
|
|
|
|
}) => |
|
|
|
|
|
|
|
_wrap<String, String>( |
|
|
|
|
|
|
|
clientID, |
|
|
|
|
|
|
|
k, |
|
|
|
|
|
|
|
(final s) => s, |
|
|
|
|
|
|
|
(final d) => d, |
|
|
|
|
|
|
|
call, |
|
|
|
|
|
|
|
preferCache: preferCache, |
|
|
|
|
|
|
|
previousData: previousData, |
|
|
|
|
|
|
|
disableTimeout: disableTimeout, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
Stream<Result<T>> _wrap<T, R>( |
|
|
|
|
|
|
|
final String clientID, |
|
|
|
|
|
|
|
final String k, |
|
|
|
|
|
|
|
final String Function(R) serialize, |
|
|
|
|
|
|
|
final R Function(String) deserialize, |
|
|
|
|
|
|
|
final Future<R> Function() call, { |
|
|
|
|
|
|
|
final bool preferCache = false, |
|
|
|
|
|
|
|
final bool disableTimeout = false, |
|
|
|
|
|
|
|
T Function(R)? unwrap, |
|
|
|
|
|
|
|
final T? previousData, |
|
|
|
|
|
|
|
}) async* { |
|
|
|
|
|
|
|
unwrap ??= (final a) => a as T; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (previousData != null) { |
|
|
|
if (previousData != null) { |
|
|
|
yield ResultCached(previousData, loading: true); |
|
|
|
yield ResultCached(previousData, loading: true); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -106,35 +36,20 @@ class RequestManager { |
|
|
|
|
|
|
|
|
|
|
|
final key = '$clientID-$k'; |
|
|
|
final key = '$clientID-$k'; |
|
|
|
|
|
|
|
|
|
|
|
_print('[Request]: $k'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cache != null && await cache!.has(key)) { |
|
|
|
if (cache != null && await cache!.has(key)) { |
|
|
|
_print('[Cache]: $k'); |
|
|
|
yield ResultCached(unwrap(deserialize<R>(json.decode((await cache!.get(key))!))), loading: true); |
|
|
|
final s = unwrap(deserialize((await cache!.get(key))!)); |
|
|
|
|
|
|
|
if (preferCache) { |
|
|
|
|
|
|
|
yield Result.success(s); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
yield ResultCached(s, loading: true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
final response = await _timeout(disableTimeout, call); |
|
|
|
final response = await _timeout(disableTimeout, call); |
|
|
|
|
|
|
|
await cache?.set(key, json.encode(serialize<R>(response))); |
|
|
|
final s = serialize(response); |
|
|
|
|
|
|
|
_print('[Response]: $k'); |
|
|
|
|
|
|
|
await cache?.set(key, s); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
yield Result.success(unwrap(response)); |
|
|
|
yield Result.success(unwrap(response)); |
|
|
|
} on Exception catch (e) { |
|
|
|
} on Exception catch (e) { |
|
|
|
if (cache != null && await cache!.has(key)) { |
|
|
|
if (cache != null && await cache!.has(key)) { |
|
|
|
_print('[Cache]: $k'); |
|
|
|
|
|
|
|
debugPrint(e.toString()); |
|
|
|
debugPrint(e.toString()); |
|
|
|
yield ResultCached(unwrap(deserialize((await cache!.get(key))!)), error: e); |
|
|
|
yield ResultCached(unwrap(deserialize<R>(json.decode((await cache!.get(key))!))), error: e); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
_print('[Failure]: $k'); |
|
|
|
|
|
|
|
debugPrint(e.toString()); |
|
|
|
debugPrint(e.toString()); |
|
|
|
yield Result.error(e); |
|
|
|
yield Result.error(e); |
|
|
|
} |
|
|
|
} |
|
|
|