callGetApi static method

Future callGetApi(
  1. String endPoint, {
  2. bool hasAuth = true,
  3. required String token,
  4. bool? defaultResponse,
  5. bool? withStream,
  6. bool? utf8Convert,
  7. bool isTypeJson = true,
  8. Map<String, String>? customHeader,
  9. String? changeLocalization,
  10. String tokenKey = 'Bearer ',
  11. bool? useDefaultURl,
  12. bool? showLogs,
  13. int? callTimeoutInSec,
  14. bool? usePreCheckFn,
})

Implementation

static Future<dynamic> callGetApi(
  String endPoint, {
  bool hasAuth = true,
  required String token,
  bool? defaultResponse,
  bool? withStream,
  bool? utf8Convert,
  bool isTypeJson = true,
  Map<String, String>? customHeader,
  String? changeLocalization,
  String tokenKey = 'Bearer ',
  bool? useDefaultURl,
  bool? showLogs,
  int? callTimeoutInSec,
      bool? usePreCheckFn,
}) async {
  dynamic response;

  try {
  await callPreCheckFn(usePreCheckFn);

  Uri url = HttpCalls.getRequestURL(endPoint, useDefaultURl: useDefaultURl);
  showLog(url, showLog: showLogs, logName: endPoint);

  final Map<String, String> header = {};

  if ((localization ?? changeLocalization) != null) {
    header['X-localization'] = localization ?? changeLocalization ?? '';
    header['Accept-Language'] = localization ?? changeLocalization ?? '';
  }

  if (isTypeJson) {
    header[HttpHeaders.contentTypeHeader] = 'application/json';
  }

  if (hasAuth) {
    header[Static.httpCallTokenKey??HttpHeaders.authorizationHeader] = '${Static.canHttpCallAddBearerAsPreToken?tokenKey:''}$token';
  }

  if (headerAddOns != null) {
    header.addAll(headerAddOns!);
  }

  showLog((customHeader ?? httpHeader ?? header),
      showLog: showLogs, logName: endPoint);

    if (withStream ?? httpCallsWithStream) {
      var request = http.Request('GET', url);
      request.headers.addAll(customHeader ?? httpHeader ?? header);
      var streamedResponse = await request.send().timeout(
          Duration(seconds: callTimeoutInSec ?? httpCallTimeoutInSec));
      var result = await Response.fromStream(streamedResponse);
      if (result.statusCode < Static.stopDecodingFromErrorCode) {
        if (utf8Convert ?? httpResponseUtf8Convert) {
          response = HttpCalls.getDataObject(
              Response(utf8.decoder.convert(result.bodyBytes),
                  streamedResponse.statusCode),
              defaultResponse: defaultResponse);

          showLog(utf8.decoder.convert(result.bodyBytes).toString(),
              enableJsonEncode: false, showLog: showLogs, logName: endPoint);
        } else {
          response = HttpCalls.getDataObject(result,
              defaultResponse: defaultResponse);
          showLog(result.body.toString(),
              enableJsonEncode: false, showLog: showLogs, logName: endPoint);
        }
      } else {
        throw Exception(result);
      }
    } else {
      var result = await http
          .get(
            url,
            headers: customHeader ?? httpHeader ?? header,
          )
          .timeout(
              Duration(seconds: callTimeoutInSec ?? httpCallTimeoutInSec));
      if (result.statusCode < Static.stopDecodingFromErrorCode) {
        response =
            HttpCalls.getDataObject(result, defaultResponse: defaultResponse);

        showLog(result.body.toString(),
            enableJsonEncode: false, showLog: showLogs, logName: endPoint);
      } else {
        throw Exception(result);
      }
    }
  } on Exception catch (e) {

    response = errorHandler(e, response, defaultResponse);
  }
  return response;
}