-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathretry_policy.dart
More file actions
55 lines (49 loc) · 1.5 KB
/
retry_policy.dart
File metadata and controls
55 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'dart:async';
import 'package:http/http.dart';
/// Defines the behavior for retrying requests.
///
/// Example:
///
/// ```dart
/// class ExpiredTokenRetryPolicy extends RetryPolicy {
/// @override
/// int get maxRetryAttempts => 2;
///
/// @override
/// bool shouldAttemptRetryOnException(Exception reason) {
/// log(reason.toString());
///
/// return false;
/// }
///
/// @override
/// Future<bool> shouldAttemptRetryOnResponse(BaseResponse response) async {
/// if (response.statusCode == 401) {
/// log("Retrying request...");
/// final cache = await SharedPreferences.getInstance();
///
/// cache.setString(kOWApiToken, OPEN_WEATHER_API_KEY);
///
/// return true;
/// }
///
/// return false;
/// }
/// }
/// ```
abstract class RetryPolicy {
/// Defines whether the request should be retried when an Exception occurs
/// while making said request to the server.
FutureOr<bool> shouldAttemptRetryOnException(
Exception reason, BaseRequest request) =>
false;
/// Defines whether the request should be retried after the request has
/// received `response` from the server.
FutureOr<bool> shouldAttemptRetryOnResponse(BaseResponse response) => false;
/// Number of maximum request attempts that can be retried.
int get maxRetryAttempts => 1;
Duration delayRetryAttemptOnException({required int retryAttempt}) =>
Duration.zero;
Duration delayRetryAttemptOnResponse({required int retryAttempt}) =>
Duration.zero;
}