downloadFile static method
Downloads content from path and saves it to saveToPath.
Returns a success message or throws on error.
Implementation
static Future<String> downloadFile(
String path,
String saveToPath, {
Map<String, String>? headers,
OnSuccess? onSuccess,
OnError? onError,
}) async {
try {
final response = await http.get(
Uri.parse('$baseUrl$path'),
headers: {...defaultHeaders, ...?headers},
);
if (response.statusCode == 200) {
final file = File(saveToPath);
await file.writeAsBytes(response.bodyBytes);
final message = 'File saved to $saveToPath';
onSuccess?.call(message);
return message;
} else {
throw FinnUtilsClientException('Download failed: ${response.body}');
}
} catch (e) {
onError?.call(FinnUtilsClientException(e.toString()));
rethrow;
}
}