File size: 2,359 Bytes
815d872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Shared HTTP helpers for smoke scripts and tests."""

from __future__ import annotations

import urllib.request
from typing import Any

EXPECTED_STEP_KEYS = {"observation", "reward", "done"}


def ensure_server_up(base_url: str, timeout: float = 3) -> None:
    request = urllib.request.Request(f"{base_url}/health", method="GET")
    with urllib.request.urlopen(request, timeout=timeout) as response:
        if response.status != 200:
            raise RuntimeError(f"/health returned {response.status}")


def assert_step_payload(payload: dict[str, Any], action_kind: str | None = None) -> None:
    if set(payload.keys()) != EXPECTED_STEP_KEYS:
        context = f" for action {action_kind}" if action_kind else ""
        raise RuntimeError(
            f"Unexpected /step payload keys{context}: {sorted(payload.keys())}"
        )


def post_testclient_json(client: Any, path: str, payload: dict[str, Any] | None = None) -> dict[str, Any]:
    response = client.post(path, json=payload or {})
    response.raise_for_status()
    return response.json()


def step_testclient_json(client: Any, action: dict[str, Any]) -> dict[str, Any]:
    response = client.post("/step", json={"action": action})
    if response.status_code == 422:
        raise RuntimeError(f"422 from /step for action {action['kind']}: {response.text}")
    response.raise_for_status()
    payload = response.json()
    assert_step_payload(payload, str(action.get("kind", "")))
    return payload


def post_requests_json(

    base_url: str,

    path: str,

    payload: dict[str, Any] | None = None,

    timeout: float = 10,

) -> dict[str, Any]:
    import requests

    response = requests.post(f"{base_url}{path}", json=payload or {}, timeout=timeout)
    response.raise_for_status()
    return response.json()


def step_requests_json(

    base_url: str,

    action: dict[str, Any],

    timeout: float = 10,

) -> dict[str, Any]:
    import requests

    response = requests.post(f"{base_url}/step", json={"action": action}, timeout=timeout)
    if response.status_code == 422:
        raise RuntimeError(f"422 from /step for action {action['kind']}: {response.text}")
    response.raise_for_status()
    payload = response.json()
    assert_step_payload(payload, str(action.get("kind", "")))
    return payload