MCPcopy Create free account
hub / github.com/apache/mesos / get_json

Function get_json

src/python/cli_new/lib/cli/http.py:54–88  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

52
53
54def 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)

Callers

nothing calls this directly

Calls 4

CLIExceptionClass · 0.90
read_endpointFunction · 0.85
sleepMethod · 0.80
timeMethod · 0.45

Tested by

no test coverage detected