37 lines
1.1 KiB
Dart
37 lines
1.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../nextcloud_ocs.dart';
|
|
import 'search_files_response.dart';
|
|
|
|
/// Wraps the Nextcloud OCS Search Provider API for the `files` provider.
|
|
/// Endpoint: `/ocs/v2.php/search/providers/files/search`.
|
|
class SearchFiles {
|
|
Future<SearchFilesResponse> run({
|
|
required String term,
|
|
int limit = 50,
|
|
int? cursor,
|
|
}) async {
|
|
final endpoint = NextcloudOcs.uri(
|
|
'search/providers/files/search',
|
|
queryParameters: {
|
|
'term': term,
|
|
'limit': limit.toString(),
|
|
if (cursor != null) 'cursor': cursor.toString(),
|
|
},
|
|
);
|
|
final response = await http.get(endpoint, headers: NextcloudOcs.headers());
|
|
if (response.statusCode != HttpStatus.ok) {
|
|
throw Exception(
|
|
'Files search failed with ${response.statusCode}: ${response.body}',
|
|
);
|
|
}
|
|
final decoded = jsonDecode(response.body) as Map<String, dynamic>;
|
|
final ocs = decoded['ocs'] as Map<String, dynamic>;
|
|
final data = ocs['data'] as Map<String, dynamic>;
|
|
return SearchFilesResponse.fromJson(data);
|
|
}
|
|
}
|