Skip to content

Commit dd76453

Browse files
refactor: switch OpenHands API helper to requests.Session and JSON helpers
- Replace urllib with requests - Keep simple _http_json wrapper for consistency, but leverage response.json() Co-authored-by: openhands <[email protected]>
1 parent b2b0774 commit dd76453

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

scripts/openhands_api.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import json
2929
import time
30-
import urllib.request
30+
import requests
3131

3232

3333
DEFAULT_BASE_URL = "https://app.all-hands.dev"
@@ -41,17 +41,16 @@ class Conversation:
4141

4242

4343
def _http_json(url: str, method: str, headers: dict[str, str], data: dict[str, Any] | None) -> dict[str, Any]:
44-
req = urllib.request.Request(url=url, method=method)
45-
for k, v in headers.items():
46-
req.add_header(k, v)
47-
if data is not None:
48-
payload = json.dumps(data).encode("utf-8")
49-
req.data = payload
50-
with urllib.request.urlopen(req, timeout=API_TIMEOUT) as resp:
51-
body = resp.read().decode("utf-8")
52-
if not body:
53-
return {}
54-
return json.loads(body)
44+
with requests.Session() as s:
45+
s.headers.update(headers)
46+
if method.upper() == "POST":
47+
r = s.post(url, json=data, timeout=API_TIMEOUT)
48+
elif method.upper() == "GET":
49+
r = s.get(url, timeout=API_TIMEOUT)
50+
else:
51+
r = s.request(method.upper(), url, json=data, timeout=API_TIMEOUT)
52+
r.raise_for_status()
53+
return r.json() if r.content else {}
5554

5655

5756
def create_conversation(base_url: str, api_key: str, initial_user_msg: str, repo: str | None = None) -> Conversation:

0 commit comments

Comments
 (0)