Merge pull request #1172 from GetStream/feat/remove-dio-deprecations

refactor(llc): remove dio deprecated methods
This commit is contained in:
Salvatore Giordano
2022-05-20 15:46:07 +02:00
committed by GitHub
4 changed files with 20 additions and 92 deletions
@@ -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);
@@ -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<Response<T>> fetch<T>(
RequestOptions requestOptions,
) async {
try {
final response = await httpClient.fetch<T>(requestOptions);
return response;
} on DioError catch (error) {
throw _parseError(error);
}
}
}
@@ -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);
@@ -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);
},
);