'Python KeyError after returning the value (also error_id 502)

I'm using stackoverflow's API and want to use the 'quota_remaining' so I can perform pagination. But when I try to print the 'quota_remaining' I am getting a KeyError after the print. So it prints the value but I am not able to store it in a variable because it throws a KeyError afterwards. This is my code:

# Get data
users_url = 'https://api.stackexchange.com/2.3/users?page=1&pagesize=100&order=desc&sort=modified&site=stackoverflow&filter=!56ApJn82ELRG*IWQxo6.gXu9qS90qXxNmY8e9b'

# Make the API call
response = requests.get(users_url)
result = response.json()
    
print(result)
print(result['quota_remaining']) # line 33
quota_remaining = result['quota_remaining']

And this is what is returned (I included a sample of the print(result)):

{'items': ['badge_counts': {'bronze': 6, 'silver': 0, 'gold': 0}, 'view_count': 21, 'answer_count': 2, 'question_count': 14, 'reputation_change_quarter': 0, 'reputation': 75, 'user_id': 2498916, 'link': 'https://stackoverflow.com/users/2498916/oscar-salas', 'display_name': 'Oscar Salas'}], 'has_more': True, 'backoff': 10, 'quota_max': 300, 'quota_remaining': 261, 'page': 1, 'page_size': 100}
261
1
{'error_id': 502, 'error_message': 'Violation of backoff parameter', 'error_name': 'throttle_violation'}
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    print(result['quota_remaining'])
KeyError: 'quota_remaining'

I also don't understand why I am getting the error 502, what am I violating? What is the backoff parameter?



Solution 1:[1]

Change variable name, try this:

users_url = 'https://api.stackexchange.com/2.3/users?page=1&pagesize=100&order=desc&sort=modified&site=stackoverflow&filter=!56ApJn82ELRG*IWQxo6.gXu9qS90qXxNmY8e9b'

# Make the API call
response = requests.get(users_url)
result = response.json()
    
print(result)
print(result['quota_remaining']) # line 33
quota_remaining1 = result['quota_remaining']

Solution 2:[2]

Okay I've figured it out. I was making too many requests as @SimonT pointed out.

The backoff parameter meant I had to wait that many seconds before hitting the same method again. I'm my case I had a backoff = 10 so I set up a time.sleep(10) between requests.

This is actually my full code (I had only a sample in the question as I did not understood the keyError was actually because of the throttle violation - a rookie mistake):

while quota > 240:
    # Get data
    users_url = f'https://api.stackexchange.com/2.3/users?page={page}&pagesize=100&order=desc&sort=modified&site=stackoverflow&filter=!56ApJn82ELRG*IWQxo6.gXu9qS90qXxNmY8e9b'

    # Make the API call
    response = requests.get(users_url)
    result = response.json()
    
    print(result['quota_remaining'])
    print(result['page'])

    # Update counters
    quota = result['quota_remaining']
    page = result['page']

    # Save users in a list
    for user in result["items"]:
        user['_id'] = user.pop('user_id')
        results.append(user)


    if result['has_more'] == True:
        time.sleep(10)
        page += 1
    else:
        break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EvilReboot
Solution 2 goncalocoutinho