forked from boxlite-ai/boxlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_boxes.py
More file actions
83 lines (62 loc) · 2.11 KB
/
Copy pathmanage_boxes.py
File metadata and controls
83 lines (62 loc) · 2.11 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
#!/usr/bin/env python3
"""
Box CRUD operations via the REST API.
Demonstrates:
- create() with a named box
- get() / get_info() to retrieve by ID or name
- get_or_create() for idempotent creation
- list_info() to enumerate all boxes
- remove() to delete a box
Prerequisites:
make dev:python
boxlite serve --port 8100
"""
import asyncio
from boxlite import ApiKeyCredential, Boxlite, BoxOptions, BoxliteRestOptions
SERVER_URL = "http://localhost:8100"
def connect() -> Boxlite:
return Boxlite.rest(BoxliteRestOptions(
url=SERVER_URL, credential=ApiKeyCredential("local-dev-key"),
))
async def main():
print("=" * 50)
print("REST API: Box Management (CRUD)")
print("=" * 50)
rt = connect()
# --- Create ---
print("\n=== Create ===")
opts = BoxOptions(image="alpine:latest")
box = await rt.create(opts, name="rest-crud-demo")
box_id = box.id
print(f" Created box: {box_id}")
# --- Get by ID ---
print("\n=== Get Info by ID ===")
info = await rt.get_info(box_id)
print(f" Found: id={info.id} name={info.name} status={info.state.status}")
# --- Get handle by name ---
print("\n=== Get Handle by Name ===")
handle = await rt.get("rest-crud-demo")
print(f" Found by name: {handle.id}")
# --- Get or Create (idempotent) ---
print("\n=== Get or Create ===")
box2, created = await rt.get_or_create(
BoxOptions(image="alpine:latest"), name="rest-crud-demo",
)
print(f" id={box2.id} newly_created={created}")
# --- List ---
print("\n=== List ===")
all_boxes = await rt.list_info()
print(f" Total boxes: {len(all_boxes)}")
for b in all_boxes:
marker = " <-- ours" if str(b.id) == str(box_id) else ""
print(f" {b.id} {b.name or '(unnamed)'} {b.state.status}{marker}")
# --- Remove ---
print("\n=== Remove ===")
await rt.remove(box_id, force=True)
print(f" Removed {box_id}")
# Verify removal
info_after = await rt.get_info(box_id)
print(f" get_info after remove: {info_after}")
print("\n Done")
if __name__ == "__main__":
asyncio.run(main())