-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_raw.py
More file actions
84 lines (67 loc) · 2.58 KB
/
Copy pathlive_raw.py
File metadata and controls
84 lines (67 loc) · 2.58 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import atexit
import logging
import os
from typing import Any, cast
# Ignore OPENCODE_LOG for this demo
os.environ.pop("OPENCODE_LOG", None)
from opencode import OpencodeClient, RawResponse
from opencode._server import create_opencode_server
# Suppress verbose debug logging in demo
for name in ("httpx", "httpcore", "opencode"):
logging.getLogger(name).setLevel(logging.WARNING)
server = create_opencode_server(port=4096)
client = OpencodeClient(base_url=server.url)
def _cleanup() -> None:
client.close()
server.close()
atexit.register(_cleanup)
print("Raw response demo")
print("=" * 40)
# Normal mode
print("\n[1] Normal mode:")
h = client.health()
print(f" {type(h).__name__}: ok={h.ok}")
# Raw response mode
print("\n[2] Raw response mode:")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.health())
print(f" {type(raw).__name__}")
print(f" .status_code = {raw.status_code}")
print(f" .headers = {dict(raw.headers).get('content-type')}")
print(f" .parsed = {type(raw.parsed).__name__}, ok={raw.parsed.ok}")
# Raw with JSON data
print("\n[3] Raw response — project/current:")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.project_current())
print(f" {type(raw).__name__}, status={raw.status_code}")
print(f" .parsed type: {type(raw.parsed).__name__}")
if raw.parsed:
print(f" project id: {raw.parsed.id[:20]}...")
# Raw with list response
print("\n[4] Raw response — agents (list):")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.app_agents())
print(f" {type(raw).__name__}, status={raw.status_code}")
print(f" .parsed type: {type(raw.parsed).__name__}")
print(f" agents: {len(raw.parsed)}")
# Raw with stream (event)
print("\n[5] Raw response — event stream:")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.global_event())
print(f" {type(raw).__name__}, status={raw.status_code}")
print(f" .parsed type: {type(raw.parsed).__name__}")
raw.parsed.close()
# Raw with 204
print("\n[6] Raw response — 204 (clear prompt):")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.tui_clear_prompt())
print(f" {type(raw).__name__}, status={raw.status_code}")
print(f" .parsed = {raw.parsed!r}")
# Headers inspection
print("\n[7] Headers inspection:")
with client.with_raw_response:
raw = cast(RawResponse[Any], client.health())
print(f" Server: {raw.headers.get('server', 'N/A')}")
print(f" Content-Type: {raw.headers.get('content-type')}")
print(f" Date: {raw.headers.get('date', 'N/A')}")
print("\nDone!")