diff options
author | A. Cody Schuffelen <schuffelen@google.com> | 2021-05-25 17:03:08 -0700 |
---|---|---|
committer | Connor O'Brien <connoro@google.com> | 2021-06-24 17:26:51 -0700 |
commit | b69453e46831b502b3258da4152364ee281b86c6 (patch) | |
tree | b39993010fe436a345c89a34b260cb7d1b5158e2 | |
parent | 2d864ffe1c68729d401eb16440dd7315416982b9 (diff) | |
download | cuttlefish-b69453e46831b502b3258da4152364ee281b86c6.tar.gz |
Paginate when listing artifacts.
This handles the case where there are more than 1000 returned artifacts.
Bug: 189159498
Bug: 191309622
Bug: 191302402
Test: Run with command from b/189159498
Change-Id: If301f24da785b7a2b3f1cb7c7f854fd384aa8995
-rw-r--r-- | host/commands/fetcher/build_api.cc | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/host/commands/fetcher/build_api.cc b/host/commands/fetcher/build_api.cc index 186719989..8dc4db987 100644 --- a/host/commands/fetcher/build_api.cc +++ b/host/commands/fetcher/build_api.cc @@ -128,16 +128,27 @@ std::string BuildApi::ProductName(const DeviceBuild& build) { } std::vector<Artifact> BuildApi::Artifacts(const DeviceBuild& build) { - std::string url = BUILD_API + "/builds/" + build.id + "/" + build.target - + "/attempts/latest/artifacts?maxResults=1000"; - auto artifacts_json = curl.DownloadToJson(url, Headers()); - CHECK(!artifacts_json.isMember("error")) << "Error fetching the artifacts of " - << build << ". Response was " << artifacts_json; - + std::string page_token = ""; std::vector<Artifact> artifacts; - for (const auto& artifact_json : artifacts_json["artifacts"]) { - artifacts.emplace_back(artifact_json); - } + do { + std::string url = BUILD_API + "/builds/" + build.id + "/" + build.target + + "/attempts/latest/artifacts?maxResults=1000"; + if (page_token != "") { + url += "&pageToken=" + page_token; + } + auto artifacts_json = curl.DownloadToJson(url, Headers()); + CHECK(!artifacts_json.isMember("error")) + << "Error fetching the artifacts of " << build << ". Response was " + << artifacts_json; + if (artifacts_json.isMember("nextPageToken")) { + page_token = artifacts_json["nextPageToken"].asString(); + } else { + page_token = ""; + } + for (const auto& artifact_json : artifacts_json["artifacts"]) { + artifacts.emplace_back(artifact_json); + } + } while (page_token != ""); return artifacts; } |