Test an endpoint and check for expected key in response.
(base, path, expected_key=None)
| 23 | |
| 24 | |
| 25 | def test_endpoint(base, path, expected_key=None): |
| 26 | """Test an endpoint and check for expected key in response.""" |
| 27 | url = base + path |
| 28 | print(f"Testing: {url}") |
| 29 | try: |
| 30 | with urllib.request.urlopen(url, timeout=10) as resp: |
| 31 | data = json.loads(resp.read()) |
| 32 | print(f" Response: {str(data)[:200]}") |
| 33 | if expected_key and expected_key not in data: |
| 34 | print(f" ERROR: Expected key '{expected_key}' not found!") |
| 35 | return False |
| 36 | return True |
| 37 | except urllib.error.HTTPError as e: |
| 38 | print(f" HTTP ERROR {e.code}: {e.reason}") |
| 39 | return False |
| 40 | except Exception as e: |
| 41 | print(f" ERROR: {e}") |
| 42 | return False |
| 43 | |
| 44 | |
| 45 | def main(): |