call static method

Future<void> call(
  1. String url,
  2. RequestType requestType, {
  3. Map<String, dynamic>? headers,
  4. Map<String, dynamic>? queryParameters,
  5. required dynamic onSuccess(
    1. Response response
    ),
  6. dynamic onError(
    1. ApiException error
    )?,
  7. dynamic onReceiveProgress(
    1. int value,
    2. int progress
    )?,
  8. dynamic onSendProgress(
    1. int total,
    2. int progress
    )?,
  9. Function? onLoading,
  10. CancelToken? cancelToken,
  11. dynamic data,
})

perform safe api request

Implementation

static Future<void> call(
  String url,
  RequestType requestType, {
  Map<String, dynamic>? headers,
  Map<String, dynamic>? queryParameters,
  required Function(Response response) onSuccess,
  Function(ApiException error)? onError,
  Function(int value, int progress)? onReceiveProgress,
  Function(int total, int progress)?
  onSendProgress, // while sending (uploading) progress
  Function? onLoading,
  CancelToken? cancelToken,
  dynamic data,
}) async {
  try {
    // 1) indicate loading state
    await onLoading?.call();
    // 2) try to perform http request
    late Response response;
    if (requestType == RequestType.get) {
      response = await _dio.get(
        url,
        onReceiveProgress: onReceiveProgress,
        queryParameters: queryParameters,
        options: Options(headers: headers),
      );
    } else if (requestType == RequestType.post) {
      response = await _dio.post(
        url,
        data: data,
        onReceiveProgress: onReceiveProgress,
        onSendProgress: onSendProgress,
        queryParameters: queryParameters,
        options: Options(headers: headers),
        cancelToken: cancelToken,
      );
    } else if (requestType == RequestType.put) {
      response = await _dio.put(
        url,
        data: data,
        onReceiveProgress: onReceiveProgress,
        onSendProgress: onSendProgress,
        queryParameters: queryParameters,
        options: Options(headers: headers),
      );
    } else if (requestType == RequestType.patch) {
      response = await _dio.patch(
        url,
        data: data,
        onReceiveProgress: onReceiveProgress,
        onSendProgress: onSendProgress,
        queryParameters: queryParameters,
        options: Options(headers: headers),
      );
    } else {
      response = await _dio.delete(
        url,
        data: data,
        queryParameters: queryParameters,
        options: Options(headers: headers),
      );
    }
    await onSuccess(response);
  } on DioException catch (error) {
    _handleDioError(error: error, url: url, onError: onError);
  } on SocketException {
    _handleSocketException(url: url, onError: onError);
  } on TimeoutException {
    _handleTimeoutException(url: url, onError: onError);
  } catch (error) {
    _handleUnexpectedException(url: url, onError: onError, error: error);
  }
}