Showing
3 changed files
with
39 additions
and
7 deletions
| @@ -192,11 +192,11 @@ class GetHttpClient { | @@ -192,11 +192,11 @@ class GetHttpClient { | ||
| 192 | }); | 192 | }); |
| 193 | 193 | ||
| 194 | if (authenticate) await _modifier.authenticator!(request); | 194 | if (authenticate) await _modifier.authenticator!(request); |
| 195 | - await _modifier.modifyRequest(request); | 195 | + final newRequest = await _modifier.modifyRequest<T>(request); |
| 196 | 196 | ||
| 197 | - var response = await _httpClient.send<T>(request); | 197 | + var response = await _httpClient.send<T>(newRequest); |
| 198 | 198 | ||
| 199 | - await _modifier.modifyResponse(request, response); | 199 | + await _modifier.modifyResponse(newRequest, response); |
| 200 | 200 | ||
| 201 | if (HttpStatus.unauthorized == response.statusCode && | 201 | if (HttpStatus.unauthorized == response.statusCode && |
| 202 | _modifier.authenticator != null && | 202 | _modifier.authenticator != null && |
| @@ -205,14 +205,14 @@ class GetHttpClient { | @@ -205,14 +205,14 @@ class GetHttpClient { | ||
| 205 | handler, | 205 | handler, |
| 206 | authenticate: true, | 206 | authenticate: true, |
| 207 | requestNumber: requestNumber + 1, | 207 | requestNumber: requestNumber + 1, |
| 208 | - headers: request.headers, | 208 | + headers: newRequest.headers, |
| 209 | ); | 209 | ); |
| 210 | } else if (HttpStatus.unauthorized == response.statusCode) { | 210 | } else if (HttpStatus.unauthorized == response.statusCode) { |
| 211 | if (!errorSafety) { | 211 | if (!errorSafety) { |
| 212 | throw UnauthorizedException(); | 212 | throw UnauthorizedException(); |
| 213 | } else { | 213 | } else { |
| 214 | return Response<T>( | 214 | return Response<T>( |
| 215 | - request: request, | 215 | + request: newRequest, |
| 216 | headers: response.headers, | 216 | headers: response.headers, |
| 217 | statusCode: response.statusCode, | 217 | statusCode: response.statusCode, |
| 218 | body: response.body, | 218 | body: response.body, |
| @@ -31,12 +31,15 @@ class GetModifier<T> { | @@ -31,12 +31,15 @@ class GetModifier<T> { | ||
| 31 | _requestModifiers.remove(interceptor); | 31 | _requestModifiers.remove(interceptor); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | - Future<void> modifyRequest(Request request) async { | 34 | + Future<Request<T>> modifyRequest<T>(Request<T> request) async { |
| 35 | + var newRequest = request; | ||
| 35 | if (_requestModifiers.isNotEmpty) { | 36 | if (_requestModifiers.isNotEmpty) { |
| 36 | for (var interceptor in _requestModifiers) { | 37 | for (var interceptor in _requestModifiers) { |
| 37 | - await interceptor(request); | 38 | + newRequest = await interceptor(newRequest) as Request<T>; |
| 38 | } | 39 | } |
| 39 | } | 40 | } |
| 41 | + | ||
| 42 | + return newRequest; | ||
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | Future<void> modifyResponse(Request request, Response response) async { | 45 | Future<void> modifyResponse(Request request, Response response) async { |
| @@ -74,6 +74,35 @@ class Request<T> { | @@ -74,6 +74,35 @@ class Request<T> { | ||
| 74 | decoder: decoder, | 74 | decoder: decoder, |
| 75 | ); | 75 | ); |
| 76 | } | 76 | } |
| 77 | + | ||
| 78 | + Request copyWith({ | ||
| 79 | + required Uri url, | ||
| 80 | + required String method, | ||
| 81 | + required Map<String, String> headers, | ||
| 82 | + Stream<List<int>>? bodyBytes, | ||
| 83 | + bool followRedirects = true, | ||
| 84 | + int maxRedirects = 4, | ||
| 85 | + int? contentLength, | ||
| 86 | + FormData? files, | ||
| 87 | + bool persistentConnection = true, | ||
| 88 | + Decoder<T>? decoder, | ||
| 89 | + }) { | ||
| 90 | + if (followRedirects) { | ||
| 91 | + assert(maxRedirects > 0); | ||
| 92 | + } | ||
| 93 | + return Request._( | ||
| 94 | + url: url, | ||
| 95 | + method: method, | ||
| 96 | + bodyBytes: bodyBytes ??= BodyBytesStream.fromBytes(const []), | ||
| 97 | + headers: Map.from(headers), | ||
| 98 | + followRedirects: followRedirects, | ||
| 99 | + maxRedirects: maxRedirects, | ||
| 100 | + contentLength: contentLength, | ||
| 101 | + files: files, | ||
| 102 | + persistentConnection: persistentConnection, | ||
| 103 | + decoder: decoder, | ||
| 104 | + ); | ||
| 105 | + } | ||
| 77 | } | 106 | } |
| 78 | 107 | ||
| 79 | extension BodyBytesStream on Stream<List<int>> { | 108 | extension BodyBytesStream on Stream<List<int>> { |
-
Please register or login to post a comment