@@ -195,7 +195,7 @@ class PubHttpException implements Exception {
195
195
PubHttpException (this .message, {this .isIntermittent = false });
196
196
}
197
197
198
- /// Exception thrown when an HTTP response is not OK .
198
+ /// Exception thrown when an HTTP response is not Ok .
199
199
class PubHttpResponseException extends PubHttpException {
200
200
final http.Response response;
201
201
@@ -216,6 +216,14 @@ class PubHttpResponseException extends PubHttpException {
216
216
/// Program-wide limiter for concurrent network requests.
217
217
final _httpPool = Pool (16 );
218
218
219
+ /// Runs the provided function [fn] and returns the response.
220
+ ///
221
+ /// If there is an HTTP-related exception, an intermittent HTTP error response,
222
+ /// or an async timeout, [fn] is run repeatedly until there is a successful
223
+ /// response or at most seven total attempts have been made. If all attempts
224
+ /// fail, the final exception is re-thrown.
225
+ ///
226
+ /// Each attempt is run within a [Pool] configured with 16 maximum resources.
219
227
Future <T > retryForHttp <T >(String operation, FutureOr <T > Function () fn) async {
220
228
return await retry (
221
229
() async => await _httpPool.withResource (() async => await fn ()),
@@ -235,6 +243,13 @@ Future<T> retryForHttp<T>(String operation, FutureOr<T> Function() fn) async {
235
243
}
236
244
237
245
extension Throwing on http.BaseResponse {
246
+ /// Throws [PubHttpResponseException] , calls [fail] , or does nothing depending
247
+ /// on the status code.
248
+ ///
249
+ /// If the code is in the 200 range, nothing is done. If the code is 408, 429,
250
+ /// or in the 500 range, [PubHttpResponseException] is thrown with
251
+ /// "isIntermittent" set to `true` . Otherwise, [PubHttpResponseException] is
252
+ /// thrown with "isIntermittent" set to `false` .
238
253
void throwIfNotOk () {
239
254
if (statusCode >= 200 && statusCode <= 299 ) {
240
255
return ;
0 commit comments