Return the contents of the 'endpoint' at 'addr' as JSON data subject to the condition specified in 'condition'. If we are unable to read the data or unable to meet the condition within 'timeout' seconds we throw an error.
(addr, endpoint, condition=None, timeout=5, query=None)
| 52 | |
| 53 | |
| 54 | def get_json(addr, endpoint, condition=None, timeout=5, query=None): |
| 55 | """ |
| 56 | Return the contents of the 'endpoint' at 'addr' as JSON data |
| 57 | subject to the condition specified in 'condition'. If we are |
| 58 | unable to read the data or unable to meet the condition within |
| 59 | 'timeout' seconds we throw an error. |
| 60 | """ |
| 61 | start_time = time.time() |
| 62 | |
| 63 | while True: |
| 64 | data = None |
| 65 | |
| 66 | try: |
| 67 | data = read_endpoint(addr, endpoint, query) |
| 68 | except Exception as exception: |
| 69 | pass |
| 70 | |
| 71 | if data: |
| 72 | try: |
| 73 | data = json.loads(data) |
| 74 | except Exception as exception: |
| 75 | raise CLIException("Could not load JSON from '{data}': {error}" |
| 76 | .format(data=data, error=str(exception))) |
| 77 | |
| 78 | if not condition: |
| 79 | return data |
| 80 | |
| 81 | if condition(data): |
| 82 | return data |
| 83 | |
| 84 | if time.time() - start_time > timeout: |
| 85 | raise CLIException("Failed to get data within {seconds} seconds" |
| 86 | .format(seconds=str(timeout))) |
| 87 | |
| 88 | time.sleep(0.1) |
nothing calls this directly
no test coverage detected