From 76038e67d83a8f6f63c52df5f074178e0d6d56f1 Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Wed, 21 Feb 2024 11:22:59 +0100 Subject: [PATCH] Retry request on 429 and renaming --- .../model/account/account_preferences.dart | 4 +- lib/src/model/account/account_repository.dart | 8 +-- .../analysis/server_analysis_service.dart | 2 +- lib/src/model/auth/auth_controller.dart | 4 +- lib/src/model/common/http.dart | 52 +++++++++++++------ .../correspondence_service.dart | 2 +- lib/src/model/game/game_controller.dart | 2 +- .../model/game/game_repository_providers.dart | 4 +- lib/src/model/game/game_share_service.dart | 8 +-- lib/src/model/lobby/create_game_service.dart | 4 +- lib/src/model/lobby/lobby_repository.dart | 2 +- lib/src/model/puzzle/puzzle_activity.dart | 2 +- lib/src/model/puzzle/puzzle_controller.dart | 8 +-- lib/src/model/puzzle/puzzle_providers.dart | 18 +++---- lib/src/model/puzzle/storm_controller.dart | 2 +- .../relation_repository_providers.dart | 2 +- lib/src/model/tv/live_tv_channels.dart | 2 +- lib/src/model/tv/tv_controller.dart | 2 +- .../model/user/user_repository_providers.dart | 16 +++--- lib/src/notification_service.dart | 4 +- lib/src/view/account/edit_profile_screen.dart | 2 +- lib/src/view/relation/following_screen.dart | 2 +- lib/src/view/user/recent_games.dart | 2 +- lib/src/view/user/user_activity.dart | 2 +- lib/src/view/watch/watch_tab_screen.dart | 2 +- test/model/auth/auth_controller_test.dart | 6 +-- test/model/puzzle/puzzle_repository_test.dart | 14 ++--- test/model/puzzle/puzzle_service_test.dart | 4 +- test/test_app.dart | 4 +- test/test_container.dart | 4 +- test/view/auth/sign_in_widget_test.dart | 6 +-- test/view/game/archived_game_screen_test.dart | 6 +-- test/view/home/search_screen_test.dart | 6 +-- test/view/puzzle/puzzle_screen_test.dart | 8 +-- test/view/puzzle/storm_screen_test.dart | 12 ++--- test/view/settings/settings_screen_test.dart | 4 +- test/view/user/leaderboard_screen_test.dart | 4 +- test/view/user/leaderboard_widget_test.dart | 4 +- test/view/user/perf_stats_screen_test.dart | 6 +-- test/view/user/user_screen_test.dart | 4 +- 40 files changed, 134 insertions(+), 116 deletions(-) diff --git a/lib/src/model/account/account_preferences.dart b/lib/src/model/account/account_preferences.dart index d9dce72d1e..9dbece200b 100644 --- a/lib/src/model/account/account_preferences.dart +++ b/lib/src/model/account/account_preferences.dart @@ -64,7 +64,7 @@ class AccountPreferences extends _$AccountPreferences { } try { - return ref.withAuthClient( + return ref.withClient( (client) => AccountRepository(client).getPreferences(), ); } catch (e) { @@ -91,7 +91,7 @@ class AccountPreferences extends _$AccountPreferences { Future _setPref(String key, AccountPref value) async { await Future.delayed(const Duration(milliseconds: 200)); - await ref.withAuthClient( + await ref.withClient( (client) => AccountRepository(client).setPreference(key, value), ); ref.invalidateSelf(); diff --git a/lib/src/model/account/account_repository.dart b/lib/src/model/account/account_repository.dart index 3dfb1cf47d..4943e72a46 100644 --- a/lib/src/model/account/account_repository.dart +++ b/lib/src/model/account/account_repository.dart @@ -26,7 +26,7 @@ Future account(AccountRef ref) async { final session = ref.watch(authSessionProvider); if (session == null) return null; - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => AccountRepository(client).getProfile(), const Duration(hours: 1), ); @@ -41,7 +41,7 @@ Future accountUser(AccountUserRef ref) async { Future> accountActivity(AccountActivityRef ref) async { final session = ref.watch(authSessionProvider); if (session == null) return IList(); - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => UserRepository(client).getActivity(session.user.id), const Duration(hours: 1), ); @@ -53,7 +53,7 @@ Future> accountRecentGames( ) async { final session = ref.watch(authSessionProvider); if (session == null) return IList(); - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => GameRepository(client).getRecentGames(session.user.id), const Duration(hours: 1), ); @@ -64,7 +64,7 @@ Future> ongoingGames(OngoingGamesRef ref) async { final session = ref.watch(authSessionProvider); if (session == null) return IList(); - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => AccountRepository(client).getOngoingGames(), const Duration(hours: 1), ); diff --git a/lib/src/model/analysis/server_analysis_service.dart b/lib/src/model/analysis/server_analysis_service.dart index c325d0fa27..87a3286d2f 100644 --- a/lib/src/model/analysis/server_analysis_service.dart +++ b/lib/src/model/analysis/server_analysis_service.dart @@ -74,7 +74,7 @@ class ServerAnalysisService { ); try { - await ref.withAuthClient( + await ref.withClient( (client) => GameRepository(client).requestServerAnalysis(id.gameId), ); } catch (e) { diff --git a/lib/src/model/auth/auth_controller.dart b/lib/src/model/auth/auth_controller.dart index c32bdd0e1e..499e1ef97f 100644 --- a/lib/src/model/auth/auth_controller.dart +++ b/lib/src/model/auth/auth_controller.dart @@ -21,7 +21,7 @@ class AuthController extends _$AuthController { try { final session = await ref - .withAuthClient((client) => AuthRepository(client, appAuth).signIn()); + .withClient((client) => AuthRepository(client, appAuth).signIn()); ref.read(authSessionProvider.notifier).update(session); ref.read(notificationServiceProvider).registerDevice(); @@ -39,7 +39,7 @@ class AuthController extends _$AuthController { final appAuth = ref.read(appAuthProvider); try { - await ref.withAuthClient( + await ref.withClient( (client) => AuthRepository(client, appAuth).signOut(), ); ref.read(notificationServiceProvider).unregister(); diff --git a/lib/src/model/common/http.dart b/lib/src/model/common/http.dart index 91c83f70c2..778c477893 100644 --- a/lib/src/model/common/http.dart +++ b/lib/src/model/common/http.dart @@ -16,6 +16,7 @@ import 'package:http/http.dart' Response, StreamedResponse; import 'package:http/io_client.dart'; +import 'package:http/retry.dart'; import 'package:lichess_mobile/src/model/auth/auth_session.dart'; import 'package:lichess_mobile/src/model/auth/bearer.dart'; import 'package:lichess_mobile/src/model/common/socket.dart'; @@ -60,17 +61,32 @@ Client httpClient(PackageInfo pInfo) { } @Riverpod(keepAlive: true) -AuthClientFactory authClientFactory(AuthClientFactoryRef ref) { - return AuthClientFactory(ref); +LichessClientFactory lichessClientFactory(LichessClientFactoryRef ref) { + return LichessClientFactory(ref); } -class AuthClientFactory { - AuthClientFactory(this._ref); +/// Factory for the default [Client] used to send requests to lichess server. +/// +/// It creates a client that: +/// - Retries just once, after 500ms, on 429 Too Many Requests. +/// - Sets the Authorization header when a token has been stored. +/// - Sets the user-agent header with the app version, build number, and device info. +/// - Logs all requests and responses with status code >= 400. +/// +/// This class should be overridden in tests to provide a mock client. +class LichessClientFactory { + LichessClientFactory(this._ref); - final AuthClientFactoryRef _ref; + final LichessClientFactoryRef _ref; Client call() { - return AuthClient(httpClient(_ref.read(packageInfoProvider)), _ref); + // Retry just once, after 500ms, on 429 Too Many Requests. + // TODO consider throttling the requests, instead of retrying. + return RetryClient( + AuthClient(httpClient(_ref.read(packageInfoProvider)), _ref), + retries: 1, + when: (response) => response.statusCode == 429, + ); } } @@ -95,7 +111,7 @@ String userAgent(UserAgentRef ref) { class AuthClient extends BaseClient { AuthClient(this._inner, this._ref); - final AuthClientFactoryRef _ref; + final LichessClientFactoryRef _ref; final Client _inner; @override @@ -338,7 +354,7 @@ class _ReuseClientService { /// Returns the client and a unique key to be used to close it later. /// /// If the client is null, it creates a new one. - (Client, UniqueKey) get(AuthClientFactory factory) { + (Client, UniqueKey) get(LichessClientFactory factory) { if (_client == null) { _logger.info('Creating a new client.'); } @@ -370,14 +386,14 @@ class _ReuseClientService { } extension ClientWidgetRefExtension on WidgetRef { - /// Runs [fn] with an [AuthClient]. + /// Runs [fn] with a [Client] configured to send requests to lichess server. /// /// It handles the creation and closing of the client, while trying to reuse /// the same client for multiple requests at the same time. /// Will try to close the client after [fn] completes. - Future withAuthClient(Future Function(Client) fn) async { + Future withClient(Future Function(Client) fn) async { final (client, key) = - _ReuseClientService.instance.get(read(authClientFactoryProvider)); + _ReuseClientService.instance.get(read(lichessClientFactoryProvider)); try { return await fn(client); } finally { @@ -387,15 +403,15 @@ extension ClientWidgetRefExtension on WidgetRef { } extension ClientRefExtension on Ref { - /// Runs [fn] with an [AuthClient]. + /// Runs [fn] with an [Client] configured to send requests to lichess server. /// /// It handles the creation and closing of the client, while trying to reuse /// the same client for multiple requests at the same time. /// Will try to close the client after [fn] completes, or when the provider is /// disposed. - Future withAuthClient(Future Function(Client) fn) async { + Future withClient(Future Function(Client) fn) async { final (client, key) = - _ReuseClientService.instance.get(read(authClientFactoryProvider)); + _ReuseClientService.instance.get(read(lichessClientFactoryProvider)); onDispose(() => _ReuseClientService.instance.close(key)); try { return await fn(client); @@ -406,7 +422,9 @@ extension ClientRefExtension on Ref { } extension ClientAutoDisposeRefExtension on AutoDisposeRef { - /// Runs [fn] with an [AuthClient] and keeps the provider alive for [duration]. + /// Runs [fn] with an [Client] configured to send requests to lichess server, + /// and keeps the provider alive for [duration]. + /// /// This is primarily used for caching network requests in a [FutureProvider]. /// /// It handles the creation and closing of the client, while trying to reuse @@ -416,14 +434,14 @@ extension ClientAutoDisposeRefExtension on AutoDisposeRef { /// /// If [fn] throws with a [SocketException], the provider is not kept alive, this /// allows to retry the request later. - Future withAuthClientCacheFor( + Future withClientCacheFor( Future Function(Client) fn, Duration duration, ) async { final link = keepAlive(); final timer = Timer(duration, link.close); final (client, key) = - _ReuseClientService.instance.get(read(authClientFactoryProvider)); + _ReuseClientService.instance.get(read(lichessClientFactoryProvider)); onDispose(() { _ReuseClientService.instance.close(key); timer.cancel(); diff --git a/lib/src/model/correspondence/correspondence_service.dart b/lib/src/model/correspondence/correspondence_service.dart index eb99e063e4..93eeb65b7c 100644 --- a/lib/src/model/correspondence/correspondence_service.dart +++ b/lib/src/model/correspondence/correspondence_service.dart @@ -48,7 +48,7 @@ class CorrespondenceService { final storedOngoingGames = await _storage.fetchOngoingGames(_session?.user.id); - ref.withAuthClient((client) async { + ref.withClient((client) async { try { final accountRepository = AccountRepository(client); final gameRepository = GameRepository(client); diff --git a/lib/src/model/game/game_controller.dart b/lib/src/model/game/game_controller.dart index 43460acaf7..b3b3d08036 100644 --- a/lib/src/model/game/game_controller.dart +++ b/lib/src/model/game/game_controller.dart @@ -839,7 +839,7 @@ class GameController extends _$GameController { FutureResult _getPostGameData() { return Result.capture( - ref.withAuthClient( + ref.withClient( (client) => GameRepository(client).getGame(gameFullId.gameId), ), ); diff --git a/lib/src/model/game/game_repository_providers.dart b/lib/src/model/game/game_repository_providers.dart index a854911ce6..9cd51fc000 100644 --- a/lib/src/model/game/game_repository_providers.dart +++ b/lib/src/model/game/game_repository_providers.dart @@ -10,7 +10,7 @@ part 'game_repository_providers.g.dart'; @riverpod Future archivedGame(ArchivedGameRef ref, {required GameId id}) { - return ref.withAuthClient( + return ref.withClient( (client) => GameRepository(client).getGame(id), ); } @@ -20,7 +20,7 @@ Future> gamesById( GamesByIdRef ref, { required ISet ids, }) { - return ref.withAuthClient( + return ref.withClient( (client) => GameRepository(client).getGamesByIds(ids), ); } diff --git a/lib/src/model/game/game_share_service.dart b/lib/src/model/game/game_share_service.dart index a8939f4a76..0c8de18b17 100644 --- a/lib/src/model/game/game_share_service.dart +++ b/lib/src/model/game/game_share_service.dart @@ -22,7 +22,7 @@ class GameShareService { /// Fetches the raw PGN of a game and launches the share dialog. Future rawPgn(GameId id) async { - final resp = await _ref.withAuthClient( + final resp = await _ref.withClient( (client) => client .get( Uri.parse( @@ -39,7 +39,7 @@ class GameShareService { /// Fetches the annotated PGN of a game and launches the share dialog. Future annotatedPgn(GameId id) async { - final resp = await _ref.withAuthClient( + final resp = await _ref.withClient( (client) => client .get( Uri.parse( @@ -63,7 +63,7 @@ class GameShareService { ) async { final boardTheme = _ref.read(boardPreferencesProvider).boardTheme; final pieceTheme = _ref.read(boardPreferencesProvider).pieceSet; - final resp = await _ref.withAuthClient( + final resp = await _ref.withClient( (client) => client .get( Uri.parse( @@ -84,7 +84,7 @@ class GameShareService { Future gameGif(GameId id, Side orientation) async { final boardTheme = _ref.read(boardPreferencesProvider).boardTheme; final pieceTheme = _ref.read(boardPreferencesProvider).pieceSet; - final resp = await _ref.withAuthClient( + final resp = await _ref.withClient( (client) => client .get( Uri.parse( diff --git a/lib/src/model/lobby/create_game_service.dart b/lib/src/model/lobby/create_game_service.dart index 05774011ca..fdc9203828 100644 --- a/lib/src/model/lobby/create_game_service.dart +++ b/lib/src/model/lobby/create_game_service.dart @@ -38,7 +38,7 @@ class CreateGameService { final completer = Completer()..future.whenComplete(_close); _pendingGameConnection = ( - ref.read(authClientFactoryProvider)(), + ref.read(lichessClientFactoryProvider)(), stream.listen((event) { if (event.topic == 'redirect') { final data = event.data as Map; @@ -81,7 +81,7 @@ class CreateGameService { Future newCorrespondenceGame(GameSeek seek) async { _log.info('Creating new correspondence game'); - await ref.withAuthClient( + await ref.withClient( (client) => LobbyRepository(client).createSeek( seek, sri: ref.read(socketClientProvider).sri, diff --git a/lib/src/model/lobby/lobby_repository.dart b/lib/src/model/lobby/lobby_repository.dart index 63de4f8eb6..388e2162f6 100644 --- a/lib/src/model/lobby/lobby_repository.dart +++ b/lib/src/model/lobby/lobby_repository.dart @@ -17,7 +17,7 @@ part 'lobby_repository.g.dart'; Future> correspondenceChallenges( CorrespondenceChallengesRef ref, ) { - final client = ref.read(authClientFactoryProvider)(); + final client = ref.read(lichessClientFactoryProvider)(); ref.onDispose(client.close); final lobbyRepository = LobbyRepository(client); diff --git a/lib/src/model/puzzle/puzzle_activity.dart b/lib/src/model/puzzle/puzzle_activity.dart index 249bdac055..dad0eb9473 100644 --- a/lib/src/model/puzzle/puzzle_activity.dart +++ b/lib/src/model/puzzle/puzzle_activity.dart @@ -68,7 +68,7 @@ class PuzzleActivity extends _$PuzzleActivity { if (_list.length < _maxPuzzles) { state = AsyncData(currentVal.copyWith(isLoading: true)); Result.capture( - ref.withAuthClient( + ref.withClient( (client) => PuzzleRepository(client) .puzzleActivity(_nbPerPage, before: _list.last.date), ), diff --git a/lib/src/model/puzzle/puzzle_controller.dart b/lib/src/model/puzzle/puzzle_controller.dart index a69eb65f6a..05a46e1b5b 100644 --- a/lib/src/model/puzzle/puzzle_controller.dart +++ b/lib/src/model/puzzle/puzzle_controller.dart @@ -215,7 +215,7 @@ class PuzzleController extends _$PuzzleController { .setDifficulty(difficulty); // ignore: avoid_manual_providers_as_generated_provider_dependency - final nextPuzzle = await ref.withAuthClient( + final nextPuzzle = await ref.withClient( (client) => _service(client).resetBatch( userId: initialContext.userId, angle: initialContext.angle, @@ -239,7 +239,7 @@ class PuzzleController extends _$PuzzleController { if (initialContext.userId != null) { final streak = state.streak?.index; if (streak != null && streak > 0) { - ref.withAuthClient( + ref.withClient( (client) => _repository(client).postStreakRun(streak), ); } @@ -285,7 +285,7 @@ class PuzzleController extends _$PuzzleController { FutureResult _fetchNextStreakPuzzle(PuzzleStreak streak) { return streak.nextId != null ? Result.capture( - ref.withAuthClient( + ref.withClient( (client) => _repository(client).fetch(streak.nextId!).then( (puzzle) => PuzzleContext( angle: const PuzzleTheme(PuzzleThemeKey.mix), @@ -332,7 +332,7 @@ class PuzzleController extends _$PuzzleController { final soundService = ref.read(soundServiceProvider); if (state.streak == null) { - final next = await ref.withAuthClient( + final next = await ref.withClient( (client) => _service(client).solve( userId: initialContext.userId, angle: initialContext.angle, diff --git a/lib/src/model/puzzle/puzzle_providers.dart b/lib/src/model/puzzle/puzzle_providers.dart index c58cff244f..6bef9867de 100644 --- a/lib/src/model/puzzle/puzzle_providers.dart +++ b/lib/src/model/puzzle/puzzle_providers.dart @@ -24,7 +24,7 @@ Future nextPuzzle( ) { final session = ref.watch(authSessionProvider); - return ref.withAuthClient((client) { + return ref.withClient((client) { final puzzleService = ref.read(puzzleServiceFactoryProvider)( client, queueLength: kPuzzleLocalQueueLength, @@ -38,12 +38,12 @@ Future nextPuzzle( @riverpod Future streak(StreakRef ref) { - return ref.withAuthClient((client) => PuzzleRepository(client).streak()); + return ref.withClient((client) => PuzzleRepository(client).streak()); } @riverpod Future storm(StormRef ref) { - return ref.withAuthClient((client) => PuzzleRepository(client).storm()); + return ref.withClient((client) => PuzzleRepository(client).storm()); } @riverpod @@ -51,12 +51,12 @@ Future puzzle(PuzzleRef ref, PuzzleId id) async { final puzzleStorage = ref.watch(puzzleStorageProvider); final puzzle = await puzzleStorage.fetch(puzzleId: id); if (puzzle != null) return puzzle; - return ref.withAuthClient((client) => PuzzleRepository(client).fetch(id)); + return ref.withClient((client) => PuzzleRepository(client).fetch(id)); } @riverpod Future dailyPuzzle(DailyPuzzleRef ref) { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => PuzzleRepository(client).daily(), const Duration(hours: 6), ); @@ -84,7 +84,7 @@ Future<(PuzzleDashboard, IList)?> puzzleDashboardActivity( ) async { final session = ref.watch(authSessionProvider); if (session == null) return null; - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) { final repo = PuzzleRepository(client); return Future.wait([ @@ -105,7 +105,7 @@ Future<(PuzzleDashboard, IList)?> puzzleDashboardActivity( Future stormDashboard(StormDashboardRef ref) async { final session = ref.watch(authSessionProvider); if (session == null) return null; - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => PuzzleRepository(client).stormDashboard(session.user.id), const Duration(minutes: 30), ); @@ -115,7 +115,7 @@ Future stormDashboard(StormDashboardRef ref) async { Future> puzzleThemes( PuzzleThemesRef ref, ) { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => PuzzleRepository(client).puzzleThemes(), const Duration(days: 1), ); @@ -123,7 +123,7 @@ Future> puzzleThemes( @riverpod Future> puzzleOpenings(PuzzleOpeningsRef ref) { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => PuzzleRepository(client).puzzleOpenings(), const Duration(days: 1), ); diff --git a/lib/src/model/puzzle/storm_controller.dart b/lib/src/model/puzzle/storm_controller.dart index 84e1d19021..776572d47b 100644 --- a/lib/src/model/puzzle/storm_controller.dart +++ b/lib/src/model/puzzle/storm_controller.dart @@ -134,7 +134,7 @@ class StormController extends _$StormController { final session = ref.read(authSessionProvider); if (session != null) { - final res = await ref.withAuthClient( + final res = await ref.withClient( (client) => Result.capture( PuzzleRepository(client) .postStormRun(stats) diff --git a/lib/src/model/relation/relation_repository_providers.dart b/lib/src/model/relation/relation_repository_providers.dart index 42e0e12f52..9357213429 100644 --- a/lib/src/model/relation/relation_repository_providers.dart +++ b/lib/src/model/relation/relation_repository_providers.dart @@ -9,7 +9,7 @@ part 'relation_repository_providers.g.dart'; @riverpod Future> following(FollowingRef ref) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => RelationRepository(client).getFollowing(), const Duration(hours: 1), ); diff --git a/lib/src/model/tv/live_tv_channels.dart b/lib/src/model/tv/live_tv_channels.dart index 61464340ff..ec1dc2fe1a 100644 --- a/lib/src/model/tv/live_tv_channels.dart +++ b/lib/src/model/tv/live_tv_channels.dart @@ -51,7 +51,7 @@ class LiveTvChannels extends _$LiveTvChannels { _socketReadySubscription?.cancel(); final repoGames = - await ref.withAuthClient((client) => TvRepository(client).channels()); + await ref.withClient((client) => TvRepository(client).channels()); final (stream, readyStream) = _socket.connect(Uri(path: kDefaultSocketRoute)); diff --git a/lib/src/model/tv/tv_controller.dart b/lib/src/model/tv/tv_controller.dart index e597d7985f..e919fc6d37 100644 --- a/lib/src/model/tv/tv_controller.dart +++ b/lib/src/model/tv/tv_controller.dart @@ -63,7 +63,7 @@ class TvController extends _$TvController { orientation = game.$2; } else { final channels = - await ref.withAuthClient((client) => TvRepository(client).channels()); + await ref.withClient((client) => TvRepository(client).channels()); final channelGame = channels[channel]!; id = channelGame.id; orientation = channelGame.side ?? Side.white; diff --git a/lib/src/model/user/user_repository_providers.dart b/lib/src/model/user/user_repository_providers.dart index 5cbec63f61..e8246a3b4e 100644 --- a/lib/src/model/user/user_repository_providers.dart +++ b/lib/src/model/user/user_repository_providers.dart @@ -15,7 +15,7 @@ const _kAutoCompleteDebounceTimer = Duration(milliseconds: 300); @riverpod Future user(UserRef ref, {required UserId id}) async { - return ref.withAuthClient( + return ref.withClient( (client) => UserRepository(client).getUser(id), ); } @@ -25,7 +25,7 @@ Future<(User, UserStatus)> userAndStatus( UserAndStatusRef ref, { required UserId id, }) async { - return ref.withAuthClient( + return ref.withClient( (client) async { final repo = UserRepository(client); return Future.wait( @@ -47,7 +47,7 @@ Future userPerfStats( required UserId id, required Perf perf, }) async { - return ref.withAuthClient( + return ref.withClient( (client) => UserRepository(client).getPerfStats(id, perf), ); } @@ -57,14 +57,14 @@ Future> userStatuses( UserStatusesRef ref, { required ISet ids, }) async { - return ref.withAuthClient( + return ref.withClient( (client) => UserRepository(client).getUsersStatuses(ids), ); } @riverpod Future> liveStreamers(LiveStreamersRef ref) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => UserRepository(client).getLiveStreamers(), const Duration(minutes: 1), ); @@ -72,7 +72,7 @@ Future> liveStreamers(LiveStreamersRef ref) async { @riverpod Future> top1(Top1Ref ref) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => UserRepository(client).getTop1(), const Duration(hours: 12), ); @@ -80,7 +80,7 @@ Future> top1(Top1Ref ref) async { @riverpod Future leaderboard(LeaderboardRef ref) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => UserRepository(client).getLeaderboard(), const Duration(hours: 2), ); @@ -99,7 +99,7 @@ Future> autoCompleteUser( throw Exception('Cancelled'); } - return ref.withAuthClient( + return ref.withClient( (client) => UserRepository(client).autocompleteUser(term), ); } diff --git a/lib/src/notification_service.dart b/lib/src/notification_service.dart index b384294c0a..688ec316fe 100644 --- a/lib/src/notification_service.dart +++ b/lib/src/notification_service.dart @@ -48,7 +48,7 @@ class NotificationService { return; } try { - await ref.withAuthClient( + await ref.withClient( (client) => client .post(Uri.parse('$kLichessHost/mobile/register/firebase/$token')), ); @@ -64,7 +64,7 @@ class NotificationService { return; } try { - await ref.withAuthClient( + await ref.withClient( (client) => client.post(Uri.parse('$kLichessHost/mobile/unregister')), ); } catch (e, st) { diff --git a/lib/src/view/account/edit_profile_screen.dart b/lib/src/view/account/edit_profile_screen.dart index c932f3646e..3dc91f388d 100644 --- a/lib/src/view/account/edit_profile_screen.dart +++ b/lib/src/view/account/edit_profile_screen.dart @@ -270,7 +270,7 @@ class _EditProfileFormState extends ConsumerState<_EditProfileForm> { return value == null; }); final future = Result.capture( - ref.withAuthClient( + ref.withClient( (client) => AccountRepository(client).saveProfile( _formData.map( diff --git a/lib/src/view/relation/following_screen.dart b/lib/src/view/relation/following_screen.dart index 75f5bba827..bf36a1d3fd 100644 --- a/lib/src/view/relation/following_screen.dart +++ b/lib/src/view/relation/following_screen.dart @@ -99,7 +99,7 @@ class _Body extends ConsumerWidget { ); }); try { - await ref.withAuthClient( + await ref.withClient( (client) => RelationRepository(client) .unfollow(user.username), ); diff --git a/lib/src/view/user/recent_games.dart b/lib/src/view/user/recent_games.dart index aec2ff9fed..6cc65d9c94 100644 --- a/lib/src/view/user/recent_games.dart +++ b/lib/src/view/user/recent_games.dart @@ -31,7 +31,7 @@ Future> _userRecentGames( _UserRecentGamesRef ref, { required UserId userId, }) { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => GameRepository(client).getRecentGames(userId), // cache is important because the associated widget is in a [ListView] and // the provider may be instanciated multiple times in a short period of time diff --git a/lib/src/view/user/user_activity.dart b/lib/src/view/user/user_activity.dart index 42af288d52..f1cba999d0 100644 --- a/lib/src/view/user/user_activity.dart +++ b/lib/src/view/user/user_activity.dart @@ -26,7 +26,7 @@ Future> _userActivity( _UserActivityRef ref, { required UserId id, }) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) => UserRepository(client).getActivity(id), // cache is important because the associated widget is in a [ListView] and // the provider may be instanciated multiple times in a short period of time diff --git a/lib/src/view/watch/watch_tab_screen.dart b/lib/src/view/watch/watch_tab_screen.dart index 80eeaf9e48..87a811baaa 100644 --- a/lib/src/view/watch/watch_tab_screen.dart +++ b/lib/src/view/watch/watch_tab_screen.dart @@ -34,7 +34,7 @@ const _featuredChannelsSet = ISetConst({ @riverpod Future> featuredChannels(FeaturedChannelsRef ref) async { - return ref.withAuthClientCacheFor( + return ref.withClientCacheFor( (client) async { final channels = await TvRepository(client).channels(); return channels.entries diff --git a/test/model/auth/auth_controller_test.dart b/test/model/auth/auth_controller_test.dart index efa2ff8b5b..982d26b1c0 100644 --- a/test/model/auth/auth_controller_test.dart +++ b/test/model/auth/auth_controller_test.dart @@ -23,7 +23,7 @@ class Listener extends Mock { void call(T? previous, T value); } -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -86,7 +86,7 @@ void main() { overrides: [ appAuthProvider.overrideWithValue(mockFlutterAppAuth), sessionStorageProvider.overrideWithValue(mockSessionStorage), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -128,7 +128,7 @@ void main() { overrides: [ appAuthProvider.overrideWithValue(mockFlutterAppAuth), sessionStorageProvider.overrideWithValue(mockSessionStorage), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/model/puzzle/puzzle_repository_test.dart b/test/model/puzzle/puzzle_repository_test.dart index be1832aae6..c2f161040c 100644 --- a/test/model/puzzle/puzzle_repository_test.dart +++ b/test/model/puzzle/puzzle_repository_test.dart @@ -9,7 +9,7 @@ import 'package:lichess_mobile/src/model/puzzle/puzzle_repository.dart'; import '../../test_container.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { FakeClientFactory(this._client); final http.Client _client; @@ -24,7 +24,7 @@ void main() { Future makeTestContainer(MockClient mockClient) async { return makeContainer( overrides: [ - authClientFactoryProvider.overrideWith((ref) { + lichessClientFactoryProvider.overrideWith((ref) { return FakeClientFactory(mockClient); }), ], @@ -46,7 +46,7 @@ void main() { }); final container = await makeTestContainer(mockClient); - final client = container.read(authClientFactoryProvider)(); + final client = container.read(lichessClientFactoryProvider)(); final repo = PuzzleRepository(client); final response = await repo.selectBatch(nb: 3); @@ -69,7 +69,7 @@ void main() { }); final container = await makeTestContainer(mockClient); - final client = container.read(authClientFactoryProvider)(); + final client = container.read(lichessClientFactoryProvider)(); final repo = PuzzleRepository(client); final response = await repo.selectBatch(nb: 1); @@ -92,7 +92,7 @@ void main() { return mockResponse('', 404); }); final container = await makeTestContainer(mockClient); - final client = container.read(authClientFactoryProvider)(); + final client = container.read(lichessClientFactoryProvider)(); final repo = PuzzleRepository(client); final result = await repo.selectBatch(nb: 1); @@ -116,7 +116,7 @@ void main() { }); final container = await makeTestContainer(mockClient); - final client = container.read(authClientFactoryProvider)(); + final client = container.read(lichessClientFactoryProvider)(); final repo = PuzzleRepository(client); final result = await repo.streak(); @@ -137,7 +137,7 @@ void main() { }); final container = await makeTestContainer(mockClient); - final client = container.read(authClientFactoryProvider)(); + final client = container.read(lichessClientFactoryProvider)(); final repo = PuzzleRepository(client); final result = await repo.puzzleDashboard(); diff --git a/test/model/puzzle/puzzle_service_test.dart b/test/model/puzzle/puzzle_service_test.dart index 8c7f3fb40e..be2d63c7bb 100644 --- a/test/model/puzzle/puzzle_service_test.dart +++ b/test/model/puzzle/puzzle_service_test.dart @@ -20,7 +20,7 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import '../../test_container.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { FakeClientFactory(this._client); final http.Client _client; @@ -44,7 +44,7 @@ void main() { ref.onDispose(db.close); return db; }), - authClientFactoryProvider.overrideWith((ref) { + lichessClientFactoryProvider.overrideWith((ref) { return FakeClientFactory(mockClient); }), ], diff --git a/test/test_app.dart b/test/test_app.dart index 580aee679b..46af655cf0 100644 --- a/test/test_app.dart +++ b/test/test_app.dart @@ -36,7 +36,7 @@ class MockSoundPool extends Mock implements Soundpool {} class MockDatabase extends Mock implements Database {} -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) async { @@ -83,7 +83,7 @@ Future buildTestApp( return ProviderScope( overrides: [ // ignore: scoped_providers_should_specify_dependencies - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), // ignore: scoped_providers_should_specify_dependencies showRatingsPrefProvider.overrideWith((ref) { return true; diff --git a/test/test_container.dart b/test/test_container.dart index 7612e0a805..ce5fb40313 100644 --- a/test/test_container.dart +++ b/test/test_container.dart @@ -35,7 +35,7 @@ class MockHttpClient extends Mock implements http.Client {} const shouldLog = false; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockHttpClient(); @@ -64,7 +64,7 @@ Future makeContainer({ final container = ProviderContainer( overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), crashlyticsProvider.overrideWithValue(FakeCrashlytics()), notificationServiceProvider.overrideWithValue(FakeNotificationService()), soundServiceProvider.overrideWithValue(FakeSoundService()), diff --git a/test/view/auth/sign_in_widget_test.dart b/test/view/auth/sign_in_widget_test.dart index c8081ebe99..414a0dc353 100644 --- a/test/view/auth/sign_in_widget_test.dart +++ b/test/view/auth/sign_in_widget_test.dart @@ -14,7 +14,7 @@ import '../../test_utils.dart'; class MockFlutterAppAuth extends Mock implements FlutterAppAuth {} -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -58,7 +58,7 @@ void main() { ), ), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -87,7 +87,7 @@ void main() { ), overrides: [ appAuthProvider.overrideWithValue(mockFlutterAppAuth), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/game/archived_game_screen_test.dart b/test/view/game/archived_game_screen_test.dart index 9b82cdb3de..d6ad99c264 100644 --- a/test/view/game/archived_game_screen_test.dart +++ b/test/view/game/archived_game_screen_test.dart @@ -21,7 +21,7 @@ import 'package:lichess_mobile/src/widgets/bottom_bar_button.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -45,7 +45,7 @@ void main() { orientation: Side.white, ), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -117,7 +117,7 @@ void main() { orientation: Side.white, ), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/home/search_screen_test.dart b/test/view/home/search_screen_test.dart index 463e168ce7..c134fd0e35 100644 --- a/test/view/home/search_screen_test.dart +++ b/test/view/home/search_screen_test.dart @@ -11,7 +11,7 @@ import 'package:lichess_mobile/src/widgets/user_list_tile.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -35,7 +35,7 @@ void main() { tester, home: const SearchScreen(), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -75,7 +75,7 @@ void main() { tester, home: const SearchScreen(), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/puzzle/puzzle_screen_test.dart b/test/view/puzzle/puzzle_screen_test.dart index 406e80bfc3..da11e8f9d5 100644 --- a/test/view/puzzle/puzzle_screen_test.dart +++ b/test/view/puzzle/puzzle_screen_test.dart @@ -27,7 +27,7 @@ class MockPuzzleBatchStorage extends Mock implements PuzzleBatchStorage {} class MockPuzzleStorage extends Mock implements PuzzleStorage {} -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { FakeClientFactory(this._client); final http.Client _client; @@ -168,7 +168,7 @@ void main() { ), ), overrides: [ - authClientFactoryProvider.overrideWith((ref) { + lichessClientFactoryProvider.overrideWith((ref) { return FakeClientFactory(mockClient); }), puzzleBatchStorageProvider.overrideWith((ref) { @@ -280,7 +280,7 @@ void main() { ), ), overrides: [ - authClientFactoryProvider.overrideWith((ref) { + lichessClientFactoryProvider.overrideWith((ref) { return FakeClientFactory(mockClient); }), puzzleBatchStorageProvider.overrideWith((ref) { @@ -381,7 +381,7 @@ void main() { ), ), overrides: [ - authClientFactoryProvider.overrideWith((ref) { + lichessClientFactoryProvider.overrideWith((ref) { return FakeClientFactory(mockClient); }), puzzleBatchStorageProvider.overrideWith((ref) { diff --git a/test/view/puzzle/storm_screen_test.dart b/test/view/puzzle/storm_screen_test.dart index c41994c317..857bcdd605 100644 --- a/test/view/puzzle/storm_screen_test.dart +++ b/test/view/puzzle/storm_screen_test.dart @@ -14,7 +14,7 @@ import 'package:lichess_mobile/src/view/puzzle/storm_screen.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -38,7 +38,7 @@ void main() { home: const StormScreen(), overrides: [ stormProvider.overrideWith((ref) => mockStromRun), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -58,7 +58,7 @@ void main() { home: const StormScreen(), overrides: [ stormProvider.overrideWith((ref) => mockStromRun), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -81,7 +81,7 @@ void main() { home: const StormScreen(), overrides: [ stormProvider.overrideWith((ref) => mockStromRun), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -135,7 +135,7 @@ void main() { home: const StormScreen(), overrides: [ stormProvider.overrideWith((ref) => mockStromRun), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -179,7 +179,7 @@ void main() { home: const StormScreen(), overrides: [ stormProvider.overrideWith((ref) => mockStromRun), - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/settings/settings_screen_test.dart b/test/view/settings/settings_screen_test.dart index 5bac2ec2ed..784e90d7d7 100644 --- a/test/view/settings/settings_screen_test.dart +++ b/test/view/settings/settings_screen_test.dart @@ -12,7 +12,7 @@ import '../../model/auth/fake_session_storage.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -71,7 +71,7 @@ void main() { home: const SettingsScreen(), userSession: fakeSession, overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/user/leaderboard_screen_test.dart b/test/view/user/leaderboard_screen_test.dart index aa58fec219..3274c4a1df 100644 --- a/test/view/user/leaderboard_screen_test.dart +++ b/test/view/user/leaderboard_screen_test.dart @@ -8,7 +8,7 @@ import 'package:lichess_mobile/src/view/user/leaderboard_screen.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -30,7 +30,7 @@ void main() { final app = await buildTestApp( tester, overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], home: const LeaderboardScreen(), ); diff --git a/test/view/user/leaderboard_widget_test.dart b/test/view/user/leaderboard_widget_test.dart index 2814a546fc..4f90d56aaa 100644 --- a/test/view/user/leaderboard_widget_test.dart +++ b/test/view/user/leaderboard_widget_test.dart @@ -9,7 +9,7 @@ import 'package:lichess_mobile/src/view/user/leaderboard_widget.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -31,7 +31,7 @@ void main() { tester, home: Column(children: [LeaderboardWidget()]), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/user/perf_stats_screen_test.dart b/test/view/user/perf_stats_screen_test.dart index 1b04fc2ffe..af7aa38ec2 100644 --- a/test/view/user/perf_stats_screen_test.dart +++ b/test/view/user/perf_stats_screen_test.dart @@ -11,7 +11,7 @@ import '../../model/auth/fake_auth_repository.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -38,7 +38,7 @@ void main() { perf: testPerf, ), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); @@ -68,7 +68,7 @@ void main() { perf: testPerf, ), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], ); diff --git a/test/view/user/user_screen_test.dart b/test/view/user/user_screen_test.dart index 480628d6f1..55c606b7da 100644 --- a/test/view/user/user_screen_test.dart +++ b/test/view/user/user_screen_test.dart @@ -10,7 +10,7 @@ import '../../model/user/user_repository_test.dart'; import '../../test_app.dart'; import '../../test_utils.dart'; -class FakeClientFactory implements AuthClientFactory { +class FakeClientFactory implements LichessClientFactory { @override http.Client call() { return MockClient((request) { @@ -48,7 +48,7 @@ void main() { tester, home: const UserScreen(user: testUser), overrides: [ - authClientFactoryProvider.overrideWithValue(FakeClientFactory()), + lichessClientFactoryProvider.overrideWithValue(FakeClientFactory()), ], );