commit method

Future<AlgoliaTask> commit()

Commit

Commits all of the writes in this write batch as a single atomic unit.

Calling this method prevents any future operations from being added.

Implementation

Future<AlgoliaTask> commit() async {
  if (!_committed) {
    if (_actions.isNotEmpty) {
      _committed = true;

      var actions = _actions.map((a) => a.toMap()).toList();

      var response = await algolia._apiCall(
        ApiRequestType.post,
        'indexes/$_index/batch',
        data: {'requests': actions},
      );

      Map<String, dynamic> body = json.decode(response.body);

      if (!(response.statusCode >= 200 && response.statusCode < 300)) {
        throw AlgoliaError._(body, response.statusCode);
      }
      return AlgoliaTask._(algolia, _index, body);
    } else {
      throw StateError('This batch has no actions to commit.');
    }
  } else {
    throw StateError('This batch has already been committed.');
  }
}