33import httpx
44import json
55import sys
6+ import os
7+ import socket
68from datetime import datetime
9+ from urllib .parse import urlparse
710
8- # Configuration
9- API_URL = "http://localhost:8000/api/v1"
11+ # Configuration (allow override via env; try host.docker.internal -> localhost)
12+ def _url_reachable (url , timeout = 1.0 ):
13+ try :
14+ p = urlparse (url )
15+ host = p .hostname or "localhost"
16+ port = p .port or (443 if p .scheme == "https" else 80 )
17+ sock = socket .create_connection ((host , port ), timeout )
18+ sock .close ()
19+ return True
20+ except Exception :
21+ return False
22+
23+ API_URL = os .getenv ("API_URL" )
24+ candidates = [API_URL , "http://host.docker.internal:8000/api/v1" , "http://localhost:8000/api/v1" ]
25+ API_URL = next ((c for c in candidates if c and _url_reachable (c )), candidates [0 ])
1026# For a real demo with auth, we would need to login first.
1127# Assuming default dev setup might allow open access or we mock a token if RBAC is strict.
1228# Based on existing code, some endpoints might be protected.
@@ -23,7 +39,28 @@ async def run_demo():
2339 print ("[1] Checking API Health..." )
2440 try :
2541 resp = await client .get ("/" )
26- print (f" ✅ API is up: { resp .json ()['message' ]} " )
42+ # Try to read a 'message' (root) if present
43+ try :
44+ root_json = resp .json ()
45+ except Exception :
46+ root_json = None
47+
48+ if resp .status_code == 200 and root_json and isinstance (root_json , dict ) and root_json .get ('message' ):
49+ print (f" ✅ API is up: { root_json .get ('message' )} " )
50+ else :
51+ # If base_url targets /api/v1, try the health route under that prefix
52+ resp2 = await client .get ("/health" )
53+ if resp2 .status_code == 200 :
54+ try :
55+ h = resp2 .json ()
56+ status_str = h .get ('status' ) or h .get ('message' ) or str (h )
57+ except Exception :
58+ status_str = resp2 .text
59+ print (f" ✅ API is up: { status_str } " )
60+ else :
61+ print (f" ❌ Health check failed: { resp .status_code } { resp .text } " )
62+ print (" Make sure 'docker compose up' is running!" )
63+ sys .exit (1 )
2764 except Exception as e :
2865 print (f" ❌ API Not Reachable: { e } " )
2966 print (" Make sure 'docker compose up' is running!" )
0 commit comments