callAPI static method

Future<String> callAPI(
  1. String endpoint,
  2. Map<String, dynamic> parameters, {
  3. bool skipNative = false,
})

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';
  }
}