getCollection method

Future<CollectionInfo?> getCollection({
  1. required PeerId ownerPeerId,
  2. required String path,
  3. PeerId? fromServer,
})

Get collection metadata

Implementation

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

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

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

    await stream.close();

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

    final body = jsonDecode(utf8.decode(response.body!)) as Map<String, dynamic>;
    return CollectionInfo(
      path: body['path'] as String? ?? path,
      name: body['name'] as String? ?? '',
      recordCount: body['recordCount'] as int? ?? 0,
      lastModifiedAt: body['lastModifiedAt'] as int?,
      createdAt: body['createdAt'] as int? ?? 0,
    );
  } on TimeoutException {
    _logger.warning('GET collection timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to GET collection: $e');
    return null;
  }
}