patch function

Future<Response> patch({
  1. dynamic onResponse(
    1. Response
    )?,
  2. dynamic onTimeout()?,
  3. dynamic onNoInternet()?,
  4. required String tag,
  5. required String url,
  6. Map<String, String> headers = defaultJsonHeaders,
  7. dynamic body,
  8. Map<String, String>? params,
  9. Duration timeoutDuration = defaultTimeoutDuration,
})

Implementation

Future<Response> patch({
  final Function(Response)? onResponse,
  final Function()? onTimeout,
  final Function()? onNoInternet,
  required final String tag,
  required final String url,
  final Map<String, String> headers = defaultJsonHeaders,
  final dynamic body,
  final Map<String, String>? params,
  final Duration timeoutDuration = defaultTimeoutDuration,
}) async {
  final fullUrl = await getFullURL(url, params);
  http.Response response = http.Response('', 444);

  try {
    response = await http
        .patch(Uri.parse(fullUrl), headers: headers, body: body)
        .timeout(
      timeoutDuration,
      onTimeout: () {
        printDebugLogFail(
          tag: tag,
          message: '\nRequest URL: $fullUrl'
              '\nRequest Method: PATCH'
              '\nRequest Headers: ${getPrettyJSONString(headers)}'
              '\nRequest Body: ${getPrettyJSONString(body)}'
              '\nRequest Timeout: $timeoutDuration',
        );

        if (onTimeout != null) onTimeout();

        return response;
      },
    ).then((http.Response response) {
      printDebugLogSuccess(
        tag: tag,
        message: '\nRequest URL: $fullUrl'
            '\nRequest Method: PATCH'
            '\nRequest Headers: ${getPrettyJSONString(headers)}'
            '\nRequest Body: ${getPrettyJSONString(body)}'
            '\nResponse Code: ${response.statusCode}'
            '\nResponse Body: ${getPrettyJSONString(response.body)}',
      );

      return response;
    });
    if (response.statusCode == 444) {
      return Response(
        status: ResponseStatus.timeout,
      );
    } else {
      onResponse?.call(Response(
        status: ResponseStatus.ok,
        response: response,
      ));

      return Response(
        status: ResponseStatus.ok,
        response: response,
      );
    }
  } catch (_) {
    if (await checkInternetConnection()) {
      return Response(status: ResponseStatus.unknownException);
    }

    return Response(status: ResponseStatus.connectionError);
  }
}