Browse Source

Merge pull request #515 from provokateurin/refactor/webdav-endpoint-username

refactor(nextcloud): Use WebDAV endpoint that does not require the username
pull/508/head
Kate 1 year ago committed by GitHub
parent
commit
f6f3597c63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      packages/nextcloud/lib/src/client.dart
  2. 13
      packages/nextcloud/lib/src/webdav/client.dart
  3. 14
      packages/nextcloud/lib/src/webdav/file.dart
  4. 10
      packages/nextcloud/test/webdav_test.dart

22
packages/nextcloud/lib/src/client.dart

@ -44,27 +44,10 @@ class NextcloudClient extends DynamiteClient {
/// Identifier used for authentication. This can be the username or email or something else.
final String? loginName;
/// Username of the user on the server, it needs to be set for using WebDAV.
/// It can be obtained via the
/// Username of the user on the server
final String? username;
WebDavClient? _webdav;
/// Client for WebDAV. Username needs to be set in order to use it
WebDavClient get webdav {
if (_webdav != null) {
return _webdav!;
}
if (username == null) {
throw Exception('The WebDAV client is only available when a username is set');
}
return _webdav = WebDavClient(
this,
'/remote.php/dav/files/$username',
);
}
CoreClient? _core;
NewsClient? _news;
NotesClient? _notes;
@ -73,6 +56,9 @@ class NextcloudClient extends DynamiteClient {
UppushClient? _uppush;
UserStatusClient? _userStatus;
/// Client for WebDAV
WebDavClient get webdav => _webdav ??= WebDavClient(this);
/// Client for the core APIs
CoreClient get core => _core ??= CoreClient.fromClient(this);

13
packages/nextcloud/lib/src/webdav/client.dart

@ -7,20 +7,17 @@ import 'package:nextcloud/src/webdav/webdav.dart';
import 'package:universal_io/io.dart';
import 'package:xml/xml.dart' as xml;
/// Base path used on the server
const String webdavBasePath = '/remote.php/webdav';
/// WebDavClient class
class WebDavClient {
// ignore: public_member_api_docs
WebDavClient(
this.rootClient,
this.basePath,
);
WebDavClient(this.rootClient);
// ignore: public_member_api_docs
final DynamiteClient rootClient;
/// Base path used on the server
final String basePath;
Future<HttpClientResponse> _send(
final String method,
final String url,
@ -62,7 +59,7 @@ class WebDavClient {
String _constructPath([final String? path]) => [
rootClient.baseURL,
basePath,
webdavBasePath,
if (path != null) ...[
path,
],

14
packages/nextcloud/lib/src/webdav/file.dart

@ -7,12 +7,7 @@ extension WebDavMultistatusFile on WebDavMultistatus {
/// Convert the [WebDavMultistatus] into a [WebDavFile] for easier handling
List<WebDavFile> toWebDavFiles(final WebDavClient client) => responses
.where((final response) => response.href != null)
.map(
(final response) => WebDavFile(
basePath: client.basePath,
response: response,
),
)
.map((final response) => WebDavFile(response: response))
.toList();
}
@ -20,12 +15,9 @@ extension WebDavMultistatusFile on WebDavMultistatus {
class WebDavFile {
/// Creates a new WebDavFile object with the given path
WebDavFile({
required final String basePath,
required final WebDavResponse response,
}) : _basePath = basePath,
_response = response;
}) : _response = response;
final String _basePath;
final WebDavResponse _response;
/// Get the props of the file
@ -34,7 +26,7 @@ class WebDavFile {
/// The path of file
late final String path =
Uri.decodeFull(_response.href!.substring(Uri.encodeFull(_basePath).length, _response.href!.length));
Uri.decodeFull(_response.href!.substring(Uri.encodeFull(webdavBasePath).length, _response.href!.length));
/// The fileid namespaced by the instance id, globally unique
late final String? id = props.ocid;

10
packages/nextcloud/test/webdav_test.dart

@ -23,17 +23,9 @@ void main() {
});
tearDown(() => container.destroy());
test('Fail without username', () async {
client = await getTestClient(
container,
username: null,
);
expect(() => client.webdav, throwsException);
});
test('Get status', () async {
final status = await client.webdav.status();
expect(status.capabilities, containsAll(['1', '3', 'access-control']));
expect(status.capabilities, containsAll(['1', '3']));
expect(status.searchCapabilities, hasLength(0));
});

Loading…
Cancel
Save