跳至主要內容

指南

分頁

游標分頁的用法。

  • limit:每頁條數,1–100,預設 20。
  • 回應的 meta.next_cursor 不為 null 時,把它作為下一次請求的 cursor 參數即可取得下一頁;為 null 表示已經是最後一頁。
  • 翻頁期間新增的數據不會導致重複或遺漏。
  • 游標是不透明字串,請原樣傳回;格式錯誤的游標返回 422 invalid_cursor。
def all_events(client, session_id):
    cursor = None
    while True:
        params = {"limit": 100, **({"cursor": cursor} if cursor else {})}
        page = client.get(f"{API}/sessions/{session_id}/events", params=params).json()
        yield from page["items"]
        cursor = page["meta"]["next_cursor"]
        if cursor is None:
            return