apiCallForFile function
Future<Response>
apiCallForFile({
- dynamic onResponse()?,
- dynamic onTimeout()?,
- dynamic onNoInternet()?,
- required String tag,
- required HttpMethods apiMethod,
- required String url,
- Map<
String, String> headers = defaultJsonHeaders, - Map<
String, dynamic> ? body, - Map<
String, String> ? params, - Map<
String, AttachmentFile> ? keyAndfileMap, - List<
String> ? apiKeysForFiles, - List<
AttachmentFile> ? files, - Duration timeoutDuration = defaultTimeoutDuration,
- bool needToPrintResponseLog = true,
Implementation
Future<Response> apiCallForFile({
final Function(Response)? onResponse,
final Function()? onTimeout,
final Function()? onNoInternet,
required final String tag,
required final HttpMethods apiMethod,
required final String url,
final Map<String, String> headers = defaultJsonHeaders,
final Map<String, dynamic>? body,
final Map<String, String>? params,
final Map<String, AttachmentFile>? keyAndfileMap,
final List<String>? apiKeysForFiles,
final List<AttachmentFile>? files,
final Duration timeoutDuration = defaultTimeoutDuration,
final bool needToPrintResponseLog = true,
}) async {
final fullUrl = await getFullURL(url, params);
http.StreamedResponse response = http.StreamedResponse(
const Stream.empty(),
444,
);
final request = http.MultipartRequest(
apiMethod.toString().split('.').last.toUpperCase(),
Uri.parse(fullUrl),
);
request.headers.addAll(headers);
if (body != null && body.isNotEmpty) {
body.forEach((key, value) {
request.fields[key] = (value ?? '').toString();
});
}
if (keyAndfileMap != null && keyAndfileMap.isNotEmpty) {
keyAndfileMap.forEach((key, value) {
request.files.add(http.MultipartFile.fromBytes(
key,
value.data,
filename: value.fileNameWithExtension,
contentType: value.contentType,
));
});
} else if (files != null &&
apiKeysForFiles != null &&
files.length == apiKeysForFiles.length) {
for (int i = 0, l = files.length; i < l; i++) {
final file = files[i];
final key = apiKeysForFiles[i];
request.files.add(http.MultipartFile.fromBytes(
key,
file.data,
filename: file.fileNameWithExtension,
));
}
}
try {
response = await request.send().timeout(
timeoutDuration,
onTimeout: () {
printDebugLogFail(
tag: tag,
message: '\nRequest URL: $fullUrl'
'\nRequest Method: ${apiMethod.toString().split('.').last.toUpperCase()}'
'\nRequest Headers: ${getPrettyJSONString(headers)}'
'\nRequest Keys: ${keyAndfileMap?.entries.map((e) => e.key).toList()}'
'\nRequest Body: ${getPrettyJSONString(body)}'
'\nRequest Timeout: $timeoutDuration',
);
if (onTimeout != null) onTimeout();
return response;
},
);
if (response.statusCode == 444) {
return Response(status: ResponseStatus.timeout);
} else {
final res = await http.Response.fromStream(response);
printDebugLogSuccess(
tag: tag,
// ignore: prefer_interpolation_to_compose_strings
message: '\nRequest URL: $fullUrl'
'\nRequest Method: ${apiMethod.toString().split('.').last.toUpperCase()}'
'\nRequest Headers: ${getPrettyJSONString(headers)}'
'\nRequest Keys: ${keyAndfileMap?.values.map((AttachmentFile f) => f.path).toList()}'
'\nRequest Body: ${getPrettyJSONString(body)}'
'\nResponse Code: ${res.statusCode}' +
(needToPrintResponseLog
? '\nResponse Body: ${getPrettyJSONString(res.body)}'
: '\nResponse Body: Log is off'),
);
final finalRes = Response(
status: ResponseStatus.ok,
response: res,
);
onResponse?.call(finalRes);
return finalRes;
}
} catch (_) {
if (await checkInternetConnection()) {
return Response(status: ResponseStatus.unknownException);
}
return Response(status: ResponseStatus.connectionError);
}
}