5 concepts · 10 practice questions
Page through a customer's API with the cursor or next link it returns, not offsets you compute, because offsets skip or repeat records when data changes mid-read. Expect rate limits: on a 429, wait as long as Retry-After says, otherwise back off exponentially with jitter, and cap your own concurrency. Save the cursor as a checkpoint, so a long extraction resumes where it stopped instead of starting over and burning the customer's quota again.
import random
import time
import requests
def wait_seconds(resp, attempt):
try:
return float(resp.headers["Retry-After"]) # seconds form only
except (KeyError, ValueError):
return 2 ** attempt + random.uniform(0, 1) # backoff with jitter
def fetch_all(url, headers):
params = {"limit": 200}
while True:
for attempt in range(6):
resp = requests.get(url, headers=headers, params=params, timeout=30)
if resp.status_code != 429 and resp.status_code < 500:
break
time.sleep(wait_seconds(resp, attempt))
resp.raise_for_status()
body = resp.json()
yield from body["data"]
if not body.get("next_cursor"):
return
params = {"limit": 200, "cursor": body["next_cursor"]}
Sign in to read the rest and take the 10-question test.
Sign in to continue