You are writing an automation script against the Databricks REST API and need to collect all items from a paginated endpoint while respecting rate limits. Implement a function that processes a predefined sequence of page responses, retries rate-limited pages with exponential backoff, and returns the final ordered result.
Implement fetch_all_items(responses, max_retries).
responses is a list of page response objects in the order they are encountered.page: integer page numberstatus: integer HTTP-like status code (200 or 429)items: list of integers, present when status == 200next_page: integer or null, present when status == 200retry_after: integer seconds, present when status == 429items: all fetched items in page ordertotal_wait: total simulated wait time from retriesattempts: number of processed responsesIf a page returns 429, retry the same page later. The wait added for the k-th retry of that page is retry_after * 2^(k-1). If any page exceeds max_retries, return {"items": [], "total_wait": -1, "attempts": attempts_so_far}.
Example 1
Input: responses = [{"page":1,"status":200,"items":[1,2],"next_page":2},{"page":2,"status":429,"retry_after":3},{"page":2,"status":200,"items":[3],"next_page":null}], max_retries = 2
Output: {"items":[1,2,3],"total_wait":3,"attempts":3}
Explanation: Page 2 is rate-limited once, then succeeds.
Example 2
Input: responses = [{"page":1,"status":429,"retry_after":2},{"page":1,"status":429,"retry_after":2}], max_retries = 1
Output: {"items":[],"total_wait":-1,"attempts":2}
Explanation: Page 1 exceeds the allowed retry count.
1 <= len(responses) <= 10^51 <= page <= 10^50 <= len(items) <= 10^31 <= retry_after <= 10^4