putCollectionItem method

Future<CollectionItemResult?> putCollectionItem({
  1. required String path,
  2. required String key,
  3. required Uint8List content,
  4. String? ifMatch,
  5. PeerId? toServer,
})

Put (create or update) a collection item

Implementation

Future<CollectionItemResult?> putCollectionItem({
  required String path,
  required String key,
  required Uint8List content,
  String? ifMatch,
  PeerId? toServer,
}) async {
  final serverId = toServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for collection item put');
    return null;
  }

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

    final response = await CollectionHandler.putCollectionItem(
      stream,
      ownerPeerId: host.id,
      path: path,
      key: key,
      content: content,
      ifMatch: ifMatch,
    ).timeout(config.messageTimeout);

    await stream.close();

    return CollectionItemResult(
      status: response.status,
      etag: response.etag,
      version: response.version,
      created: response.status == 201,
    );
  } on TimeoutException {
    _logger.warning('PUT collection item timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to PUT collection item: $e');
    return null;
  }
}