runApiTestCommand function
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),
);
}