runApiTestCommand function

Future<void> runApiTestCommand(
  1. String projectPath
)

Executes the api test command.

Prompts the user for:

  • API URL
  • HTTP method
  • Request headers
  • Request body

After executing the API request, the CLI:

  • Prints the server response
  • Extracts the server message if available
  • Allows generating a Flutter page module from the response.

Implementation

Future<void> runApiTestCommand(String projectPath) async {

  print("Enter API URL:");
  final url = stdin.readLineSync();

  if (url == null || url.isEmpty) {
    print("āŒ API URL required.");
    return;
  }

  print("HTTP Method (GET/POST/PUT/DELETE):");
  final method = stdin.readLineSync()?.toUpperCase() ?? "GET";

  /// ======================================
  /// READ HEADERS FROM USER PROJECT
  /// ======================================

  Map<String, String> headers = _readProjectHeaders(projectPath);

  if (headers.isNotEmpty) {
    print("\nšŸ“¦ Headers loaded from user project:");
    headers.forEach((k, v) => print("$k : $v"));
  }

  /// ======================================
  /// EXTRA HEADERS INPUT
  /// ======================================

  print("\nEnter extra headers JSON (optional, press ENTER twice to skip):");

  final headerBuffer = StringBuffer();

  while (true) {
    final line = stdin.readLineSync();

    if (line == null || line.trim().isEmpty) {
      break;
    }

    headerBuffer.writeln(line);
  }

  final headerText = headerBuffer.toString().trim();

  if (headerText.isNotEmpty) {
    try {

      final extraHeaders =
      Map<String, dynamic>.from(jsonDecode(headerText));

      extraHeaders.forEach((key, value) {
        headers[key] = value.toString();
      });

    } catch (e) {
      print("āŒ Invalid Headers JSON");
      return;
    }
  }

  Map<String, dynamic>? requestBody;

  /// ======================================
  /// REQUEST BODY INPUT
  /// ======================================

  if (method == "POST" || method == "PUT") {

    print("\nEnter Request JSON (press ENTER twice to finish):");

    final bodyBuffer = StringBuffer();

    while (true) {
      final line = stdin.readLineSync();

      if (line == null || line.trim().isEmpty) {
        break;
      }

      bodyBuffer.writeln(line);
    }

    final bodyText = bodyBuffer.toString().trim();

    if (bodyText.isNotEmpty) {
      try {
        requestBody = Map<String, dynamic>.from(jsonDecode(bodyText));
      } catch (e) {
        print("āŒ $e");
        return;
      }
    }
  }

  print("\nšŸ“” Calling API...");

  http.Response response;

  try {

    if (method == "POST") {

      response = await http.post(
        Uri.parse(url),
        headers: headers,
        body: requestBody != null ? jsonEncode(requestBody) : null,
      );

    } else if (method == "PUT") {

      response = await http.put(
        Uri.parse(url),
        headers: headers,
        body: requestBody != null ? jsonEncode(requestBody) : null,
      );

    } else if (method == "DELETE") {

      response = await http.delete(
        Uri.parse(url),
        headers: headers,
      );

    } else {

      response = await http.get(
        Uri.parse(url),
        headers: headers,
      );

    }

  } catch (e) {

    print("āŒ API call failed: $e");
    return;

  }

  /// ======================================
  /// HANDLE RESPONSE
  /// ======================================

  dynamic jsonResponse;

  try {

    jsonResponse = jsonDecode(response.body);

    if (jsonResponse is Map && jsonResponse.containsKey("message")) {

      print("\nāœ… Server Message:");
      print(jsonResponse["message"]);

    }

    print("\nšŸ“¦ Full Response:");
    print(const JsonEncoder.withIndent('  ').convert(jsonResponse));

  } catch (_) {

    print("\n⚠ Server Response:");
    print(response.body);
    return;

  }

  print("\nGenerate module from this response? (y/n)");

  final confirm = stdin.readLineSync();

  if (confirm?.toLowerCase() != "y") return;

  print("Enter module name:");

  final moduleName = stdin.readLineSync();

  if (moduleName == null || moduleName.isEmpty) {
    print("āŒ Module name required.");
    return;
  }

  /// Command used to generate enterprise page modules.
  await generatePageModule(
    projectPath,
    moduleName,
    jsonInput: jsonEncode(jsonResponse),
  );

}