Skip to content

feat: Simplify configuration of delay between retries #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ http_interceptor: <latest>
- 🍦 Compatible with vanilla Dart projects or Flutter projects.
- 🎉 Null-safety.
- ⏲ Timeout configuration with duration and timeout functions.
- ⏳ Configure the delay for each retry attempt.

## Usage

Expand Down Expand Up @@ -227,6 +228,17 @@ class ExpiredTokenRetryPolicy extends RetryPolicy {

You can also set the maximum amount of retry attempts with `maxRetryAttempts` property or override the `shouldAttemptRetryOnException` if you want to retry the request after it failed with an exception.

Sometimes it is helpful to have a cool-down phase between multiple requests. This delay could for example also differ between the first and the second retry attempt as shown in the following example.

```dart
class ExpiredTokenRetryPolicy extends RetryPolicy {
@override
Duration delayRetryAttemptOnResponse({required int retryAttempt}) {
return const Duration(milliseconds: 250) * math.pow(2.0, retryAttempt);
}
}
```

### Using self signed certificates

You can achieve support for self-signed certificates by providing `InterceptedHttp` or `InterceptedClient` with the `client` parameter when using the `build` method on either of those, it should look something like this:
Expand Down
6 changes: 6 additions & 0 deletions example/lib/weather_app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:http_interceptor/http_interceptor.dart';
Expand Down Expand Up @@ -344,6 +345,11 @@ class ExpiredTokenRetryPolicy extends RetryPolicy {
return false;
}

@override
Duration delayRetryAttemptOnResponse({required int retryAttempt}) {
return const Duration(milliseconds: 250) * math.pow(2.0, retryAttempt);
}

@override
Future<bool> shouldAttemptRetryOnResponse(BaseResponse response) async {
if (response.statusCode == 401) {
Expand Down
4 changes: 4 additions & 0 deletions lib/http/intercepted_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,17 @@ class InterceptedClient extends BaseClient {
retryPolicy!.maxRetryAttempts > _retryCount &&
await retryPolicy!.shouldAttemptRetryOnResponse(response)) {
_retryCount += 1;
await Future.delayed(retryPolicy!
.delayRetryAttemptOnResponse(retryAttempt: _retryCount));
return _attemptRequest(request);
}
} on Exception catch (error) {
if (retryPolicy != null &&
retryPolicy!.maxRetryAttempts > _retryCount &&
await retryPolicy!.shouldAttemptRetryOnException(error, request)) {
_retryCount += 1;
await Future.delayed(retryPolicy!
.delayRetryAttemptOnException(retryAttempt: _retryCount));
return _attemptRequest(request);
} else {
rethrow;
Expand Down
1 change: 0 additions & 1 deletion lib/http_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export './extensions/base_response_none.dart'
if (dart.library.io) './extensions/base_response_io.dart';
export './extensions/multipart_request.dart';
export './extensions/request.dart';
export './extensions/request.dart';
export './extensions/response.dart';
export './extensions/streamed_request.dart';
export './extensions/streamed_response.dart';
Expand Down
6 changes: 6 additions & 0 deletions lib/models/retry_policy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ abstract class RetryPolicy {

/// Number of maximum request attempts that can be retried.
final int maxRetryAttempts = 1;

Duration delayRetryAttemptOnException({required int retryAttempt}) =>
Duration.zero;

Duration delayRetryAttemptOnResponse({required int retryAttempt}) =>
Duration.zero;
}
20 changes: 20 additions & 0 deletions test/models/retry_policy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ main() {
});
});

group("delayRetryAttemptOnException", () {
test("returns no delay by default", () async {
// Act
final result = testObject.delayRetryAttemptOnException(retryAttempt: 0);

// Assert
expect(result, Duration.zero);
});
});

group("delayRetryAttemptOnResponse", () {
test("returns no delay by default", () async {
// Act
final result = testObject.delayRetryAttemptOnResponse(retryAttempt: 0);

// Assert
expect(result, Duration.zero);
});
});

group("shouldAttemptRetryOnException", () {
test("returns false by default", () async {
expect(
Expand Down