Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(llc): remove dio deprecated methods #1172

Merged
merged 4 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
27 changes: 13 additions & 14 deletions packages/stream_chat/lib/src/core/http/stream_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -280,4 +266,17 @@ class StreamHttpClient {
throw _parseError(error);
}
}

/// Handy method to make http requests from [RequestOptions]
/// with error parsing.
Future<Response<T>> fetch<T>(
RequestOptions requestOptions,
) async {
try {
final response = await httpClient.fetch<T>(requestOptions);
return response;
} on DioError catch (error) {
throw _parseError(error);
}
}
}
2 changes: 1 addition & 1 deletion packages/stream_chat/test/src/client/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
Expand All @@ -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);
});

Expand All @@ -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);

Expand All @@ -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);
},
);
Expand Down