Browse Source

neon: Wrap WebDAV calls in request manager

pull/262/head
jld3103 2 years ago
parent
commit
6a93b9fc75
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 8
      packages/app/pubspec.lock
  2. 1
      packages/neon/neon/lib/neon.dart
  3. 60
      packages/neon/neon/lib/src/utils/request_manager.dart
  4. 1
      packages/neon/neon/pubspec.yaml

8
packages/app/pubspec.lock

@ -1281,6 +1281,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.2.2"
xml_annotation:
dependency: transitive
description:
name: xml_annotation
sha256: "4c67438bd8cbe4c717b66f3991631ea3cdd372dc77255f9fd166a6b6e1cf6965"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
yaml:
dependency: transitive
description:

1
packages/neon/neon/lib/neon.dart

@ -41,6 +41,7 @@ import 'package:url_launcher/url_launcher_string.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:window_manager/window_manager.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg;
import 'package:xml/xml.dart' as xml;
export 'l10n/localizations.dart';
export 'src/models/account.dart';

60
packages/neon/neon/lib/src/utils/request_manager.dart

@ -14,8 +14,50 @@ class RequestManager {
final Future<R> Function() call,
final T Function(R) unwrap, {
final bool disableTimeout = false,
final int retries = 0,
}) async {
}) async =>
_wrap<T, R>(
clientID,
k,
subject,
call,
unwrap,
(final data) => json.encode(serializeNextcloud<R>(data)),
(final data) => deserializeNextcloud<R>(json.decode(data)),
disableTimeout,
0,
);
Future wrapWebDav<T>(
final String clientID,
final String k,
final BehaviorSubject<Result<T>> subject,
final Future<WebDavMultistatus> Function() call,
final T Function(WebDavMultistatus) unwrap, {
final bool disableTimeout = false,
}) async =>
_wrap<T, WebDavMultistatus>(
clientID,
k,
subject,
call,
unwrap,
(final data) => data.toXmlElement(namespaces: namespaces).toXmlString(),
(final data) => WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(data).rootElement),
disableTimeout,
0,
);
Future _wrap<T, R>(
final String clientID,
final String k,
final BehaviorSubject<Result<T>> subject,
final Future<R> Function() call,
final T Function(R) unwrap,
final String Function(R) serialize,
final R Function(String) deserialize,
final bool disableTimeout,
final int retries,
) async {
if (subject.valueOrNull?.data != null) {
subject.add(
Result(
@ -35,7 +77,7 @@ class RequestManager {
try {
subject.add(
Result(
unwrap(deserializeNextcloud<R>(json.decode((await cache!.get(key))!))),
unwrap(deserialize((await cache!.get(key))!)),
null,
loading: true,
cached: true,
@ -49,21 +91,23 @@ class RequestManager {
try {
final response = await (disableTimeout ? call() : timeout(call));
await cache?.set(key, json.encode(serializeNextcloud<R>(response)));
await cache?.set(key, serialize(response));
subject.add(Result.success(unwrap(response)));
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
if (e is NextcloudApiException && e.statusCode >= 500 && retries < 3) {
debugPrint('Retrying...');
await wrapNextcloud(
await _wrap(
clientID,
k,
subject,
call,
unwrap,
disableTimeout: disableTimeout,
retries: retries + 1,
serialize,
deserialize,
disableTimeout,
retries + 1,
);
return;
}
@ -71,7 +115,7 @@ class RequestManager {
try {
subject.add(
Result(
unwrap(deserializeNextcloud<R>(json.decode((await cache!.get(key))!))),
unwrap(deserialize((await cache!.get(key))!)),
null,
loading: false,
cached: true,

1
packages/neon/neon/pubspec.yaml

@ -46,6 +46,7 @@ dependencies:
webview_flutter: ^3.0.0
window_manager: ^0.2.5
xdg_directories: ^0.2.0+1
xml: ^6.2.2
dev_dependencies:
build_runner: ^2.1.7

Loading…
Cancel
Save