call static method
Future<void>
call(
- String url,
- RequestType requestType, {
- Map<
String, dynamic> ? headers, - Map<
String, dynamic> ? queryParameters, - required dynamic onSuccess(
- Response response
- dynamic onError(
- ApiException error
- dynamic onReceiveProgress()?,
- dynamic onSendProgress()?,
- Function? onLoading,
- CancelToken? cancelToken,
- 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);
}
}