get<T> static method
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());
}
}