download method

Future<Either<BaseException, String>> download(
  1. String path, {
  2. Map<String, dynamic>? params,
  3. OnProgressFunction? onProgress,
  4. RequestEnum type = .get,
  5. required Future<String> saver(
    1. Uint8List bytes
    ),
})

Implementation

Future<Either<BaseException, String>> download(
  String path, {
  Map<String, dynamic>? params,
  OnProgressFunction? onProgress,
  RequestEnum type = .get,
  required Future<String> Function(Uint8List bytes) saver,
}) async {
  try {
    final options = Options(
      responseType: ResponseType.bytes,
      headers: {
        'accept': '*/*',
        'Content-Type': 'application/json',
      },
    );

    final response = await switch (type) {
      .get => _dio.get(
        path,
        queryParameters: params,
        options: options,
        onReceiveProgress: (received, total) => onProgress?.call(received / total),
      ),
      .post => _dio.post(
        path,
        data: params,
        options: options,
        onReceiveProgress: (received, total) => onProgress?.call(received / total),
      ),
    };

    return Right(await saver(Uint8List.fromList(response.data)));
  } catch (err, s) {
    return ErrorHandler.fromError(err, stackTrace: s);
  }
}