File size: 1,287 Bytes
4bef415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import urllib.request
import json

BASE_URL = "http://localhost:7860"

def test_endpoint(name, method, path, data=None):
    print(f"Testing {name}...")
    url = f"{BASE_URL}{path}"
    try:
        req = urllib.request.Request(url, method=method)
        if data:
            req.add_header('Content-Type', 'application/json')
            req.data = json.dumps(data).encode('utf-8')
        
        with urllib.request.urlopen(req) as response:
            status = response.getcode()
            print(f"Status: {status}")
            if status == 200:
                resp_data = json.loads(response.read().decode('utf-8'))
                print(f"Response: {json.dumps(resp_data, indent=2)[:100]}...")
                return resp_data
            else:
                print(f"Error: {response.read().decode('utf-8')}")
    except Exception as e:
        print(f"Exception: {e}")
    return None

if __name__ == "__main__":
    test_endpoint("Health", "GET", "/health")
    test_endpoint("Metadata", "GET", "/metadata")
    reset_data = test_endpoint("Reset", "POST", "/reset", {"task_name": "easy"})
    if reset_data:
        episode_id = reset_data["observation"]["episode_id"]
        test_endpoint("Step", "POST", "/step", {"episode_id": episode_id, "action": {"action": 2}})