From 6a93b9fc753ec83907dd69a084a1aca2b3818127 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sat, 22 Apr 2023 11:22:44 +0200 Subject: [PATCH] neon: Wrap WebDAV calls in request manager --- packages/app/pubspec.lock | 8 +++ packages/neon/neon/lib/neon.dart | 1 + .../neon/lib/src/utils/request_manager.dart | 60 ++++++++++++++++--- packages/neon/neon/pubspec.yaml | 1 + 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/packages/app/pubspec.lock b/packages/app/pubspec.lock index 826a8e99..5788d063 100644 --- a/packages/app/pubspec.lock +++ b/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: diff --git a/packages/neon/neon/lib/neon.dart b/packages/neon/neon/lib/neon.dart index ca55f91e..b51a2c14 100644 --- a/packages/neon/neon/lib/neon.dart +++ b/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'; diff --git a/packages/neon/neon/lib/src/utils/request_manager.dart b/packages/neon/neon/lib/src/utils/request_manager.dart index b15b98f1..6865140c 100644 --- a/packages/neon/neon/lib/src/utils/request_manager.dart +++ b/packages/neon/neon/lib/src/utils/request_manager.dart @@ -14,8 +14,50 @@ class RequestManager { final Future Function() call, final T Function(R) unwrap, { final bool disableTimeout = false, - final int retries = 0, - }) async { + }) async => + _wrap( + clientID, + k, + subject, + call, + unwrap, + (final data) => json.encode(serializeNextcloud(data)), + (final data) => deserializeNextcloud(json.decode(data)), + disableTimeout, + 0, + ); + + Future wrapWebDav( + final String clientID, + final String k, + final BehaviorSubject> subject, + final Future Function() call, + final T Function(WebDavMultistatus) unwrap, { + final bool disableTimeout = false, + }) async => + _wrap( + 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( + final String clientID, + final String k, + final BehaviorSubject> subject, + final Future 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(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(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(json.decode((await cache!.get(key))!))), + unwrap(deserialize((await cache!.get(key))!)), null, loading: false, cached: true, diff --git a/packages/neon/neon/pubspec.yaml b/packages/neon/neon/pubspec.yaml index d8dca6e1..217f7c61 100644 --- a/packages/neon/neon/pubspec.yaml +++ b/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