request method

Future<Response> request(
  1. RequestType type,
  2. String path, {
  3. bool https = true,
  4. Object? data = '',
  5. Map<String, dynamic>? queryParameters,
  6. String contentType = 'application/json',
  7. String accept = 'text/plain',
  8. String? token,
  9. String tokenPrefix = 'Bearer',
})

Implementation

Future<http.Response> request(
  RequestType type,
  String path, {
  bool https = true,
  Object? data = '',
  Map<String, dynamic>? queryParameters,
  String contentType = 'application/json',
  String accept = 'text/plain',
  String? token,
  String tokenPrefix = 'Bearer',
}) async {
  late final Uri url;
  if (https) {
    url = Uri.https(base, path, queryParameters);
  } else {
    url = Uri.http(base, path, queryParameters);
  }

  final headers = <String, String>{};
  if (token != null) {
    headers['Authorization'] = '$tokenPrefix $token';
  }
  if (type == RequestType.PUT ||
      type == RequestType.POST ||
      type == RequestType.PATCH) {
    headers['Content-Type'] = contentType;
    headers['accept'] = accept;
  }
  dynamic json;
  (data is! String) ? json = jsonEncode(data) : json = data;

  late http.Response resp;
  try {
    switch (type) {
      case RequestType.GET:
        resp = await http.get(url, headers: headers);
        break;
      case RequestType.PATCH:
        resp = await http.patch(url, body: json, headers: headers);
        break;
      case RequestType.POST:
        resp = await http.post(url, body: json, headers: headers);
        break;
      case RequestType.PUT:
        resp = await http.put(url, body: json, headers: headers);
        break;
      case RequestType.DELETE:
        resp = await http.delete(url, headers: headers);
        break;
    }
    return resp;
  } catch (e, s) {
    throw Exception(e.toString() + s.toString());
  }
}