callPutApi static method

Future callPutApi(
  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. String? paramAsBody,
  11. Uint8List? paramAsBodyBinary,
  12. Map<String, String>? customHeader,
  13. String? changeLocalization,
  14. String tokenKey = 'Bearer ',
  15. bool? useDefaultURl,
  16. bool? showLogs,
  17. bool? usePreCheckFn,
  18. int? callTimeoutInSec,
})

Implementation

static Future<dynamic> callPutApi(String endPoint, Map params,
    {bool hasAuth = true,
    bool hasEncoded = true,
    required String token,
    bool? defaultResponse,
    bool? withStream,
    bool? utf8Convert,
    bool isTypeJson = true,
    String? paramAsBody,
      Uint8List? paramAsBodyBinary,
    Map<String, String>? customHeader,
    String? changeLocalization,
    String tokenKey = 'Bearer ',
    bool? useDefaultURl,
    bool? showLogs,
      bool? usePreCheckFn,

    int? callTimeoutInSec}) async {
  dynamic response;

  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!);
  }
  showLog((customHeader ?? httpHeader ?? header),
      showLog: showLogs, logName: endPoint);
  try {
    if(paramAsBodyBinary != null){
      var response = await http.put(Uri.parse(endPoint), body: paramAsBodyBinary);
      return response;
    }
    if (withStream ?? httpCallsWithStream) {
      var request = http.Request('PUT', url);
      if(paramAsBodyBinary != null){
        request.bodyBytes = paramAsBodyBinary;
    }
    request.body = 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 (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);
      }
    } else {
      var result = await http
          .put(url,
              headers: customHeader ?? httpHeader ?? header,
              body: paramAsBody ?? utf8.encode(json.encode(params)))
          .timeout(
              Duration(seconds: callTimeoutInSec ?? httpCallTimeoutInSec));
      if (result.statusCode < Static.stopDecodingFromErrorCode) {
        response =
            HttpCalls.getDataObject(result, defaultResponse: defaultResponse);
        showLog((params), showLog: showLogs, logName: endPoint);
        showLog(result.body.toString(),
            enableJsonEncode: false, showLog: showLogs, logName: endPoint);
      } else {
        throw Exception(result);
      }
    }
  } catch (e) {
    response = errorHandler(e, response, defaultResponse);
  }
  return response;
}