diff --git a/packages/stream_chat/lib/src/core/http/interceptor/auth_interceptor.dart b/packages/stream_chat/lib/src/core/http/interceptor/auth_interceptor.dart index 7748f96fc..759e8e791 100644 --- a/packages/stream_chat/lib/src/core/http/interceptor/auth_interceptor.dart +++ b/packages/stream_chat/lib/src/core/http/interceptor/auth_interceptor.dart @@ -8,7 +8,7 @@ import 'package:stream_chat/src/core/http/token_manager.dart'; /// Authentication interceptor that refreshes the token if /// an auth error is received -class AuthInterceptor extends Interceptor { +class AuthInterceptor extends QueuedInterceptor { /// Initialize a new auth interceptor AuthInterceptor(this._client, this._tokenManager); @@ -57,35 +57,10 @@ class AuthInterceptor extends Interceptor { final error = ErrorResponse.fromJson(data); if (error.code == ChatErrorCode.tokenExpired.code) { if (_tokenManager.isStatic) return handler.next(err); - _client.lock(); await _tokenManager.loadToken(refresh: true); - _client.unlock(); try { final options = err.requestOptions; - final response = await _client.request( - options.path, - cancelToken: options.cancelToken, - data: options.data, - onReceiveProgress: options.onReceiveProgress, - onSendProgress: options.onSendProgress, - queryParameters: options.queryParameters, - options: Options( - method: options.method, - sendTimeout: options.sendTimeout, - receiveTimeout: options.receiveTimeout, - extra: options.extra, - headers: options.headers, - responseType: options.responseType, - contentType: options.contentType, - validateStatus: options.validateStatus, - receiveDataWhenStatusError: options.receiveDataWhenStatusError, - followRedirects: options.followRedirects, - maxRedirects: options.maxRedirects, - requestEncoder: options.requestEncoder, - responseDecoder: options.responseDecoder, - listFormat: options.listFormat, - ), - ); + final response = await _client.fetch(options); return handler.resolve(response); } on DioError catch (error) { return handler.next(error); diff --git a/packages/stream_chat/lib/src/core/http/stream_http_client.dart b/packages/stream_chat/lib/src/core/http/stream_http_client.dart index d977b0106..7052ce384 100644 --- a/packages/stream_chat/lib/src/core/http/stream_http_client.dart +++ b/packages/stream_chat/lib/src/core/http/stream_http_client.dart @@ -76,20 +76,6 @@ class StreamHttpClient { @visibleForTesting final Dio httpClient; - /// Lock the current [StreamHttpClient] instance. - /// - /// [StreamHttpClient] will enqueue the incoming request tasks instead - /// send them directly when [interceptor.requestOptions] is locked. - void lock() => httpClient.lock(); - - /// Unlock the current [StreamHttpClient] instance. - /// - /// [StreamHttpClient] instance dequeue the request task。 - void unlock() => httpClient.unlock(); - - /// Clear the current [StreamHttpClient] instance waiting queue. - void clear() => httpClient.clear(); - /// Shuts down the [StreamHttpClient]. /// /// If [force] is `false` the [StreamHttpClient] will be kept alive @@ -280,4 +266,17 @@ class StreamHttpClient { throw _parseError(error); } } + + /// Handy method to make http requests from [RequestOptions] + /// with error parsing. + Future> fetch( + RequestOptions requestOptions, + ) async { + try { + final response = await httpClient.fetch(requestOptions); + return response; + } on DioError catch (error) { + throw _parseError(error); + } + } } diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index 65c0fe264..5317e6c34 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -2513,7 +2513,7 @@ void main() { }); test( - 'setting the `currentUser` should also compute and update the unreadCounts', + '''setting the `currentUser` should also compute and update the unreadCounts''', () { final state = client.state; final initialUser = OwnUser.fromUser(user); diff --git a/packages/stream_chat/test/src/core/http/interceptor/auth_interceptor_test.dart b/packages/stream_chat/test/src/core/http/interceptor/auth_interceptor_test.dart index 1e88ea1ea..7bd81ff0e 100644 --- a/packages/stream_chat/test/src/core/http/interceptor/auth_interceptor_test.dart +++ b/packages/stream_chat/test/src/core/http/interceptor/auth_interceptor_test.dart @@ -93,23 +93,11 @@ void main() { when(() => tokenManager.isStatic).thenReturn(false); - when(() => client.lock()).thenReturn(() {}); - final token = Token.development('test-user-id'); when(() => tokenManager.loadToken(refresh: true)) .thenAnswer((_) async => token); - when(() => client.unlock()).thenReturn(() {}); - - when(() => client.request( - path, - data: options.data, - onReceiveProgress: options.onReceiveProgress, - onSendProgress: options.onSendProgress, - queryParameters: options.queryParameters, - cancelToken: options.cancelToken, - options: any(named: 'options'), - )).thenAnswer((_) async => Response( + when(() => client.fetch(options)).thenAnswer((_) async => Response( requestOptions: options, statusCode: 200, )); @@ -127,21 +115,10 @@ void main() { verify(() => tokenManager.isStatic).called(1); - verify(() => client.lock()).called(1); - verify(() => tokenManager.loadToken(refresh: true)).called(1); verifyNoMoreInteractions(tokenManager); - verify(() => client.unlock()).called(1); - verify(() => client.request( - path, - data: options.data, - onReceiveProgress: options.onReceiveProgress, - onSendProgress: options.onSendProgress, - queryParameters: options.queryParameters, - cancelToken: options.cancelToken, - options: any(named: 'options'), - )).called(1); + verify(() => client.fetch(options)).called(1); verifyNoMoreInteractions(client); }); @@ -163,23 +140,11 @@ void main() { when(() => tokenManager.isStatic).thenReturn(false); - when(() => client.lock()).thenReturn(() {}); - final token = Token.development('test-user-id'); when(() => tokenManager.loadToken(refresh: true)) .thenAnswer((_) async => token); - when(() => client.unlock()).thenReturn(() {}); - - when(() => client.request( - path, - data: options.data, - onReceiveProgress: options.onReceiveProgress, - onSendProgress: options.onSendProgress, - queryParameters: options.queryParameters, - cancelToken: options.cancelToken, - options: any(named: 'options'), - )).thenThrow(err); + when(() => client.fetch(options)).thenThrow(err); authInterceptor.onError(err, handler); @@ -193,21 +158,10 @@ void main() { verify(() => tokenManager.isStatic).called(1); - verify(() => client.lock()).called(1); - verify(() => tokenManager.loadToken(refresh: true)).called(1); verifyNoMoreInteractions(tokenManager); - verify(() => client.unlock()).called(1); - verify(() => client.request( - path, - data: options.data, - onReceiveProgress: options.onReceiveProgress, - onSendProgress: options.onSendProgress, - queryParameters: options.queryParameters, - cancelToken: options.cancelToken, - options: any(named: 'options'), - )).called(1); + verify(() => client.fetch(options)).called(1); verifyNoMoreInteractions(client); }, );