Unverified Commit 7022f981 authored by Chris Bracken's avatar Chris Bracken Committed by GitHub

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.
parent 5293fef2
......@@ -4,6 +4,7 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:archive/archive.dart';
import 'package:http/http.dart' as http;
......@@ -22,10 +23,34 @@ Future<void> main(List<String> args) async {
generateDocs(objcdocUrl, 'objcdoc', 'Classes/FlutterViewController.html');
}
Future<void> generateDocs(String url, String docName, String checkFile) async {
final http.Response response = await http.get(url);
/// Fetches the zip archive at the specified 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.');
// 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);
}
final Archive archive = ZipDecoder().decodeBytes(response.bodyBytes);
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');
print('Extracting $docName to ${output.path}');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment