callDeleteApi static method

Future callDeleteApi(
  1. String endPoint,
  2. Map params, {
  3. bool hasAuth = true,
  4. bool hasEncoded = true,
  5. required String token,
  6. bool? defaultResponse,
  7. bool? withStream,
  8. bool? utf8Convert,
  9. bool isTypeJson = true,
  10. Map<String, String>? customHeader,
  11. String? paramAsBody,
  12. String? changeLocalization,
  13. String tokenKey = 'Bearer ',
  14. bool? useDefaultURl,
  15. bool? showLogs,
  16. bool? usePreCheckFn,
  17. int? callTimeoutInSec,
  18. Future<T> httpCallPreFunction<T>()?,
  19. bool callHttpCallPreFunction = true,
  20. Future<T> httpCallPostFunction<T>()?,
  21. bool callHttpCallPostFunction = true,
})

Implementation

static Future<dynamic> callDeleteApi(String endPoint, Map params,
    {bool hasAuth = true,
    bool hasEncoded = true,
    required String token,
    bool? defaultResponse,
    bool? withStream,
    bool? utf8Convert,
    bool isTypeJson = true,
    Map<String, String>? customHeader,
    String? paramAsBody,
    String? changeLocalization,
    String tokenKey = 'Bearer ',
    bool? useDefaultURl,
    bool? showLogs,
    bool? usePreCheckFn,
    int? callTimeoutInSec,
      Future<T> Function<T>()? httpCallPreFunction,
      bool callHttpCallPreFunction = true,
      Future<T> Function<T>()? httpCallPostFunction,
      bool callHttpCallPostFunction = true,
    }) async {
  dynamic response;
  try {
    if(callHttpCallPreFunction){
      if(httpCallPreFunction != null){
        await httpCallPreFunction();
      }else{
        if(HttpCalls.httpCallPreFunction != null)await HttpCalls.httpCallPreFunction!();
      }
    }
    await callPreCheckFn(usePreCheckFn);
    showLog((params), showLog: showLogs, logName: endPoint);

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

    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!);
    }

    if (httpParamsAddOns != null) {
      params.addAll(httpParamsAddOns!);
    }

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

    var request = http.Request('DELETE', url);
    request.body = paramAsBody ?? json.encode(params);
    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),
            enableJsonEncode: false, showLog: showLogs, logName: endPoint);
      } else {
        response =
            HttpCalls.getDataObject(result, defaultResponse: defaultResponse);
        showLog(result.body.toString(),
            enableJsonEncode: false, showLog: showLogs, logName: endPoint);
      }
    }
    if(callHttpCallPostFunction){
      if(httpCallPostFunction != null){
        await httpCallPostFunction();
      }else{
        if(HttpCalls.httpCallPostFunction != null)await HttpCalls.httpCallPostFunction!();
      }
    }
  } catch (e) {
    response = errorHandler(e, response, defaultResponse);
  }
  return response;
}