From 00bad6b7e8f7fc251cc137864dddf654c806c386 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 20 May 2022 17:07:49 +0530 Subject: [PATCH 1/4] refactor(llc): remove dio deprecated methods Signed-off-by: xsahil03x --- .../core/http/interceptor/auth_interceptor.dart | 4 +--- .../lib/src/core/http/stream_http_client.dart | 14 -------------- .../http/interceptor/auth_interceptor_test.dart | 14 -------------- 3 files changed, 1 insertion(+), 31 deletions(-) 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..58db2fe3f 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,9 +57,7 @@ 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( 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..169492a7e 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 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..44b051413 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,14 +93,10 @@ 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, @@ -127,12 +123,9 @@ 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, @@ -163,14 +156,10 @@ 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, @@ -193,12 +182,9 @@ 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, From 2fdf3282e6f36cbd74e083957be52298d2529313 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 20 May 2022 17:24:06 +0530 Subject: [PATCH 2/4] refactor(llc): use `dio.fetch` instead of `dio.request` Signed-off-by: xsahil03x --- .../http/interceptor/auth_interceptor.dart | 25 +------------------ .../lib/src/core/http/stream_http_client.dart | 13 ++++++++++ 2 files changed, 14 insertions(+), 24 deletions(-) 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 58db2fe3f..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 @@ -60,30 +60,7 @@ class AuthInterceptor extends QueuedInterceptor { await _tokenManager.loadToken(refresh: true); 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 169492a7e..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 @@ -266,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); + } + } } From 8b13bcebba5ec43e9ae8b82e5bded22269e65289 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 20 May 2022 17:49:00 +0530 Subject: [PATCH 3/4] chore(llc): fix lint Signed-off-by: xsahil03x --- packages/stream_chat/test/src/client/client_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 389bd8f295111bf82a90dcf97fadaae2b582ff8b Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 20 May 2022 17:54:26 +0530 Subject: [PATCH 4/4] test(llc): fix tests Signed-off-by: xsahil03x --- .../interceptor/auth_interceptor_test.dart | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) 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 44b051413..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 @@ -97,15 +97,7 @@ void main() { when(() => tokenManager.loadToken(refresh: true)) .thenAnswer((_) async => token); - 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, )); @@ -126,15 +118,7 @@ void main() { verify(() => tokenManager.loadToken(refresh: true)).called(1); verifyNoMoreInteractions(tokenManager); - 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); }); @@ -160,15 +144,7 @@ void main() { when(() => tokenManager.loadToken(refresh: true)) .thenAnswer((_) async => token); - 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); @@ -185,15 +161,7 @@ void main() { verify(() => tokenManager.loadToken(refresh: true)).called(1); verifyNoMoreInteractions(tokenManager); - 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); }, );