Check response code, retry when downloading docs (#26386)
When downloading the ObjC/Java API docs, check the HTTP response status code and if not 200, attempt up to 5 times before giving up.
This commit is contained in:
parent
5293fef26d
commit
7022f981e2
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:archive/archive.dart';
|
import 'package:archive/archive.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
@ -22,10 +23,34 @@ Future<void> main(List<String> args) async {
|
|||||||
generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html');
|
generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> generateDocs(String url, String docName, String checkFile) async {
|
/// Fetches the zip archive at the specified url.
|
||||||
final http.Response response = await http.get(url);
|
///
|
||||||
|
/// Returns null if the archive fails to download after [maxTries] attempts.
|
||||||
|
Future<Archive> fetchArchive(String url, int maxTries) async {
|
||||||
|
List<int> responseBytes;
|
||||||
|
for (int i = 0; i < maxTries; i++) {
|
||||||
|
final http.Response response = await http.get(url);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
responseBytes = response.bodyBytes;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
stderr.writeln('Failed attempt ${i+1} to fetch $url.');
|
||||||
|
|
||||||
final Archive archive = ZipDecoder().decodeBytes(response.bodyBytes);
|
// On failure print a short snipped from the body in case it's helpful.
|
||||||
|
final int bodyLength = min(80, response.body.length);
|
||||||
|
stderr.writeln('Response status code ${response.statusCode}. Body: ' + response.body.substring(0, bodyLength));
|
||||||
|
sleep(const Duration(seconds: 1));
|
||||||
|
}
|
||||||
|
return responseBytes == null ? null : ZipDecoder().decodeBytes(responseBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> generateDocs(String url, String docName, String checkFile) async {
|
||||||
|
const int maxTries = 5;
|
||||||
|
final Archive archive = await fetchArchive(url, maxTries);
|
||||||
|
if (archive == null) {
|
||||||
|
stderr.writeln('Failed to fetch zip archive from: $url after $maxTries attempts. Giving up.');
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
final Directory output = Directory('$kDocRoot/$docName');
|
final Directory output = Directory('$kDocRoot/$docName');
|
||||||
print('Extracting $docName to ${output.path}');
|
print('Extracting $docName to ${output.path}');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user