I need to call an API interatively, but the api endpoint limits me to 100 calls per minute. I have a list which is used as the data for an API call (I'm using requests to call the API).
Lets say the list has 227 elements. I need to make a separate API call for each element (that's just the way the API endpoint works).
My question is, how do I take the first 100 elements and use them to make 100 individual API calls, then wait for 60 secs, then repeat for the next 100 elements, sleep again for 60secs, then repeat for the last 27 elements?
I'm ok with the API cals themselves. I'm ok with using sleep() to pause the function, and I'm ok processing the response from the API calls.
It's just the breaking up my iterative loop into chunks that I'm struggling with.
My code looks a bit like this:
list_of_data = [{key1:value1, key2:value2, ... key227:value227}]
total_data=len(list_of_data_
for count in range(0, total_data):
if count <= total_data: # limit the api calls to the list length
data = list_of_data[count] # Pull out the current list element
count = count + 1 # step counter to the next list element
url = 'https:api.com/endpoint'
params = {
'param_1': data[key1],
'param2': data[key2],
...
'param227': data[key227]
}
headers = {some headers}
response = requests.get(url=url,params=params,headers=headers,)
json = response.json()
<process the json>
I'm not looking for you to debug the above. Its just to illustrate what I'm doing. the actual question is, how do I get this to pause every 100 times the API is called, without leaving off the remainder(modulus)?
Many thanks in advance.
for x in range(0, 227, 100): for y in range(100): fetch(x+y)with a sleep after the 100 loop and break whenx+y == 227.