get function
Implementation
Future<Response> get({
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 Map<String, String>? params,
final Duration timeoutDuration = defaultTimeoutDuration,
bool needToPrintResponseLog = true,
}) async {
final fullUrl = await getFullURL(url, params);
http.Response response = http.Response('', 444);
try {
response = await http.get(Uri.parse(fullUrl), headers: headers).timeout(
timeoutDuration,
onTimeout: () {
printDebugLogFail(
tag: tag,
message: '\nRequest URL: $fullUrl'
'\nRequest Method: GET'
'\nRequest Headers: ${getPrettyJSONString(headers)}'
'\nRequest Timeout: $timeoutDuration',
);
if (onTimeout != null) onTimeout();
return response;
},
).then((http.Response response) {
final body = needToPrintResponseLog ? response.body : 'MIME Image';
printDebugLogSuccess(
tag: tag,
message: '\nRequest URL: $fullUrl'
'\nRequest Method: GET'
'\nRequest Headers: ${getPrettyJSONString(headers)}'
'\nResponse Code: ${response.statusCode}'
'\nResponse Body: ${getPrettyJSONString(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);
}
}