2026-03-07 17:30:20 +08:00
|
|
|
import 'dart:convert';
|
2026-02-25 14:36:03 +08:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'api_exception.dart';
|
|
|
|
|
import 'api_interceptor.dart';
|
2026-03-03 10:12:46 +08:00
|
|
|
import 'i_api_client.dart';
|
2026-02-25 14:36:03 +08:00
|
|
|
import '../storage/token_storage.dart';
|
|
|
|
|
|
2026-03-03 10:12:46 +08:00
|
|
|
class ApiClient implements IApiClient {
|
2026-02-25 14:36:03 +08:00
|
|
|
final Dio _dio;
|
|
|
|
|
final TokenStorage _tokenStorage;
|
2026-02-25 18:00:02 +08:00
|
|
|
final ApiInterceptor _interceptor;
|
2026-02-25 14:36:03 +08:00
|
|
|
|
2026-02-25 18:00:02 +08:00
|
|
|
factory ApiClient({
|
2026-02-25 14:36:03 +08:00
|
|
|
required String baseUrl,
|
|
|
|
|
required TokenStorage tokenStorage,
|
|
|
|
|
Dio? dio,
|
2026-02-25 18:00:02 +08:00
|
|
|
}) {
|
2026-03-18 13:35:25 +08:00
|
|
|
final effectiveDio =
|
|
|
|
|
dio ??
|
|
|
|
|
Dio(
|
|
|
|
|
BaseOptions(
|
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
|
connectTimeout: const Duration(seconds: 10),
|
|
|
|
|
receiveTimeout: const Duration(seconds: 20),
|
|
|
|
|
sendTimeout: const Duration(seconds: 20),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-02-25 18:00:02 +08:00
|
|
|
final interceptor = ApiInterceptor(
|
|
|
|
|
tokenStorage: tokenStorage,
|
|
|
|
|
dio: effectiveDio,
|
|
|
|
|
);
|
|
|
|
|
effectiveDio.interceptors.add(interceptor);
|
|
|
|
|
return ApiClient._(
|
|
|
|
|
dio: effectiveDio,
|
|
|
|
|
tokenStorage: tokenStorage,
|
|
|
|
|
interceptor: interceptor,
|
2026-02-25 14:36:03 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 18:00:02 +08:00
|
|
|
ApiClient._({
|
|
|
|
|
required Dio dio,
|
|
|
|
|
required TokenStorage tokenStorage,
|
|
|
|
|
required ApiInterceptor interceptor,
|
|
|
|
|
}) : _dio = dio,
|
|
|
|
|
_tokenStorage = tokenStorage,
|
|
|
|
|
_interceptor = interceptor;
|
|
|
|
|
|
2026-02-25 14:36:03 +08:00
|
|
|
Dio get dio => _dio;
|
|
|
|
|
|
2026-03-11 21:33:25 +08:00
|
|
|
void resetInterceptor() {
|
|
|
|
|
_interceptor.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 18:00:02 +08:00
|
|
|
void setRefreshCallback(Future<bool> Function(String) refresh) {
|
|
|
|
|
_interceptor.onTokenRefresh = () async {
|
|
|
|
|
final token = await _tokenStorage.getRefreshToken();
|
|
|
|
|
if (token == null) return false;
|
|
|
|
|
return refresh(token);
|
|
|
|
|
};
|
2026-02-25 14:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 13:35:25 +08:00
|
|
|
void setAuthFailureCallback(Future<void> Function() onAuthFailure) {
|
|
|
|
|
_interceptor.onAuthFailure = onAuthFailure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
2026-02-25 14:36:03 +08:00
|
|
|
Future<Response<T>> get<T>(String path, {Options? options}) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _dio.get<T>(path, options: options);
|
2026-02-25 14:41:27 +08:00
|
|
|
} on DioException catch (e) {
|
2026-02-25 14:36:03 +08:00
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 10:12:46 +08:00
|
|
|
@override
|
2026-02-25 14:36:03 +08:00
|
|
|
Future<Response<T>> post<T>(
|
|
|
|
|
String path, {
|
|
|
|
|
dynamic data,
|
|
|
|
|
Options? options,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _dio.post<T>(path, data: data, options: options);
|
2026-02-25 14:41:27 +08:00
|
|
|
} on DioException catch (e) {
|
2026-02-25 14:36:03 +08:00
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-26 14:28:58 +08:00
|
|
|
|
2026-03-03 10:12:46 +08:00
|
|
|
@override
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<Response<T>> patch<T>(
|
|
|
|
|
String path, {
|
|
|
|
|
dynamic data,
|
|
|
|
|
Options? options,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _dio.patch<T>(path, data: data, options: options);
|
2026-03-23 14:25:47 +08:00
|
|
|
} on DioException catch (e) {
|
|
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Response<T>> put<T>(
|
|
|
|
|
String path, {
|
|
|
|
|
dynamic data,
|
|
|
|
|
Options? options,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _dio.put<T>(path, data: data, options: options);
|
2026-02-26 14:28:58 +08:00
|
|
|
} on DioException catch (e) {
|
|
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 10:12:46 +08:00
|
|
|
@override
|
2026-02-26 14:28:58 +08:00
|
|
|
Future<Response<T>> delete<T>(
|
|
|
|
|
String path, {
|
|
|
|
|
dynamic data,
|
|
|
|
|
Options? options,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
return await _dio.delete<T>(path, data: data, options: options);
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-07 17:30:20 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Stream<String>> getSseLines(
|
|
|
|
|
String path, {
|
|
|
|
|
Map<String, String>? headers,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await _dio.get<ResponseBody>(
|
|
|
|
|
path,
|
2026-03-11 21:33:25 +08:00
|
|
|
options: Options(responseType: ResponseType.stream, headers: headers),
|
2026-03-07 17:30:20 +08:00
|
|
|
);
|
|
|
|
|
final responseBody = response.data;
|
|
|
|
|
if (responseBody == null) {
|
|
|
|
|
return const Stream<String>.empty();
|
|
|
|
|
}
|
|
|
|
|
return responseBody.stream
|
|
|
|
|
.cast<List<int>>()
|
|
|
|
|
.transform(utf8.decoder)
|
|
|
|
|
.transform(const LineSplitter());
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
throw ApiException.fromDioError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 14:36:03 +08:00
|
|
|
}
|