47 lines
1.6 KiB
Dart
47 lines
1.6 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../../../model/endpoint_data.dart';
|
|
import '../../errors/server_exception.dart';
|
|
import '../nextcloud_ocs.dart';
|
|
import 'autocomplete_response.dart';
|
|
|
|
class AutocompleteApi {
|
|
/// Searches sharees (users by default). Pass [shareTypes] to widen the search
|
|
/// — e.g. `[0, 1]` for both users and groups (0 = user, 1 = group).
|
|
Future<AutocompleteResponse> find(
|
|
String query, {
|
|
List<int> shareTypes = const [0],
|
|
}) async {
|
|
// NextcloudOcs.uri serialises every query value via `toString()`, which
|
|
// would turn the `shareTypes[]` list into `"[0, 1]"`. Build the Uri here so
|
|
// Dart encodes the list as repeated `shareTypes[]=0&shareTypes[]=1` params.
|
|
final endpoint = EndpointData().nextcloud();
|
|
final uri = Uri.https(
|
|
endpoint.domain,
|
|
'${endpoint.path}/ocs/v2.php/core/autocomplete/get',
|
|
{
|
|
'format': 'json',
|
|
'search': query,
|
|
'itemType': ' ',
|
|
'itemId': ' ',
|
|
'shareTypes[]': shareTypes.map((t) => t.toString()).toList(),
|
|
'limit': '10',
|
|
},
|
|
);
|
|
final response = await http.get(uri, headers: NextcloudOcs.headers());
|
|
if (response.statusCode != HttpStatus.ok) {
|
|
throw ServerException(
|
|
statusCode: response.statusCode,
|
|
technicalDetails: 'core/autocomplete/get: ${response.body}',
|
|
);
|
|
}
|
|
final decoded = jsonDecode(response.body) as Map<String, dynamic>;
|
|
return AutocompleteResponse.fromJson(
|
|
decoded['ocs'] as Map<String, dynamic>,
|
|
);
|
|
}
|
|
}
|