guidesoutenance
This commit is contained in:
@@ -3,15 +3,26 @@ import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'intra_api_exception.dart';
|
||||
import 'intra_auth_service.dart';
|
||||
|
||||
/// GET API 42 avec gestion 401 / 429 (retry + backoff).
|
||||
/// GET API 42 avec gestion 401 (refresh token), 429 (retry + backoff).
|
||||
Future<http.Response> intraApiGet(
|
||||
Uri uri, {
|
||||
required Map<String, String> headers,
|
||||
required IntraAuthService auth,
|
||||
int maxAttempts = 4,
|
||||
}) async {
|
||||
var retriedAfterRefresh = false;
|
||||
|
||||
for (var attempt = 0; attempt < maxAttempts; attempt++) {
|
||||
final response = await http.get(uri, headers: headers);
|
||||
final response = await http.get(uri, headers: auth.authHeaders());
|
||||
|
||||
if (response.statusCode == 401) {
|
||||
if (!retriedAfterRefresh && await auth.refreshAccessToken()) {
|
||||
retriedAfterRefresh = true;
|
||||
continue;
|
||||
}
|
||||
throw const IntraUnauthorizedException();
|
||||
}
|
||||
|
||||
if (response.statusCode == 429) {
|
||||
if (attempt + 1 >= maxAttempts) {
|
||||
@@ -35,7 +46,6 @@ Duration _retryDelay(http.Response response, int attempt) {
|
||||
return Duration(seconds: seconds.clamp(1, 30));
|
||||
}
|
||||
}
|
||||
// 0.8s, 1.6s, 3.2s…
|
||||
final ms = 800 * (1 << attempt);
|
||||
return Duration(milliseconds: ms.clamp(800, 8000));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user