run method

Future<CommandResult> run(
  1. Command command, {
  2. Map<String, String> headers = const {},
})

Send a command to the backend and get the results of running it, that is whether it was successful and validation errors if there were any.

Headers provided in headers are on top of the headers from the Cqrs constructor, meaning headers override _headers. Content-Type header will be ignored.

A CqrsException will be thrown in case of an error.

Implementation

Future<CommandResult> run(
  Command command, {
  Map<String, String> headers = const {},
}) async {
  final response = await _send(
    command,
    pathPrefix: 'command',
    headers: headers,
  );

  if ([200, 422].contains(response.statusCode)) {
    try {
      final json = jsonDecode(response.body) as Map<String, dynamic>;

      return CommandResult.fromJson(json);
    } catch (e) {
      throw CqrsException(
        response,
        'An error occured while decoding response body JSON:\n$e',
      );
    }
  }

  throw CqrsException(
    response,
    'Invalid, non 200 or 422 status code returned '
    'by ${command.getFullName()} command.',
  );
}