retrieveData<T> method

  1. @override
Future<Either<ErrorResponse, SuccessResponse<T>>> retrieveData<T>(
  1. BaseRequest request, {
  2. bool? cached,
  3. bool? forceRefresh,
  4. Duration? duration,
})
override

Implementation

@override
Future<Either<ErrorResponse, SuccessResponse<T>>> retrieveData<T>(
    BaseRequest request,
    {bool? cached,
    bool? forceRefresh,
    Duration? duration}) async {
  try {
    Options options = Options(
      headers: request.headers,
      method: request.method.rawValue,
      receiveDataWhenStatusError: true,
      receiveTimeout: request.receiveTimeout ?? requestReceiveTimeout,
      sendTimeout: request.sendTimeout ?? requestSendTimeout,
      responseType: request.responseType,
    );

    Response dioResponse = await _dioClient.request(
      request.completeUrl,
      data: request.formData ?? request.body,
      options: [
                TargetPlatform.iOS,
                TargetPlatform.android,
                TargetPlatform.macOS,
              ].contains(defaultTargetPlatform) &&
              (cached ?? false)
          ? buildCacheOptions(
              duration ?? const Duration(days: 7),
              forceRefresh: forceRefresh,
              options: options,
            )
          : options,
      queryParameters: request.queryParameters,
    );

    final decoder = request.decoder;

    if (decoder != null) {
      final data = decoder(dioResponse.data);
      return Right(SuccessResponse<T>(dioResponse.statusCode, data));
    } else {
      return Right(SuccessResponse(dioResponse.statusCode, dioResponse.data));
    }
  } on DioError catch (e, stacktrace) {
    return Left(manageDioError(
      e,
      stacktrace,
      decoder: request.decoder,
    ));
  } on Exception catch (e, stacktrace) {
    return Left(manageException(e, stacktrace));
  } catch (e, stacktrace) {
    return Left(manageError(e, stacktrace));
  }
}