Files
social-app/apps/lib/core/api/api_client.dart
T

61 lines
1.5 KiB
Dart
Raw Normal View History

2026-02-25 14:36:03 +08:00
import 'package:dio/dio.dart';
import 'api_exception.dart';
import 'api_interceptor.dart';
import '../storage/token_storage.dart';
class ApiClient {
final Dio _dio;
final TokenStorage _tokenStorage;
final Future<bool> Function(String)? _refreshToken;
ApiClient({
required String baseUrl,
required TokenStorage tokenStorage,
Dio? dio,
Future<bool> Function(String)? refreshToken,
}) : _tokenStorage = tokenStorage,
_refreshToken = refreshToken,
_dio = dio ?? Dio(BaseOptions(baseUrl: baseUrl)) {
_dio.interceptors.add(
ApiInterceptor(
tokenStorage: _tokenStorage,
dio: _dio,
2026-02-25 14:36:03 +08:00
onTokenRefresh: _handleTokenRefresh,
),
);
}
Dio get dio => _dio;
Future<bool> _handleTokenRefresh() async {
final refreshToken = await _tokenStorage.getRefreshToken();
if (refreshToken == null || _refreshToken == null) return false;
try {
final success = await _refreshToken!(refreshToken);
return success;
} catch (_) {
return false;
}
}
Future<Response<T>> get<T>(String path, {Options? options}) async {
try {
return await _dio.get<T>(path, options: options);
} on DioException catch (e) {
2026-02-25 14:36:03 +08:00
throw ApiException.fromDioError(e);
}
}
Future<Response<T>> post<T>(
String path, {
dynamic data,
Options? options,
}) async {
try {
return await _dio.post<T>(path, data: data, options: options);
} on DioException catch (e) {
2026-02-25 14:36:03 +08:00
throw ApiException.fromDioError(e);
}
}
}