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());
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import 'package:social_app/core/api/api_client.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'models/signup_request.dart';
|
||||
import 'models/login_request.dart';
|
||||
import 'models/auth_response.dart';
|
||||
|
||||
class AuthApi {
|
||||
final ApiClient _client;
|
||||
final IApiClient _client;
|
||||
static const _prefix = '/api/v1/auth';
|
||||
|
||||
AuthApi(this._client);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:social_app/core/config/env.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import '../models/schedule_item_model.dart';
|
||||
|
||||
class MockCalendarService {
|
||||
@@ -56,54 +56,50 @@ class MockCalendarService {
|
||||
}
|
||||
|
||||
class CalendarService {
|
||||
static final CalendarService _instance = CalendarService._internal();
|
||||
factory CalendarService() => _instance;
|
||||
CalendarService._internal();
|
||||
final IApiClient? _apiClient;
|
||||
final MockCalendarService _mock = MockCalendarService();
|
||||
|
||||
MockCalendarService get _mock => MockCalendarService();
|
||||
CalendarService({IApiClient? apiClient}) : _apiClient = apiClient;
|
||||
|
||||
List<ScheduleItemModel> getEventsForDay(DateTime date) {
|
||||
if (Env.isMockApi) {
|
||||
return _mock.getEventsForDay(date);
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
return _mock.getEventsForDay(date);
|
||||
}
|
||||
|
||||
List<ScheduleItemModel> getEventsForRange(DateTime start, DateTime end) {
|
||||
if (Env.isMockApi) {
|
||||
return _mock.getEventsForRange(start, end);
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
return _mock.getEventsForRange(start, end);
|
||||
}
|
||||
|
||||
ScheduleItemModel? getEventById(String id) {
|
||||
if (Env.isMockApi) {
|
||||
return _mock.getEventById(id);
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
return _mock.getEventById(id);
|
||||
}
|
||||
|
||||
void addEvent(ScheduleItemModel event) {
|
||||
if (Env.isMockApi) {
|
||||
_mock.addEvent(event);
|
||||
return;
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
_mock.addEvent(event);
|
||||
}
|
||||
|
||||
void updateEvent(ScheduleItemModel event) {
|
||||
if (Env.isMockApi) {
|
||||
_mock.updateEvent(event);
|
||||
return;
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
_mock.updateEvent(event);
|
||||
}
|
||||
|
||||
void deleteEvent(String id) {
|
||||
if (Env.isMockApi) {
|
||||
_mock.deleteEvent(id);
|
||||
return;
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
_mock.deleteEvent(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,7 +304,7 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen> {
|
||||
vertical: 2,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.2),
|
||||
color: color.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(
|
||||
|
||||
@@ -368,7 +368,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
Row(
|
||||
children: defaultColors.map((color) {
|
||||
final colorHex =
|
||||
'#${color.value.toRadixString(16).substring(2).toUpperCase()}';
|
||||
'#${color.toARGB32().toRadixString(16).substring(2).toUpperCase()}';
|
||||
final isSelected = _selectedColor == colorHex;
|
||||
return GestureDetector(
|
||||
onTap: () => setState(() => _selectedColor = colorHex),
|
||||
|
||||
@@ -89,7 +89,7 @@ final _typeToFactory = {
|
||||
AgUiEventType.unknown: UnknownAgUiEvent.fromJson,
|
||||
};
|
||||
|
||||
@JsonSerializable()
|
||||
@JsonSerializable(createFactory: false)
|
||||
class AgUiEvent {
|
||||
final AgUiEventType type;
|
||||
|
||||
@@ -104,7 +104,7 @@ class AgUiEvent {
|
||||
Map<String, dynamic> toJson() => _$AgUiEventToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
@JsonSerializable(createFactory: false, createToJson: false)
|
||||
class UnknownAgUiEvent extends AgUiEvent {
|
||||
final Map<String, dynamic> rawJson;
|
||||
|
||||
|
||||
@@ -6,9 +6,6 @@ part of 'ag_ui_event.dart';
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
AgUiEvent _$AgUiEventFromJson(Map<String, dynamic> json) =>
|
||||
AgUiEvent(type: $enumDecode(_$AgUiEventTypeEnumMap, json['type']));
|
||||
|
||||
Map<String, dynamic> _$AgUiEventToJson(AgUiEvent instance) => <String, dynamic>{
|
||||
'type': _$AgUiEventTypeEnumMap[instance.type]!,
|
||||
};
|
||||
@@ -29,12 +26,6 @@ const _$AgUiEventTypeEnumMap = {
|
||||
AgUiEventType.unknown: 'unknown',
|
||||
};
|
||||
|
||||
UnknownAgUiEvent _$UnknownAgUiEventFromJson(Map<String, dynamic> json) =>
|
||||
UnknownAgUiEvent(rawJson: json['rawJson'] as Map<String, dynamic>);
|
||||
|
||||
Map<String, dynamic> _$UnknownAgUiEventToJson(UnknownAgUiEvent instance) =>
|
||||
<String, dynamic>{'rawJson': instance.rawJson};
|
||||
|
||||
RunStartedEvent _$RunStartedEventFromJson(Map<String, dynamic> json) =>
|
||||
RunStartedEvent(
|
||||
threadId: json['threadId'] as String,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:social_app/core/config/env.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
|
||||
import '../ai/ai_decision_engine.dart';
|
||||
import '../models/ag_ui_event.dart';
|
||||
@@ -24,27 +24,29 @@ const _textChunkSize = 10;
|
||||
typedef EventCallback = void Function(AgUiEvent event);
|
||||
|
||||
class AgUiService {
|
||||
final IApiClient? _apiClient;
|
||||
EventCallback onEvent;
|
||||
final AiDecisionEngine _decisionEngine;
|
||||
final MockHistoryService _historyService;
|
||||
|
||||
AgUiService({EventCallback? onEvent})
|
||||
AgUiService({EventCallback? onEvent, IApiClient? apiClient})
|
||||
: onEvent = onEvent ?? ((_) {}),
|
||||
_apiClient = apiClient,
|
||||
_decisionEngine = AiDecisionEngine(),
|
||||
_historyService = MockHistoryService();
|
||||
|
||||
Future<void> sendMessage(String content) async {
|
||||
if (Env.isMockApi) {
|
||||
await _mockEventStream(content);
|
||||
} else {
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
await _mockEventStream(content);
|
||||
}
|
||||
|
||||
Future<void> loadHistory({DateTime? beforeDate}) async {
|
||||
if (Env.isMockApi) {
|
||||
await _mockLoadHistory(beforeDate: beforeDate);
|
||||
if (_apiClient != null) {
|
||||
throw UnimplementedError('Real API not implemented');
|
||||
}
|
||||
await _mockLoadHistory(beforeDate: beforeDate);
|
||||
}
|
||||
|
||||
bool hasEarlierHistory(DateTime fromDate) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import 'package:social_app/core/api/api_client.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'models/user_response.dart';
|
||||
|
||||
class UsersApi {
|
||||
final ApiClient _client;
|
||||
final IApiClient _client;
|
||||
static const _prefix = '/api/v1/users';
|
||||
|
||||
UsersApi(this._client);
|
||||
|
||||
Reference in New Issue
Block a user