From f3267abe0056fa0b8e5a132415e4324642d365d3 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sun, 13 Aug 2023 22:26:12 +0200 Subject: [PATCH] feat(nextcloud): Use enum for WebDAV depth Signed-off-by: jld3103 --- packages/nextcloud/lib/src/webdav/client.dart | 69 ++++++++++++------- packages/nextcloud/test/webdav_test.dart | 4 +- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/packages/nextcloud/lib/src/webdav/client.dart b/packages/nextcloud/lib/src/webdav/client.dart index 3f67d7ca..fa05a74a 100644 --- a/packages/nextcloud/lib/src/webdav/client.dart +++ b/packages/nextcloud/lib/src/webdav/client.dart @@ -234,40 +234,38 @@ class WebDavClient { /// Retrieves the props for the resource at [path]. /// - /// Optionally populates the given [prop]s on the returned files. - /// [depth] can be '0', '1' or 'infinity'. + /// Optionally populates the given [prop]s on the returned resources. + /// [depth] can be used to limit scope of the returned resources. /// See http://www.webdav.org/specs/rfc2518.html#METHOD_PROPFIND for more information. Future propfind( final String path, { final WebDavPropWithoutValues? prop, - final String? depth, - }) async { - assert(depth == null || ['0', '1', 'infinity'].contains(depth), 'Depth has to be 0, 1 or infinity'); - return _parseResponse( - await _send( - 'PROPFIND', - _constructPath(path), - data: Stream.value( - Uint8List.fromList( - utf8.encode( - WebDavPropfind(prop: prop ?? WebDavPropWithoutValues()) - .toXmlElement(namespaces: namespaces) - .toXmlString(), + final WebDavDepth? depth, + }) async => + _parseResponse( + await _send( + 'PROPFIND', + _constructPath(path), + data: Stream.value( + Uint8List.fromList( + utf8.encode( + WebDavPropfind(prop: prop ?? WebDavPropWithoutValues()) + .toXmlElement(namespaces: namespaces) + .toXmlString(), + ), ), ), - ), - headers: { - if (depth != null) ...{ - 'Depth': depth, + headers: { + if (depth != null) ...{ + 'Depth': depth.value, + }, }, - }, - ), - ); - } + ), + ); /// Runs the filter-files report with the [filterRules] on the resource at [path]. /// - /// Optionally populates the [prop]s on the returned files. + /// Optionally populates the [prop]s on the returned resources. /// See https://github.com/owncloud/docs/issues/359 for more information. Future report( final String path, @@ -378,3 +376,26 @@ class WebDavOptions { /// DAV search and locating capabilities as advertised by the server in the 'dasl' header. Set searchCapabilities; } + +/// Depth used for [WebDavClient.propfind]. +/// +/// See http://www.webdav.org/specs/rfc2518.html#HEADER_Depth for more information. +enum WebDavDepth { + /// Returns props of the resource. + zero('0'), + + /// Returns props of the resource and its immediate children. + /// + /// Only works on collections and returns the same as [WebDavDepth.zero] for other resources. + one('1'), + + /// Returns props of the resource and all its progeny. + /// + /// Only works on collections and returns the same as [WebDavDepth.zero] for other resources. + infinity('infinity'); + + const WebDavDepth(this.value); + + // ignore: public_member_api_docs + final String value; +} diff --git a/packages/nextcloud/test/webdav_test.dart b/packages/nextcloud/test/webdav_test.dart index 37527a22..f01a11cb 100644 --- a/packages/nextcloud/test/webdav_test.dart +++ b/packages/nextcloud/test/webdav_test.dart @@ -47,7 +47,7 @@ void main() { test('List directory recursively', () async { final responses = (await client.webdav.propfind( '/', - depth: 'infinity', + depth: WebDavDepth.infinity, )) .responses; expect(responses, hasLength(48)); @@ -149,7 +149,7 @@ void main() { davresourcetype: true, ocsize: true, ), - depth: '0', + depth: WebDavDepth.zero, )) .toWebDavFiles() .single;