0

I found this question but the problem seems to persist. When I run

result = requests.get("https://api.etc.com/v1/something", params=payload).json()

and then run

print(type(result))
print(type(result['data'][0]))

I get the types 'dict' and 'string' (or something like that) as you would expect.

However, I'm getting a "rate limit exceeded" error from the API at that last line on the console. If we're really storing the results in result, why does an API request appear to be generated from that line?

The aim is to store the API request result in a variable to then probe its contents freely without triggering further requests. How do I do that? Is something else going on? The console points specifically to that line when moaning about request rate limits.

Sometimes the error is triggered by other lines. For example, the latest output of the console is

> {'message': 'rate_limit_exceeded', 'status': 'error'}
Traceback (most recent call last):
  File "/Users/User/folder/code.py", line 85, in <module>
    derivative_resuslt = result['data'][0]
KeyError: 'data'
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Norbert
  • 435
  • 5
  • 14
  • That code should not be generating extra API requests. Can you provide a [mcve] program that exhibits the described behavior? – jwodder Oct 31 '17 at 20:50
  • Thank you, @jwodder. I will try to provide said code either in the next couple of hours or tomorrow. In the meantime, I am adding the console's output. – Norbert Oct 31 '17 at 21:01
  • 3
    The console output appears to show the contents of `result` (which happen to be about a rate limit being exceeded) followed by an error from trying to access the nonexistent `"data"` key of `result`. This suggests to me that the original API request is returning a rate limit error, your code then prints the response, and then some other error occurs, and you're just assuming the rate limit error is the cause because it's the last line printed before the traceback. – jwodder Oct 31 '17 at 21:09
  • 2
    Your first line prints the resulting JSON from `.to_json()`. The second line tries to access content within that dict despite the fact that the structure doesn't match. The console isn't *moaning* about the output API limits, but rather showing the output requested and _subsequently_ dumping an error for the invalid reference. – benschumacher Oct 31 '17 at 21:10
  • @jwodder, @benschumacher thank you. You're right that in this particular console output example, the error is due to something else: probably that `result['data']` doesn't exist because a preceding rate limit error prevented it from taking a value. However, I also sometimes and insistently get the error at this line: `print(type(result['data'][0]))`. Is it possible that `result` stored a rate limit error message and that that is why it is printed and the code stopped at that point, and not because the request is being re-run? – Norbert Oct 31 '17 at 21:18

0 Answers0