← 返回 ramp 的题目列表Follow a Stateful HTTP Request Chain
类型:online_judge
Problem: Follow a Stateful HTTP Request Chain
Implement an HTTP client that starts from an initial URL and issues GET requests. Each response provides JSON clues needed to continue the request chain.
A response may contain:
next_url: the URL for the next request;
bearer_token: a Bearer token required by some later requests;
result: the final value to return when the task is complete.
Keep updating client state and requesting the next URL until a result is received.
Requirements
The initial request must not include authentication.
Once a bearer_token is received, subsequent requests must include:
Authorization: Bearer <token>
Some requests can fail transiently, such as with HTTP 429 or 5xx. Retry such requests a bounded number of times using exponential backoff.
If a response contains a new bearer_token, it replaces the previous token.
Report an error if a response contains neither next_url nor result, or if retries are exhausted.
Example response sequence
Response from the initial URL:
{"next_url": "https://api.example.com/step-2"}
Response from the second URL:
{"bearer_token": "abc", "next_url": "https://api.example.com/step-3"}
Response from the third URL:
{"result": "completed"}
The program should return:
completed
Constraints
The request chain contains between 1 and 10,000 requests.
Retry each retryable failure at most 3 times.
Avoid infinite loops; terminate with an error if the same URL is revisited an unreasonable number of times.