listCollectionKeys method

Future<CollectionKeysResult?> listCollectionKeys({
  1. required PeerId ownerPeerId,
  2. required String path,
  3. int? limit,
  4. int? offset,
  5. PeerId? fromServer,
})

List keys in a collection

Implementation

Future<CollectionKeysResult?> listCollectionKeys({
  required PeerId ownerPeerId,
  required String path,
  int? limit,
  int? offset,
  PeerId? fromServer,
}) async {
  final serverId = fromServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for collection keys listing');
    return null;
  }

  try {
    final context = Context();
    final stream = await host.newStream(
      serverId,
      [CollectionHandler.protocolId],
      context,
    ).timeout(config.connectionTimeout);

    final response = await CollectionHandler.listCollectionKeys(
      stream,
      ownerPeerId: ownerPeerId,
      path: path,
      limit: limit,
      offset: offset,
    ).timeout(config.messageTimeout);

    await stream.close();

    if (!response.isSuccess) {
      _logger.warning('LIST collection keys $path: ${response.status}');
      return null;
    }

    final body = jsonDecode(utf8.decode(response.body!)) as Map<String, dynamic>;
    final keys = (body['keys'] as List<dynamic>).cast<String>();

    return CollectionKeysResult(
      keys: keys,
      totalCount: response.totalCount ?? keys.length,
      hasMore: response.hasMore ?? false,
    );
  } on TimeoutException {
    _logger.warning('LIST collection keys timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to LIST collection keys: $e');
    return null;
  }
}