Skip to content

Commit

Permalink
feat(nextcloud): add webdav response converter
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Apr 2, 2024
1 parent 7ed974c commit 20ede06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/nextcloud/lib/src/webdav/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class WebDavClient {

Uri _constructUri([PathUri? path]) => constructUri(rootClient.baseURL, path);

Future<WebDavMultistatus> _parseResponse(http.StreamedResponse response) async =>
WebDavMultistatus.fromXmlElement(await response.stream.xml);

/// Request to get the WebDAV capabilities of the server.
///
/// See:
Expand All @@ -87,10 +84,7 @@ class WebDavClient {
Future<WebDavOptions> options() async {
final request = options_Request();

final response = await _send(
request,
);

final response = await _send(request);
return parseWebDavOptions(response.headers);
}

Expand Down Expand Up @@ -420,7 +414,7 @@ class WebDavClient {
);

final response = await _send(request);
return _parseResponse(response);
return const WebDavResponseConverter().convert(response);
}

/// Request to run the filter-files report with the [filterRules] on the resource at [path].
Expand Down Expand Up @@ -461,7 +455,7 @@ class WebDavClient {
);

final response = await _send(request);
return _parseResponse(response);
return const WebDavResponseConverter().convert(response);
}

/// Request to update the props of the resource at [path].
Expand Down Expand Up @@ -504,7 +498,7 @@ class WebDavClient {
);

final response = await _send(request);
final data = await _parseResponse(response);
final data = await const WebDavResponseConverter().convert(response);
for (final a in data.responses) {
for (final b in a.propstats) {
if (!b.status.contains('200')) {
Expand Down
22 changes: 22 additions & 0 deletions packages/nextcloud/lib/src/webdav/utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'dart:convert';

import 'package:dynamite_runtime/http_client.dart';
import 'package:dynamite_runtime/utils.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:nextcloud/src/webdav/models.dart';
import 'package:nextcloud/src/webdav/path_uri.dart';
import 'package:nextcloud/src/webdav/webdav.dart';

/// Base path used on the server
final webdavBase = PathUri.parse('/remote.php/webdav');
Expand All @@ -25,3 +31,19 @@ Uri constructUri(Uri baseURL, [PathUri? path]) {
}
return baseURL.replace(pathSegments: segments.where((s) => s.isNotEmpty));
}

/// Converter to transform a [http.StreamedResponse] into a [DynamiteResponse].
///
/// Throws a [DynamiteApiException] on errors.
final class WebDavResponseConverter with Converter<http.StreamedResponse, Future<WebDavMultistatus>> {
/// Creates a new response converter
const WebDavResponseConverter();

@override
Future<WebDavMultistatus> convert(http.StreamedResponse input) async {
final encoding = encodingForHeaders(input.headers);

final xml = await input.stream.bytesToXml(encoding);
return WebDavMultistatus.fromXmlElement(xml!);
}
}

0 comments on commit 20ede06

Please sign in to comment.