fetch static method

Future<void> fetch({
  1. required Method method,
  2. required String path,
  3. required String publicKey,
  4. Map<String, dynamic>? payloads,
})

Implementation

static Future<void> fetch({
  required Method method,
  required String path,
  required String publicKey,
  Map<String, dynamic>? payloads,
}) async {
  var url = Uri.parse('$apiURL$path');
  try {
    http.Response response;
    if (method == Method.get) {
      response = await http.get(
        url,
        headers: {
          'Content-Type': 'application/json',
          'Api-Public-Key': publicKey,
          'Authorization': 'Bearer $publicKey',
        },
      );
    } else if (method == Method.post) {
      response = await http.post(
        url,
        headers: {
          'Content-Type': 'application/json',
          'Api-Public-Key': publicKey,
        },
        body: json.encode(payloads),
      );
    } else {
      throw Exception("Unknown Method! supported [get and post]");
    }
    return json.decode(response.body);
  } catch (e) {
    throw Exception(e);
  }
}