Skip to content

Commit

Permalink
Fix for #259 (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
JEuler committed Oct 8, 2022
1 parent e26eef4 commit c79f26b
Showing 1 changed file with 47 additions and 53 deletions.
100 changes: 47 additions & 53 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -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>() => T;

@visibleForTesting
final List<Type> allowedInterceptorsType = [
final allowedInterceptorsType = <Type>[
RequestInterceptor,
RequestInterceptorFunc,
ResponseInterceptor,
Expand Down Expand Up @@ -121,7 +120,7 @@ class ChopperClient {
Iterable<ChopperService> 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 - ')}',
Expand Down Expand Up @@ -163,28 +162,31 @@ class ChopperClient {
/// final todoService = chopper.getService<TodosListService>();
/// ```
ServiceType getService<ServiceType extends ChopperService>() {
final Type serviceType = _typeOf<ServiceType>();
final serviceType = _typeOf<ServiceType>();
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<Request> _encodeRequest(Request request) async =>
converter?.convertRequest(request) ?? request;
Future<Request> _encodeRequest(Request request) async {
return converter?.convertRequest(request) ?? request;
}

Future<Response<BodyType>> _decodeResponse<BodyType, InnerType>(
Response response,
Converter withConverter,
) async =>
await withConverter.convertResponse<BodyType, InnerType>(response);
) async {
final converted =
await withConverter.convertResponse<BodyType, InnerType>(response);

return converted;
}

Future<Request> _interceptRequest(Request req) async {
final body = req.body;
Expand All @@ -201,7 +203,6 @@ class ChopperClient {
'Interceptors should not transform the body of the request'
'Use Request converter instead',
);

return req;
}

Expand Down Expand Up @@ -241,7 +242,11 @@ class ChopperClient {
error = errorRes?.error ?? errorRes?.body;
}

return Response<BodyType>(response.base, null, error: error);
return Response<BodyType>(
response.base,
null,
error: error,
);
}

Future<Response<BodyType>> _handleSuccessResponse<BodyType, InnerType>(
Expand All @@ -264,12 +269,17 @@ class ChopperClient {
Future<Request> _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.
Expand All @@ -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());
Expand All @@ -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<BodyType, InnerType>(
updatedRequest,
requestConverter: requestConverter,
responseConverter: responseConverter,
);
// To prevent double call with typed response
if (_responseIsSuccessful(res.statusCode)) {
return _processResponse(res);
} else {
res = await _handleErrorResponse<BodyType, InnerType>(res);

return _processResponse(res);
}
res = await send<BodyType, InnerType>(updatedRequest);
}
}

res = _responseIsSuccessful(res.statusCode)
? await _handleSuccessResponse<BodyType, InnerType>(
res,
responseConverter,
)
: await _handleErrorResponse<BodyType, InnerType>(res);

return _processResponse(res);
}
if (_responseIsSuccessful(res.statusCode)) {
res = await _handleSuccessResponse<BodyType, InnerType>(
res,
responseConverter,
);
} else {
res = await _handleErrorResponse<BodyType, InnerType>(res);
}

Future<Response<BodyType>> _processResponse<BodyType, InnerType>(
dynamic res,
) async {
res = await _interceptResponse<BodyType, InnerType>(res);

_responseController.add(res);

return res;
Expand Down

0 comments on commit c79f26b

Please sign in to comment.