putCollectionItem static method

Future<CollectionFrameResponse> putCollectionItem(
  1. P2PStream stream, {
  2. required PeerId ownerPeerId,
  3. required String path,
  4. required String key,
  5. required Uint8List content,
  6. String? ifMatch,
})

Put (create or update) a collection item

Implementation

static Future<CollectionFrameResponse> putCollectionItem(
  P2PStream stream, {
  required PeerId ownerPeerId,
  required String path,
  required String key,
  required Uint8List content,
  String? ifMatch,
}) async {
  try {
    final headers = <String, String>{};
    if (ifMatch != null) {
      headers['If-Match'] = ifMatch;
    }

    final requestBytes = CollectionFrame.encodeRequest(
      operation: 'PUT',
      ownerPeerId: ownerPeerId,
      path: path,
      key: key,
      headers: headers.isNotEmpty ? headers : null,
      body: content,
    );

    await _writeFrameStatic(stream, requestBytes);
    final responseBytes = await _readFrameStatic(stream);
    return CollectionFrame.decodeResponse(responseBytes);
  } catch (e, stackTrace) {
    _logger.severe('Error putting collection item: $e', e, stackTrace);
    rethrow;
  }
}