refactor(apps): simplify API layer by removing redundant wrapper classes
- Remove ApiClientWrapper and MockApiClientWrapper - Simplify IApiClient interface - Update dependency injection configuration - Optimize calendar service and AG-UI chat models
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'api_exception.dart';
|
||||
import 'api_interceptor.dart';
|
||||
import 'i_api_client.dart';
|
||||
import '../storage/token_storage.dart';
|
||||
|
||||
class ApiClient {
|
||||
class ApiClient implements IApiClient {
|
||||
final Dio _dio;
|
||||
final TokenStorage _tokenStorage;
|
||||
final ApiInterceptor _interceptor;
|
||||
@@ -44,6 +45,7 @@ class ApiClient {
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> get<T>(String path, {Options? options}) async {
|
||||
try {
|
||||
return await _dio.get<T>(path, options: options);
|
||||
@@ -52,6 +54,7 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> post<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
@@ -64,6 +67,7 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> patch<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
@@ -76,6 +80,7 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'api_client.dart';
|
||||
import 'mock_api_client.dart';
|
||||
|
||||
abstract class IApiClient {
|
||||
Future<Response<T>> get<T>(String path, {Options? options});
|
||||
@@ -8,53 +6,3 @@ abstract class IApiClient {
|
||||
Future<Response<T>> patch<T>(String path, {dynamic data, Options? options});
|
||||
Future<Response<T>> delete<T>(String path, {dynamic data, Options? options});
|
||||
}
|
||||
|
||||
class ApiClientWrapper implements IApiClient {
|
||||
final ApiClient _client;
|
||||
|
||||
ApiClientWrapper(this._client);
|
||||
|
||||
@override
|
||||
Future<Response<T>> get<T>(String path, {Options? options}) =>
|
||||
_client.get(path, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> post<T>(String path, {dynamic data, Options? options}) =>
|
||||
_client.post(path, data: data, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> patch<T>(String path, {dynamic data, Options? options}) =>
|
||||
_client.patch(path, data: data, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Options? options,
|
||||
}) => _client.delete(path, data: data, options: options);
|
||||
}
|
||||
|
||||
class MockApiClientWrapper implements IApiClient {
|
||||
final MockApiClient _client;
|
||||
|
||||
MockApiClientWrapper(this._client);
|
||||
|
||||
@override
|
||||
Future<Response<T>> get<T>(String path, {Options? options}) =>
|
||||
_client.get(path, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> post<T>(String path, {dynamic data, Options? options}) =>
|
||||
_client.post(path, data: data, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> patch<T>(String path, {dynamic data, Options? options}) =>
|
||||
_client.patch(path, data: data, options: options);
|
||||
|
||||
@override
|
||||
Future<Response<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Options? options,
|
||||
}) => _client.delete(path, data: data, options: options);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'i_api_client.dart';
|
||||
|
||||
class MockApiClient {
|
||||
final Map<String, _MockHandler> _handlers = {};
|
||||
typedef MockHandler = dynamic Function(dynamic data);
|
||||
|
||||
void registerHandler(String path, String method, _MockHandler handler) {
|
||||
class MockApiClient implements IApiClient {
|
||||
final Map<String, MockHandler> _handlers = {};
|
||||
|
||||
void registerHandler(String path, String method, MockHandler handler) {
|
||||
final key = '$path:$method';
|
||||
_handlers[key] = handler;
|
||||
}
|
||||
@@ -12,10 +15,12 @@ class MockApiClient {
|
||||
_handlers.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> get<T>(String path, {Options? options}) async {
|
||||
return _handleRequest('GET', path, options: options);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> post<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
@@ -24,6 +29,7 @@ class MockApiClient {
|
||||
return _handleRequest('POST', path, data: data, options: options);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> patch<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
@@ -32,6 +38,7 @@ class MockApiClient {
|
||||
return _handleRequest('PATCH', path, data: data, options: options);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
@@ -70,18 +77,3 @@ class MockApiClient {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef _MockHandler = dynamic Function(dynamic data);
|
||||
|
||||
class MockApiClientHolder {
|
||||
static MockApiClient? _instance;
|
||||
|
||||
static MockApiClient get instance {
|
||||
_instance ??= MockApiClient();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
static void reset() {
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../api/api_client.dart';
|
||||
import '../api/i_api_client.dart';
|
||||
import '../api/mock_api_client.dart';
|
||||
import '../storage/token_storage.dart';
|
||||
import '../config/env.dart';
|
||||
import '../../features/auth/data/auth_api.dart';
|
||||
@@ -13,20 +15,27 @@ import '../../features/calendar/ui/calendar_state_manager.dart';
|
||||
final sl = GetIt.instance;
|
||||
|
||||
Future<void> configureDependencies() async {
|
||||
if (sl.isRegistered<ApiClient>()) {
|
||||
if (sl.isRegistered<IApiClient>()) {
|
||||
await sl.reset();
|
||||
}
|
||||
|
||||
final dio = Dio(BaseOptions(baseUrl: Env.apiUrl));
|
||||
final secureStorage = const FlutterSecureStorage();
|
||||
final tokenStorage = SecureTokenStorage(secureStorage);
|
||||
final IApiClient apiClient;
|
||||
final SecureTokenStorage tokenStorage;
|
||||
|
||||
final apiClient = ApiClient(
|
||||
baseUrl: Env.apiUrl,
|
||||
tokenStorage: tokenStorage,
|
||||
dio: dio,
|
||||
);
|
||||
sl.registerSingleton<ApiClient>(apiClient);
|
||||
if (Env.isMockApi) {
|
||||
apiClient = MockApiClient();
|
||||
tokenStorage = SecureTokenStorage(const FlutterSecureStorage());
|
||||
} else {
|
||||
final dio = Dio(BaseOptions(baseUrl: Env.apiUrl));
|
||||
tokenStorage = SecureTokenStorage(const FlutterSecureStorage());
|
||||
apiClient = ApiClient(
|
||||
baseUrl: Env.apiUrl,
|
||||
tokenStorage: tokenStorage,
|
||||
dio: dio,
|
||||
);
|
||||
}
|
||||
|
||||
sl.registerSingleton<IApiClient>(apiClient);
|
||||
|
||||
final authApi = AuthApi(apiClient);
|
||||
sl.registerSingleton<AuthApi>(authApi);
|
||||
@@ -37,16 +46,17 @@ Future<void> configureDependencies() async {
|
||||
);
|
||||
sl.registerSingleton<AuthRepository>(authRepository);
|
||||
|
||||
apiClient.setRefreshCallback((token) async {
|
||||
try {
|
||||
await authRepository.refreshSession(token);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (!Env.isMockApi) {
|
||||
(apiClient as ApiClient).setRefreshCallback((token) async {
|
||||
try {
|
||||
await authRepository.refreshSession(token);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
sl.registerSingleton<AuthBloc>(AuthBloc(authRepository));
|
||||
|
||||
sl.registerSingleton<CalendarStateManager>(CalendarStateManager());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user