postResultsBatch method

Future<BatchResponse> postResultsBatch(
  1. int testRunId,
  2. List<BatchItem> items
)

Uploads a batch of TestResult rows.

Implementation

Future<BatchResponse> postResultsBatch(int testRunId, List<BatchItem> items) {
  const label = 'results batch upload';
  return withUploadRetry('$label (${items.length})', () async {
    final body = utf8.encode(
      jsonEncode(<String, Object?>{
        'testRunId': testRunId,
        'items': items.map((item) => item.toWire()).toList(),
      }),
    );
    final _Response response;
    try {
      response = await _send(
        'POST',
        Uri.parse('${config.baseURL}/test-results/batch'),
        headers: _headers(json: true),
        body: body,
      );
    } on Object catch (error) {
      throw ApiError('$label failed: $error');
    }
    if (response.statusCode >= 400) {
      throw ApiError(
        '$label failed: HTTP ${response.statusCode} ${response.body.trim()}',
      );
    }
    final json = _decodeObject(response.body, label);
    final rawResults = json['results'];
    final results = <BatchItemResult>[];
    if (rawResults is List) {
      for (var i = 0; i < rawResults.length; i++) {
        final row = rawResults[i];
        if (row is! Map<String, Object?>) continue;
        final index = row['index'];
        final errorText = row['error'];
        results.add(
          BatchItemResult(
            index: index is int ? index : i,
            ok: row['ok'] == true,
            error: errorText is String && errorText.isNotEmpty
                ? errorText
                : null,
          ),
        );
      }
    }
    final returnedRunId = json['testRunId'];
    return BatchResponse(
      testRunId: returnedRunId is int && returnedRunId != 0
          ? returnedRunId
          : testRunId,
      results: results,
    );
  });
}