From c79f26b6bd417ea175653a34f114bea595e6b1ec Mon Sep 17 00:00:00 2001 From: Ivan Terekhin Date: Mon, 31 May 2021 18:09:28 +0300 Subject: [PATCH] Fix for #259 (#263) --- chopper/lib/src/base.dart | 100 ++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 53 deletions(-) diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index 3fa4e998..be1d720e 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -1,20 +1,19 @@ import 'dart:async'; - -import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; - -import 'annotations.dart'; -import 'authenticator.dart'; +import 'package:http/http.dart' as http; import 'constants.dart'; + import 'interceptor.dart'; import 'request.dart'; import 'response.dart'; +import 'annotations.dart'; +import 'authenticator.dart'; import 'utils.dart'; Type _typeOf() => T; @visibleForTesting -final List allowedInterceptorsType = [ +final allowedInterceptorsType = [ RequestInterceptor, RequestInterceptorFunc, ResponseInterceptor, @@ -121,7 +120,7 @@ class ChopperClient { Iterable services = const [], }) : httpClient = client ?? http.Client(), _clientIsInternal = client == null { - if (!interceptors.every(_isAnInterceptor)) { + if (interceptors.every(_isAnInterceptor) == false) { throw ArgumentError( 'Unsupported type for interceptors, it only support the following types:\n' '${allowedInterceptorsType.join('\n - ')}', @@ -163,28 +162,31 @@ class ChopperClient { /// final todoService = chopper.getService(); /// ``` ServiceType getService() { - final Type serviceType = _typeOf(); + final serviceType = _typeOf(); if (serviceType == dynamic || serviceType == ChopperService) { throw Exception( - 'Service type should be provided, `dynamic` is not allowed.', - ); + 'Service type should be provided, `dynamic` is not allowed.'); } - final ChopperService? service = _services[serviceType]; + final service = _services[serviceType]; if (service == null) { throw Exception('Service of type \'$serviceType\' not found.'); } - return service as ServiceType; } - Future _encodeRequest(Request request) async => - converter?.convertRequest(request) ?? request; + Future _encodeRequest(Request request) async { + return converter?.convertRequest(request) ?? request; + } Future> _decodeResponse( Response response, Converter withConverter, - ) async => - await withConverter.convertResponse(response); + ) async { + final converted = + await withConverter.convertResponse(response); + + return converted; + } Future _interceptRequest(Request req) async { final body = req.body; @@ -201,7 +203,6 @@ class ChopperClient { 'Interceptors should not transform the body of the request' 'Use Request converter instead', ); - return req; } @@ -241,7 +242,11 @@ class ChopperClient { error = errorRes?.error ?? errorRes?.body; } - return Response(response.base, null, error: error); + return Response( + response.base, + null, + error: error, + ); } Future> _handleSuccessResponse( @@ -264,12 +269,17 @@ class ChopperClient { Future _handleRequestConverter( Request request, ConvertRequest? requestConverter, - ) async => - request.body != null || request.parts.isNotEmpty - ? requestConverter != null - ? await requestConverter(request) - : await _encodeRequest(request) - : request; + ) async { + if (request.body != null || request.parts.isNotEmpty) { + if (requestConverter != null) { + request = await requestConverter(request); + } else { + request = (await _encodeRequest(request)); + } + } + + return request; + } /// Sends a pre-build [Request], applying all provided [Interceptor]s and /// [Converter]s. @@ -288,9 +298,8 @@ class ChopperClient { ConvertRequest? requestConverter, ConvertResponse? responseConverter, }) async { - var req = await _interceptRequest( - await _handleRequestConverter(request, requestConverter), - ); + var req = await _handleRequestConverter(request, requestConverter); + req = await _interceptRequest(req); _requestController.add(req); final streamRes = await httpClient.send(await req.toBaseRequest()); @@ -302,39 +311,24 @@ class ChopperClient { dynamic res = Response(response, response.body); if (authenticator != null) { - var updatedRequest = await authenticator!.authenticate(req, res, request); + var updatedRequest = await authenticator!.authenticate(request, res); if (updatedRequest != null) { - res = await send( - updatedRequest, - requestConverter: requestConverter, - responseConverter: responseConverter, - ); - // To prevent double call with typed response - if (_responseIsSuccessful(res.statusCode)) { - return _processResponse(res); - } else { - res = await _handleErrorResponse(res); - - return _processResponse(res); - } + res = await send(updatedRequest); } } - res = _responseIsSuccessful(res.statusCode) - ? await _handleSuccessResponse( - res, - responseConverter, - ) - : await _handleErrorResponse(res); - - return _processResponse(res); - } + if (_responseIsSuccessful(res.statusCode)) { + res = await _handleSuccessResponse( + res, + responseConverter, + ); + } else { + res = await _handleErrorResponse(res); + } - Future> _processResponse( - dynamic res, - ) async { res = await _interceptResponse(res); + _responseController.add(res); return res;