I'm polling an API endpoint using a while loop that checks for whether or not a .get() method on the JSON returns None:
while requests.get(render_execution_url, headers=headers).json().get('finalized_at') is None:
status = requests.get(render_execution_url, headers=headers).json().get('status')
status_detail = requests.get(render_execution_url, headers=headers).json().get('status_detail')
logger.info("status for {} is {}. detailed status is {}".format(render_execution_url, status, status_detail))
The idea here is that we keep polling the endpoint until the "finalized_at" value is populated.
Unfortunately, we periodically get failures when the JSON doesn't exist at all:
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I've tried using the retry decorator on the method (see below for the decorator syntax) but it doesn't seem to be executing retries when I hit this failure.
@retry(stop_max_attempt_number=7, wait_fixed=10000)
Is there a graceful, Pythonic way to deal with the case when the JSON doesn't exist (i.e., to try again in some amount of time)?