|
1 | 1 | import 'dart:async'; |
2 | 2 | import 'dart:convert'; |
| 3 | +import 'dart:io'; |
3 | 4 | import 'dart:typed_data'; |
| 5 | + |
4 | 6 | import 'package:flutter/foundation.dart'; |
5 | | -import 'package:http/http.dart' as http; |
6 | 7 | import 'package:http/http.dart'; |
7 | | -import 'package:http_interceptor/models/models.dart'; |
| 8 | +import 'package:http/io_client.dart'; |
8 | 9 | import 'package:http_interceptor/interceptor_contract.dart'; |
| 10 | +import 'package:http_interceptor/models/models.dart'; |
9 | 11 | import 'package:http_interceptor/utils.dart'; |
10 | 12 |
|
11 | 13 | import 'http_methods.dart'; |
@@ -34,34 +36,43 @@ import 'http_methods.dart'; |
34 | 36 | ///``` |
35 | 37 | ///Don't forget to close the client once you are done, as a client keeps |
36 | 38 | ///the connection alive with the server. |
37 | | -class HttpClientWithInterceptor extends http.BaseClient { |
| 39 | +class HttpClientWithInterceptor extends BaseClient { |
38 | 40 | List<InterceptorContract> interceptors; |
39 | 41 | Duration requestTimeout; |
40 | 42 | RetryPolicy retryPolicy; |
| 43 | + bool Function(X509Certificate, String, int) badCertificateCallback; |
41 | 44 |
|
42 | | - final Client _client = Client(); |
43 | 45 | int _retryCount = 0; |
| 46 | + Client _client; |
| 47 | + |
| 48 | + void _initializeClient() { |
| 49 | + var ioClient = new HttpClient() |
| 50 | + ..badCertificateCallback = badCertificateCallback; |
| 51 | + _client = IOClient(ioClient); |
| 52 | + } |
44 | 53 |
|
45 | 54 | HttpClientWithInterceptor._internal({ |
46 | 55 | this.interceptors, |
47 | 56 | this.requestTimeout, |
48 | 57 | this.retryPolicy, |
| 58 | + this.badCertificateCallback, |
49 | 59 | }); |
50 | 60 |
|
51 | 61 | factory HttpClientWithInterceptor.build({ |
52 | 62 | @required List<InterceptorContract> interceptors, |
53 | 63 | Duration requestTimeout, |
54 | 64 | RetryPolicy retryPolicy, |
| 65 | + bool Function(X509Certificate, String, int) badCertificateCallback, |
55 | 66 | }) { |
56 | 67 | assert(interceptors != null); |
57 | 68 |
|
58 | 69 | //Remove any value that is null. |
59 | 70 | interceptors.removeWhere((interceptor) => interceptor == null); |
60 | 71 | return HttpClientWithInterceptor._internal( |
61 | | - interceptors: interceptors, |
62 | | - requestTimeout: requestTimeout, |
63 | | - retryPolicy: retryPolicy, |
64 | | - ); |
| 72 | + interceptors: interceptors, |
| 73 | + requestTimeout: requestTimeout, |
| 74 | + retryPolicy: retryPolicy, |
| 75 | + badCertificateCallback: badCertificateCallback); |
65 | 76 | } |
66 | 77 |
|
67 | 78 | Future<Response> head(url, {Map<String, String> headers}) => _sendUnstreamed( |
@@ -130,7 +141,12 @@ class HttpClientWithInterceptor extends http.BaseClient { |
130 | 141 | }); |
131 | 142 | } |
132 | 143 |
|
133 | | - Future<StreamedResponse> send(BaseRequest request) => _client.send(request); |
| 144 | + Future<StreamedResponse> send(BaseRequest request) { |
| 145 | + if (_client == null) { |
| 146 | + _initializeClient(); |
| 147 | + } |
| 148 | + return _client.send(request); |
| 149 | + } |
134 | 150 |
|
135 | 151 | Future<Response> _sendUnstreamed({ |
136 | 152 | @required Method method, |
@@ -193,23 +209,25 @@ class HttpClientWithInterceptor extends http.BaseClient { |
193 | 209 | : await send(request).timeout(requestTimeout); |
194 | 210 |
|
195 | 211 | response = await Response.fromStream(stream); |
196 | | - if (retryPolicy != null |
197 | | - && retryPolicy.maxRetryAttempts > _retryCount |
198 | | - && retryPolicy.shouldAttemptRetryOnResponse(response)) { |
| 212 | + if (retryPolicy != null && |
| 213 | + retryPolicy.maxRetryAttempts > _retryCount && |
| 214 | + await retryPolicy.shouldAttemptRetryOnResponse( |
| 215 | + ResponseData.fromHttpResponse(response))) { |
199 | 216 | _retryCount += 1; |
200 | 217 | return _attemptRequest(request); |
201 | 218 | } |
202 | 219 | } catch (error) { |
203 | | - if (retryPolicy != null |
204 | | - && retryPolicy.maxRetryAttempts > _retryCount |
205 | | - && retryPolicy.shouldAttemptRetryOnException(error)) { |
| 220 | + if (retryPolicy != null && |
| 221 | + retryPolicy.maxRetryAttempts > _retryCount && |
| 222 | + retryPolicy.shouldAttemptRetryOnException(error)) { |
206 | 223 | _retryCount += 1; |
207 | 224 | return _attemptRequest(request); |
208 | 225 | } else { |
209 | 226 | throw HttpInterceptorException(error.toString()); |
210 | 227 | } |
211 | 228 | } |
212 | 229 |
|
| 230 | + _retryCount = 0; |
213 | 231 | return response; |
214 | 232 | } |
215 | 233 |
|
@@ -238,6 +256,9 @@ class HttpClientWithInterceptor extends http.BaseClient { |
238 | 256 | } |
239 | 257 |
|
240 | 258 | void close() { |
| 259 | + if (_client == null) { |
| 260 | + _initializeClient(); |
| 261 | + } |
241 | 262 | _client.close(); |
242 | 263 | } |
243 | 264 | } |
0 commit comments