-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
147 lines (114 loc) · 4.05 KB
/
agent.py
File metadata and controls
147 lines (114 loc) · 4.05 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import os
import sys
import time
from typing import Any, Dict
import requests
BASE_URL = os.getenv("MACHINEID_BASE_URL", "https://machineid.io").rstrip("/")
REGISTER_URL = f"{BASE_URL}/api/v1/devices/register"
VALIDATE_URL = f"{BASE_URL}/api/v1/devices/validate"
def get_org_key() -> str:
org_key = os.getenv("MACHINEID_ORG_KEY")
if not org_key:
raise RuntimeError(
"Missing MACHINEID_ORG_KEY.\n"
"Example:\n"
" export MACHINEID_ORG_KEY=org_your_key_here\n"
)
return org_key.strip()
def default_device_id() -> str:
"""
Minimal, deterministic default for demos and starters.
No local machine info.
Override with MACHINEID_DEVICE_ID if needed.
"""
return "python-starter:01"
def post_json(
url: str,
headers: Dict[str, str],
payload: Dict[str, Any],
timeout_s: int = 10,
) -> Dict[str, Any]:
resp = requests.post(url, headers=headers, json=payload, timeout=timeout_s)
try:
data = resp.json()
except Exception:
print("❌ Could not parse JSON response.")
print("Status code:", resp.status_code)
print("Body:", resp.text)
raise
if resp.status_code >= 400:
if isinstance(data, dict) and data.get("error"):
return {
"status": "error",
"error": data.get("error"),
"http": resp.status_code,
}
return {
"status": "error",
"error": f"HTTP {resp.status_code}",
"http": resp.status_code,
"body": data,
}
return data
def register_device(org_key: str, device_id: str) -> Dict[str, Any]:
headers = {"x-org-key": org_key, "Content-Type": "application/json"}
payload = {"deviceId": device_id}
print(f"→ Registering device '{device_id}' via {REGISTER_URL} ...")
data = post_json(REGISTER_URL, headers, payload)
print(f"✔ register response: status={data.get('status')} handler={data.get('handler')}")
return data
def validate_device(org_key: str, device_id: str) -> Dict[str, Any]:
headers = {"x-org-key": org_key, "Content-Type": "application/json"}
payload = {"deviceId": device_id}
print(f"→ Validating device '{device_id}' via {VALIDATE_URL} (POST canonical) ...")
data = post_json(VALIDATE_URL, headers, payload)
print(
f"✔ validate decision: "
f"allowed={data.get('allowed')} "
f"code={data.get('code')} "
f"request_id={data.get('request_id')}"
)
return data
def main() -> None:
org_key = get_org_key()
device_id = (os.getenv("MACHINEID_DEVICE_ID") or "").strip() or default_device_id()
print("✔ MACHINEID_ORG_KEY loaded:", org_key[:12] + "...")
print("Using base_url:", BASE_URL)
print("Using device_id:", device_id)
print()
# 1) Register (idempotent)
reg = register_device(org_key, device_id)
reg_status = reg.get("status")
print()
print("Registration summary:")
print(" status :", reg_status)
if reg.get("planTier") is not None:
print(" planTier :", reg.get("planTier"))
if reg.get("limit") is not None:
print(" limit :", reg.get("limit"))
if reg.get("devicesUsed") is not None:
print(" devicesUsed :", reg.get("devicesUsed"))
print()
if reg_status not in ("ok", "exists"):
print("🚫 Register did not succeed. Exiting.")
sys.exit(1)
# 2) Validate (hard gate)
time.sleep(1)
val = validate_device(org_key, device_id)
allowed = bool(val.get("allowed", False))
code = val.get("code")
request_id = val.get("request_id")
print()
print("Validation summary:")
print(" allowed :", allowed)
print(" code :", code)
print(" request_id :", request_id)
print()
if not allowed:
print("🚫 Execution denied (hard gate). Exiting immediately.")
sys.exit(0)
print("✅ Execution allowed. Start/continue work here.")
print("Done. agent.py completed successfully.")
if __name__ == "__main__":
main()