get<T> static method

Future<ApiResponse<T>> get<T>(
  1. String path, {
  2. Map<String, dynamic>? queryParameters,
  3. bool useCache = true,
  4. Duration? ttl,
  5. T fromJson(
    1. dynamic
    )?,
})

Performs a GET request with optional caching.

Implementation

static Future<ApiResponse<T>> get<T>(
  String path, {
  Map<String, dynamic>? queryParameters,
  bool useCache = true,
  Duration? ttl,
  T Function(dynamic)? fromJson,
}) async {
  _ensureInitialized();
  final cacheKey = 'GET::$path::$queryParameters';

  if (!ConnectivityService.isConnected && useCache) {
    final cached = await CacheManager.getJson(cacheKey);
    if (cached != null) {
      return ApiResponse.success(
        fromJson != null ? fromJson(cached) : cached as T,
      );
    }
  }

  try {
    final response = await _dio.get(path, queryParameters: queryParameters);
    if (useCache) {
      await CacheManager.putJson(
        cacheKey,
        response.data,
        ttl: ttl ?? const Duration(minutes: 10),
      );
    }
    return ApiResponse.success(
      fromJson != null ? fromJson(response.data) : response.data as T,
      status: response.statusCode,
    );
  } catch (e) {
    if (useCache) {
      final cached = await CacheManager.getJson(cacheKey);
      if (cached != null) {
        return ApiResponse.success(
          fromJson != null ? fromJson(cached) : cached as T,
        );
      }
    }
    return ApiResponse.error(e.toString());
  }
}