I have a working loop that performs a GET request and then prints result data. What I need now is to make another loop that appends the cursor to the end of the 2nd and beyond request so each request can pull new data. So, the URL+cursor loop would contain my working data print loop.
I'm new to APIs but from what I gather request one produces a cursor and then you use that curse to find the next batch of data and then that batch has the cursor for the next batch over and over until there are no more batches and the cursor is blank.
I can get the first batch and the first cursor but I'm having issues using that cursor to find the next batch. Any advice?
PS - This example only loops 5 times but eventually I would make it loop until cursor is blank and print all the data to CSV.
import requests
import time
cursor=""
for i in range(5):
# api-endpoint
URL = "https://api.x.immutable.com/v1/orders?status=filled&sell_token_address=0xac98d8d1bb27a94e79fbf49198210240688bb1ed&cursor="
# sending get request and saving the response as response object
r = requests.get(url = URL + cursor)
# extracting data in json format
data = r.json()
for index, items in enumerate(data["result"]):
orderID = data["result"][index]['order_id']
info = data["result"][index]["sell"]["data"]["properties"]["name"]
price = data["result"][index]["buy"]["data"]["quantity"]
decimals = data["result"][index]["buy"]["data"]["decimals"]
print(index, orderID, info, price, decimals)
cursor = data["cursor"]
print("Cursor: ", cursor)
time.sleep(1)
print("END")
It's rarely a good idea to append parameters to the URL. Much better is to use a dictionary and pass that to the requests function.
When you're making multiple calls to the same URL (albeit with different parameters) performance can be improved by using a session object.
This should give you an idea of how you could construct this code:
import requests
from requests.adapters import HTTPAdapter, Retry
URL = "https://api.x.immutable.com/v1/orders"
params = {'status': 'filled',
'sell_token_address': '0xac98d8d1bb27a94e79fbf49198210240688bb1ed'}
with requests.Session() as session:
retry = Retry(
total=5,
status_forcelist=[500, 502, 503, 504],
backoff_factor=0.1
)
session.mount(URL, HTTPAdapter(max_retries=retry))
while True:
(r := session.get(URL, params=params)).raise_for_status()
data = r.json()
for value in data['result']:
orderID = value['order_id']
info = value["sell"]["data"]["properties"]["name"]
price = value["buy"]["data"]["quantity"]
decimals = value["buy"]["data"]["decimals"]
print(f'Order ID={orderID}, Info={info}, Price={price}, Decimals={decimals}')
if (cursor := data.get('cursor')):
params['cursor'] = cursor
else:
break
print("END")
Note:
Python 3.8+ required for this syntax