callAPI static method
Implementation
static Future<String> callAPI(String endpoint, Map<String, dynamic> parameters, {bool skipNative = false}) async {
try {
if (!skipNative) {
const MethodChannel _channel = MethodChannel('iezepay_common_sdk_mytestv1');
final response = await _channel.invokeMethod<String>('callAPI', {
'endpoint': endpoint,
'parameters': parameters,
});
if (response != 'callAPI') {
return 'API request was not forwarded properly from native.';
}
}
// Perform the actual HTTP request to localhost:3000
final apiUrl = 'http://localhost:3000/$endpoint';
final headers = {'Content-Type': 'application/json'};
final body = jsonEncode(parameters);
final httpResponse = await http.post(Uri.parse(apiUrl), headers: headers, body: body);
if (httpResponse.statusCode == 200) {
return httpResponse.body;
} else {
return 'Error: ${httpResponse.statusCode} - ${httpResponse.reasonPhrase}';
}
} catch (e) {
return 'Error: $e';
}
}