run method

Future<List<APIResponse>> run(
  1. FutureOr<String?> nextLine(), {
  2. bool returnResponses = true,
  3. dynamic onRequest(
    1. APIRequest request
    )?,
  4. dynamic onResponse(
    1. APIResponse response
    )?,
})

Runs the console interaction.

Implementation

Future<List<APIResponse>> run(
  FutureOr<String?> Function() nextLine, {
  bool returnResponses = true,
  Function(APIRequest request)? onRequest,
  Function(APIResponse response)? onResponse,
}) async {
  var responses = <APIResponse>[];

  while (true) {
    var ret = nextLine();

    String? line;
    if (ret is Future) {
      line = await ret;
    } else {
      line = ret;
    }

    if (line == null) {
      return responses;
    }

    var request = APIRequest.fromArgsLine(line);

    if (onRequest != null) {
      var ret = onRequest(request);
      if (ret is Future) {
        ret = await ret;
      }

      if (ret is APIRequest) {
        request = ret;
      }
    }

    var response = await processRequest(request);

    if (returnResponses) {
      responses.add(response);
    }

    if (onResponse != null) {
      var ret = onResponse(response);
      if (ret is Future) {
        await ret;
      }
    }
  }
}