Pagination on Coinbase Python API

拟墨画扇 提交于 2019-12-04 20:22:56

Above snippet encountered error. This works

client = Client(keys['apiKey'], keys['apiSecret'])
accounts = client.get_accounts()

for account in accounts.data:
    txns = client.get_transactions(account.id, limit=25)
    while True:
        for tx in txns.data:
            print(tx.id)

        if txns.pagination.next_uri != None:
            starting_after_guid = re.search('starting_after=(.*)', str(txns.pagination.next_uri), re.I).group(1)
            txns = client.get_transactions(account.id, limit=25, starting_after=starting_after_guid)
        else:
            break

This is how it can work without regex:

_cb = Client(keys['apiKey'], keys['apiSecret'])
accounts = client.get_accounts()

for account in accounts.data:
    """ Gets the transactions history from coinbase """
    all_txns = []
    starting_after = None
    while True:
        txns = _cb.get_transactions(account['id'], limit=100, starting_after=starting_after)
        if txns.pagination.next_starting_after is not None:
            starting_after = txns.pagination.next_starting_after
            for tx in txns.data:
                all_txns.append(tx)
            time.sleep(1)  # Let's not hit the rate limiting
        else:
            for tx in txns.data:
                all_txns.append(tx)
            break

Notice that next_starting_after is taken from the transaction and used as starting_after parameter for the next query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!