kevin.zhang

fix: 1. use returned instance for modifyResponse as well; 2. remove mandatory copyWith parameters.

... ... @@ -196,9 +196,10 @@ class GetHttpClient {
var response = await _httpClient.send<T>(newRequest);
await _modifier.modifyResponse(newRequest, response);
final newResponse =
await _modifier.modifyResponse<T>(newRequest, response);
if (HttpStatus.unauthorized == response.statusCode &&
if (HttpStatus.unauthorized == newResponse.statusCode &&
_modifier.authenticator != null &&
requestNumber <= maxAuthRetries) {
return _performRequest<T>(
... ... @@ -207,23 +208,23 @@ class GetHttpClient {
requestNumber: requestNumber + 1,
headers: newRequest.headers,
);
} else if (HttpStatus.unauthorized == response.statusCode) {
} else if (HttpStatus.unauthorized == newResponse.statusCode) {
if (!errorSafety) {
throw UnauthorizedException();
} else {
return Response<T>(
request: newRequest,
headers: response.headers,
statusCode: response.statusCode,
body: response.body,
bodyBytes: response.bodyBytes,
bodyString: response.bodyString,
statusText: response.statusText,
headers: newResponse.headers,
statusCode: newResponse.statusCode,
body: newResponse.body,
bodyBytes: newResponse.bodyBytes,
bodyString: newResponse.bodyString,
statusText: newResponse.statusText,
);
}
}
return response;
return newResponse;
} on Exception catch (err) {
if (!errorSafety) {
throw GetHttpException(err.toString());
... ...
... ... @@ -42,11 +42,15 @@ class GetModifier<T> {
return newRequest;
}
Future<void> modifyResponse(Request request, Response response) async {
Future<Response<T>> modifyResponse<T>(
Request<T> request, Response<T> response) async {
var newResponse = response;
if (_responseModifiers.isNotEmpty) {
for (var interceptor in _responseModifiers) {
await interceptor(request, response);
newResponse = await interceptor(request, response) as Response<T>;
}
}
return newResponse;
}
}
... ...
... ... @@ -76,9 +76,9 @@ class Request<T> {
}
Request copyWith({
required Uri url,
required String method,
required Map<String, String> headers,
Uri? url,
String? method,
Map<String, String>? headers,
Stream<List<int>>? bodyBytes,
bool followRedirects = true,
int maxRedirects = 4,
... ... @@ -91,10 +91,10 @@ class Request<T> {
assert(maxRedirects > 0);
}
return Request._(
url: url,
method: method,
url: url ?? this.url,
method: method ?? this.method,
bodyBytes: bodyBytes ??= BodyBytesStream.fromBytes(const []),
headers: Map.from(headers),
headers: headers == null ? this.headers : Map.from(headers),
followRedirects: followRedirects,
maxRedirects: maxRedirects,
contentLength: contentLength,
... ...