kevin.zhang

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

@@ -196,9 +196,10 @@ class GetHttpClient { @@ -196,9 +196,10 @@ class GetHttpClient {
196 196
197 var response = await _httpClient.send<T>(newRequest); 197 var response = await _httpClient.send<T>(newRequest);
198 198
199 - await _modifier.modifyResponse(newRequest, response); 199 + final newResponse =
  200 + await _modifier.modifyResponse<T>(newRequest, response);
200 201
201 - if (HttpStatus.unauthorized == response.statusCode && 202 + if (HttpStatus.unauthorized == newResponse.statusCode &&
202 _modifier.authenticator != null && 203 _modifier.authenticator != null &&
203 requestNumber <= maxAuthRetries) { 204 requestNumber <= maxAuthRetries) {
204 return _performRequest<T>( 205 return _performRequest<T>(
@@ -207,23 +208,23 @@ class GetHttpClient { @@ -207,23 +208,23 @@ class GetHttpClient {
207 requestNumber: requestNumber + 1, 208 requestNumber: requestNumber + 1,
208 headers: newRequest.headers, 209 headers: newRequest.headers,
209 ); 210 );
210 - } else if (HttpStatus.unauthorized == response.statusCode) { 211 + } else if (HttpStatus.unauthorized == newResponse.statusCode) {
211 if (!errorSafety) { 212 if (!errorSafety) {
212 throw UnauthorizedException(); 213 throw UnauthorizedException();
213 } else { 214 } else {
214 return Response<T>( 215 return Response<T>(
215 request: newRequest, 216 request: newRequest,
216 - headers: response.headers,  
217 - statusCode: response.statusCode,  
218 - body: response.body,  
219 - bodyBytes: response.bodyBytes,  
220 - bodyString: response.bodyString,  
221 - statusText: response.statusText, 217 + headers: newResponse.headers,
  218 + statusCode: newResponse.statusCode,
  219 + body: newResponse.body,
  220 + bodyBytes: newResponse.bodyBytes,
  221 + bodyString: newResponse.bodyString,
  222 + statusText: newResponse.statusText,
222 ); 223 );
223 } 224 }
224 } 225 }
225 226
226 - return response; 227 + return newResponse;
227 } on Exception catch (err) { 228 } on Exception catch (err) {
228 if (!errorSafety) { 229 if (!errorSafety) {
229 throw GetHttpException(err.toString()); 230 throw GetHttpException(err.toString());
@@ -42,11 +42,15 @@ class GetModifier<T> { @@ -42,11 +42,15 @@ class GetModifier<T> {
42 return newRequest; 42 return newRequest;
43 } 43 }
44 44
45 - Future<void> modifyResponse(Request request, Response response) async { 45 + Future<Response<T>> modifyResponse<T>(
  46 + Request<T> request, Response<T> response) async {
  47 + var newResponse = response;
46 if (_responseModifiers.isNotEmpty) { 48 if (_responseModifiers.isNotEmpty) {
47 for (var interceptor in _responseModifiers) { 49 for (var interceptor in _responseModifiers) {
48 - await interceptor(request, response); 50 + newResponse = await interceptor(request, response) as Response<T>;
49 } 51 }
50 } 52 }
  53 +
  54 + return newResponse;
51 } 55 }
52 } 56 }
@@ -76,9 +76,9 @@ class Request<T> { @@ -76,9 +76,9 @@ class Request<T> {
76 } 76 }
77 77
78 Request copyWith({ 78 Request copyWith({
79 - required Uri url,  
80 - required String method,  
81 - required Map<String, String> headers, 79 + Uri? url,
  80 + String? method,
  81 + Map<String, String>? headers,
82 Stream<List<int>>? bodyBytes, 82 Stream<List<int>>? bodyBytes,
83 bool followRedirects = true, 83 bool followRedirects = true,
84 int maxRedirects = 4, 84 int maxRedirects = 4,
@@ -91,10 +91,10 @@ class Request<T> { @@ -91,10 +91,10 @@ class Request<T> {
91 assert(maxRedirects > 0); 91 assert(maxRedirects > 0);
92 } 92 }
93 return Request._( 93 return Request._(
94 - url: url,  
95 - method: method, 94 + url: url ?? this.url,
  95 + method: method ?? this.method,
96 bodyBytes: bodyBytes ??= BodyBytesStream.fromBytes(const []), 96 bodyBytes: bodyBytes ??= BodyBytesStream.fromBytes(const []),
97 - headers: Map.from(headers), 97 + headers: headers == null ? this.headers : Map.from(headers),
98 followRedirects: followRedirects, 98 followRedirects: followRedirects,
99 maxRedirects: maxRedirects, 99 maxRedirects: maxRedirects,
100 contentLength: contentLength, 100 contentLength: contentLength,