You get an API client with one method, client.list_items(page_token=None). It returns a dictionary
{"items": [...], "next_page_token": str | None}. Call it until there is no next token and return all
items in order.
The client is provided by the test harness (it records your calls). Don't call it more often than needed.
Implement fetch_all. Input: client — has list_items(page_token=None) -> dict. Returns: list.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeClient:
"""Serves pre-defined pages. Records every call so the harness can check you don't over-fetch."""
def __init__(self, pages):
self._pages = pages
self.calls = []
def list_items(self, page_token=None):
self.calls.append(page_token)
index = 0 if page_token is None else int(page_token.removeprefix("p"))
if index >= len(self._pages):
raise ValueError(f"invalid page token {page_token!r}")
nxt = f"p{index + 1}" if index + 1 < len(self._pages) else None
return {"items": list(self._pages[index]), "next_page_token": nxt}
def __harness__(fn, pages):
client = FakeClient(pages)
return {"items": fn(client), "calls": len(client.calls)}
def fetch_all(client) -> list:
return client.list_items()["items"]
Run executes the public tests. Submit grades against public and hidden tests.
You get an API client with one method, client.list_items(page_token=None). It returns a dictionary
{"items": [...], "next_page_token": str | None}. Call it until there is no next token and return all
items in order.
The client is provided by the test harness (it records your calls). Don't call it more often than needed.
Implement fetch_all. Input: client — has list_items(page_token=None) -> dict. Returns: list.
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeClient:
"""Serves pre-defined pages. Records every call so the harness can check you don't over-fetch."""
def __init__(self, pages):
self._pages = pages
self.calls = []
def list_items(self, page_token=None):
self.calls.append(page_token)
index = 0 if page_token is None else int(page_token.removeprefix("p"))
if index >= len(self._pages):
raise ValueError(f"invalid page token {page_token!r}")
nxt = f"p{index + 1}" if index + 1 < len(self._pages) else None
return {"items": list(self._pages[index]), "next_page_token": nxt}
def __harness__(fn, pages):
client = FakeClient(pages)
return {"items": fn(client), "calls": len(client.calls)}
def fetch_all(client) -> list:
return client.list_items()["items"]
Run executes the public tests. Submit grades against public and hidden tests.