diff --git a/flutter/lib/core/api/api_client.dart b/flutter/lib/core/api/api_client.dart index 966a86f9..a295c109 100644 --- a/flutter/lib/core/api/api_client.dart +++ b/flutter/lib/core/api/api_client.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:http/http.dart' as http; import 'api_exception.dart'; @@ -128,4 +129,8 @@ class ApiClient { if (parsedBody is String && parsedBody.trim().isNotEmpty) return parsedBody; return null; } -} +} + +final apiClientProvider = Provider((ref) { + return ApiClient(); +}); diff --git a/flutter/lib/features/profile/data/profile_repository.dart b/flutter/lib/features/profile/data/profile_repository.dart index 35524874..975ebf26 100644 --- a/flutter/lib/features/profile/data/profile_repository.dart +++ b/flutter/lib/features/profile/data/profile_repository.dart @@ -2,25 +2,29 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../core/api/api_client.dart'; import '../../../core/api/guarded_api_call.dart'; import '../../../core/api/api_exception.dart'; +import '../../auth/data/auth_providers.dart'; final profileRepositoryProvider = Provider((ref) { final apiClient = ref.read(apiClientProvider); - return ProfileRepository(apiClient); + return ProfileRepository(apiClient, ref); }); class ProfileRepository { final ApiClient _apiClient; + final Ref _ref; - ProfileRepository(this._apiClient); + ProfileRepository(this._apiClient, this._ref); Future> getProfile() async { return guardedApiCall( + _ref, () => _apiClient.getJson('/api/profile'), ); } Future> updateProfile(Map profileData) async { return guardedApiCall( + _ref, () => _apiClient.patchJson('/api/profile', profileData), ); } diff --git a/flutter/test/features/profile/data/profile_repository_test.dart b/flutter/test/features/profile/data/profile_repository_test.dart index ac7b2753..7d393809 100644 --- a/flutter/test/features/profile/data/profile_repository_test.dart +++ b/flutter/test/features/profile/data/profile_repository_test.dart @@ -28,7 +28,7 @@ void main() { }); test('should throw ApiException when API call fails', () async { - when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException('Failed to fetch profile')); + when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException(message: 'Failed to fetch profile')); expect(() => profileRepository.getProfile(), throwsA(isA())); verify(mockApiClient.getJson('/api/profile')).called(1); @@ -49,7 +49,7 @@ void main() { test('should throw ApiException when API call fails', () async { final profileData = {'username': 'newuser', 'email': 'new@example.com'}; - when(mockApiClient.patchJson('/api/profile', profileData)).thenThrow(ApiException('Failed to update profile')); + when(mockApiClient.patchJson('/api/profile', profileData)).thenThrow(ApiException(message: 'Failed to update profile')); expect(() => profileRepository.updateProfile(profileData), throwsA(isA())); verify(mockApiClient.patchJson('/api/profile', profileData)).called(1);