Download every record from a customer's API. client.get(cursor) returns one of:
{"status": 200, "items": [...], "next": cursor_or_None}{"status": 429, "retry_after": seconds} — wait exactly that long, then retry the same cursor
(doesn't count as a failure){"status": 503} — transient: wait 1, 2, 4, 8, 8, … seconds (doubling, capped at 8) and retry the same
cursor. After 5 consecutive 503s for one cursor, raise RuntimeError.Use the provided sleep(seconds) (a fake clock). The first cursor is None. Return all items in order.
The harness reports the items, total time slept and whether you gave up.
Implement sync. Input: client (fake), sleep: callable. Returns: list.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeAPI:
def __init__(self, script):
self.script, self.i, self.cursors = script, 0, []
def get(self, cursor):
self.cursors.append(cursor)
response = self.script[self.i]
self.i += 1
return dict(response)
def __harness__(fn, script):
clock = {"slept": 0.0}
def sleep(seconds):
clock["slept"] += seconds
api = FakeAPI(script)
try:
items = fn(api, sleep)
gave_up = False
except RuntimeError:
items, gave_up = None, True
return {"items": items, "slept": clock["slept"], "gave_up": gave_up, "cursors": api.cursors}
def sync(client, sleep) -> list:
response = client.get(None)
return response.get("items", [])
Run executes the public tests. Submit grades against public and hidden tests.
Download every record from a customer's API. client.get(cursor) returns one of:
{"status": 200, "items": [...], "next": cursor_or_None}{"status": 429, "retry_after": seconds} — wait exactly that long, then retry the same cursor
(doesn't count as a failure){"status": 503} — transient: wait 1, 2, 4, 8, 8, … seconds (doubling, capped at 8) and retry the same
cursor. After 5 consecutive 503s for one cursor, raise RuntimeError.Use the provided sleep(seconds) (a fake clock). The first cursor is None. Return all items in order.
The harness reports the items, total time slept and whether you gave up.
Implement sync. Input: client (fake), sleep: callable. Returns: list.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeAPI:
def __init__(self, script):
self.script, self.i, self.cursors = script, 0, []
def get(self, cursor):
self.cursors.append(cursor)
response = self.script[self.i]
self.i += 1
return dict(response)
def __harness__(fn, script):
clock = {"slept": 0.0}
def sleep(seconds):
clock["slept"] += seconds
api = FakeAPI(script)
try:
items = fn(api, sleep)
gave_up = False
except RuntimeError:
items, gave_up = None, True
return {"items": items, "slept": clock["slept"], "gave_up": gave_up, "cursors": api.cursors}
def sync(client, sleep) -> list:
response = client.get(None)
return response.get("items", [])
Run executes the public tests. Submit grades against public and hidden tests.