Skip to content

Commit bc8b54c

Browse files
committed
feat(nextcloud)!: make WebdavClient a DynamiteClient
This unifies the implementation with other clients exposed by this package. It also allows the webdav client to function on it's own without a backing NextcloudClient. For migration `WebdavClient.fromClient` behaves the same, as the previously unnamed constructor. Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
1 parent 9ebc952 commit bc8b54c

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

packages/nextcloud/lib/src/api/webdav/webdav_client.dart

+33-27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import 'dart:async';
44
import 'dart:typed_data';
55

6+
import 'package:dynamite_runtime/http_client.dart';
67
import 'package:http/http.dart' as http;
7-
import 'package:nextcloud/nextcloud.dart';
88
import 'package:nextcloud/src/api/webdav/webdav.dart';
99
import 'package:nextcloud/utils.dart';
1010
import 'package:universal_io/io.dart' show File, FileStat;
@@ -22,22 +22,28 @@ final checksumPattern = RegExp(
2222
);
2323

2424
/// WebDavClient class
25-
class WebDavClient {
26-
// ignore: public_member_api_docs
27-
WebDavClient(this.rootClient)
28-
: csrfClient = WebDavCSRFClient(
29-
rootClient.baseURL,
30-
httpClient: rootClient,
25+
class WebDavClient extends DynamiteClient {
26+
/// Creates a new `WebDavClient`.
27+
WebDavClient(
28+
super.baseURL, {
29+
http.Client? httpClient,
30+
super.authentications,
31+
}) : super(
32+
httpClient: WebDavCSRFClient(
33+
baseURL,
34+
httpClient: httpClient,
35+
),
3136
);
3237

33-
// ignore: public_member_api_docs
34-
final NextcloudClient rootClient;
35-
36-
/// {@macro WebDavCSRFClient}
37-
// TODO: Fix this bug in server.
38-
final WebDavCSRFClient csrfClient;
38+
/// Creates a new [WebDavClient] from another [client].
39+
WebDavClient.fromClient(DynamiteClient client)
40+
: this(
41+
client.baseURL,
42+
httpClient: client.httpClient,
43+
authentications: client.authentications,
44+
);
3945

40-
Uri _constructUri([PathUri? path]) => constructUri(rootClient.baseURL, path);
46+
Uri _constructUri([PathUri? path]) => constructUri(baseURL, path);
4147

4248
/// Returns a request to query the WebDAV capabilities of the server.
4349
///
@@ -59,7 +65,7 @@ class WebDavClient {
5965
Future<WebDavOptions> options() async {
6066
final request = options_Request();
6167

62-
final streamedResponse = await csrfClient.send(request);
68+
final streamedResponse = await httpClient.send(request);
6369
if (streamedResponse.statusCode != 200) {
6470
final response = await http.Response.fromStream(streamedResponse);
6571
throw DynamiteStatusCodeException(response);
@@ -105,7 +111,7 @@ class WebDavClient {
105111
set: set,
106112
);
107113

108-
final streamedResponse = await csrfClient.send(request);
114+
final streamedResponse = await httpClient.send(request);
109115
if (streamedResponse.statusCode != 201) {
110116
final response = await http.Response.fromStream(streamedResponse);
111117
throw DynamiteStatusCodeException(response);
@@ -134,7 +140,7 @@ class WebDavClient {
134140
Future<http.StreamedResponse> delete(PathUri path) async {
135141
final request = delete_Request(path);
136142

137-
final streamedResponse = await csrfClient.send(request);
143+
final streamedResponse = await httpClient.send(request);
138144
if (streamedResponse.statusCode != 204) {
139145
final response = await http.Response.fromStream(streamedResponse);
140146
throw DynamiteStatusCodeException(response);
@@ -196,7 +202,7 @@ class WebDavClient {
196202
checksum: checksum,
197203
);
198204

199-
final streamedResponse = await csrfClient.send(request);
205+
final streamedResponse = await httpClient.send(request);
200206
if (streamedResponse.statusCode != 201) {
201207
final response = await http.Response.fromStream(streamedResponse);
202208
throw DynamiteStatusCodeException(response);
@@ -285,7 +291,7 @@ class WebDavClient {
285291
onProgress: onProgress,
286292
);
287293

288-
final streamedResponse = await csrfClient.send(request);
294+
final streamedResponse = await httpClient.send(request);
289295
if (streamedResponse.statusCode != 201) {
290296
final response = await http.Response.fromStream(streamedResponse);
291297
throw DynamiteStatusCodeException(response);
@@ -355,7 +361,7 @@ class WebDavClient {
355361
onProgress: onProgress,
356362
);
357363

358-
final streamedResponse = await csrfClient.send(request);
364+
final streamedResponse = await httpClient.send(request);
359365
if (streamedResponse.statusCode != 201) {
360366
final response = await http.Response.fromStream(streamedResponse);
361367
throw DynamiteStatusCodeException(response);
@@ -399,7 +405,7 @@ class WebDavClient {
399405
void Function(double progress)? onProgress,
400406
}) async {
401407
final request = get_Request(path);
402-
final streamedResponse = await csrfClient.send(request);
408+
final streamedResponse = await httpClient.send(request);
403409
if (streamedResponse.statusCode != 200) {
404410
final response = await http.Response.fromStream(streamedResponse);
405411
throw DynamiteStatusCodeException(response);
@@ -491,7 +497,7 @@ class WebDavClient {
491497
depth: depth,
492498
);
493499

494-
final streamedResponse = await csrfClient.send(request);
500+
final streamedResponse = await httpClient.send(request);
495501
final response = await http.Response.fromStream(streamedResponse);
496502
if (streamedResponse.statusCode != 207) {
497503
throw DynamiteStatusCodeException(response);
@@ -540,7 +546,7 @@ class WebDavClient {
540546
prop: prop,
541547
);
542548

543-
final streamedResponse = await csrfClient.send(request);
549+
final streamedResponse = await httpClient.send(request);
544550
final response = await http.Response.fromStream(streamedResponse);
545551
if (streamedResponse.statusCode != 207) {
546552
throw DynamiteStatusCodeException(response);
@@ -592,7 +598,7 @@ class WebDavClient {
592598
remove: remove,
593599
);
594600

595-
final streamedResponse = await csrfClient.send(request);
601+
final streamedResponse = await httpClient.send(request);
596602
final response = await http.Response.fromStream(streamedResponse);
597603
if (streamedResponse.statusCode != 207) {
598604
throw DynamiteStatusCodeException(response);
@@ -650,7 +656,7 @@ class WebDavClient {
650656
overwrite: overwrite,
651657
);
652658

653-
final streamedResponse = await csrfClient.send(request);
659+
final streamedResponse = await httpClient.send(request);
654660
if (streamedResponse.statusCode != 201 && streamedResponse.statusCode != 204) {
655661
final response = await http.Response.fromStream(streamedResponse);
656662
throw DynamiteStatusCodeException(response);
@@ -700,7 +706,7 @@ class WebDavClient {
700706
overwrite: overwrite,
701707
);
702708

703-
final streamedResponse = await csrfClient.send(request);
709+
final streamedResponse = await httpClient.send(request);
704710
if (streamedResponse.statusCode != 201 && streamedResponse.statusCode != 204) {
705711
final response = await http.Response.fromStream(streamedResponse);
706712
throw DynamiteStatusCodeException(response);
@@ -712,7 +718,7 @@ class WebDavClient {
712718
void _addBaseHeaders(http.BaseRequest request) {
713719
request.headers['content-type'] = 'application/xml';
714720

715-
final authentication = rootClient.authentications?.firstOrNull;
721+
final authentication = authentications?.firstOrNull;
716722
if (authentication != null) {
717723
request.headers.addAll(
718724
authentication.headers,

packages/nextcloud/lib/webdav.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ extension WebDAVExtension on NextcloudClient {
1616
static final _webdav = Expando<WebDavClient>();
1717

1818
/// Client for WebDAV.
19-
WebDavClient get webdav => _webdav[this] ??= WebDavClient(this);
19+
WebDavClient get webdav => _webdav[this] ??= WebDavClient.fromClient(this);
2020
}

packages/nextcloud/test/api/webdav/webdav_test.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:math';
44
import 'dart:typed_data';
55

66
import 'package:http/http.dart';
7+
import 'package:http/testing.dart';
78
import 'package:mocktail/mocktail.dart';
89
import 'package:nextcloud/core.dart' as core;
910
import 'package:nextcloud/files_sharing.dart' as files_sharing;
@@ -18,8 +19,6 @@ class MockCallbackFunction extends Mock {
1819
void progressCallback(double progress);
1920
}
2021

21-
class _NextcloudClientMock extends Mock implements NextcloudClient {}
22-
2322
class _FileMock extends Mock implements File {}
2423

2524
class _FileStatMock extends Mock implements FileStat {}
@@ -81,11 +80,14 @@ void main() {
8180
setUpAll(() {
8281
registerFallbackValue(Request('get', Uri()) as BaseRequest);
8382

84-
final nextcloudClient = _NextcloudClientMock();
85-
when(() => nextcloudClient.baseURL).thenReturn(Uri());
86-
// ignore: discarded_futures
87-
when(() => nextcloudClient.send(any())).thenAnswer((_) async => StreamedResponse(const Stream.empty(), 400));
88-
client = WebDavClient(nextcloudClient);
83+
final mockClient = MockClient((request) async {
84+
return Response('', 400);
85+
});
86+
87+
client = WebDavClient(
88+
Uri(),
89+
httpClient: mockClient,
90+
);
8991

9092
file = _FileMock();
9193
when(() => file.openWrite()).thenReturn(IOSink(StreamController()));

0 commit comments

Comments
 (0)