-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger_task.py
More file actions
63 lines (48 loc) · 1.54 KB
/
trigger_task.py
File metadata and controls
63 lines (48 loc) · 1.54 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
#!/usr/bin/env python3
"""Queue a Komos task run from Python.
Set KOMOS_API_KEY and KOMOS_TASK_ID before running:
export KOMOS_API_KEY="sk_live_..."
export KOMOS_TASK_ID="your-task-id"
python python/trigger_task.py
"""
from __future__ import annotations
import json
import os
import sys
import urllib.error
import urllib.request
API_BASE_URL = 'https://api.komos.ai/public/v1'
def require_env(name: str) -> str:
value = os.environ.get(name)
if not value:
print(f'Missing required environment variable: {name}', file=sys.stderr)
sys.exit(1)
return value
def main() -> None:
api_key = require_env('KOMOS_API_KEY')
task_id = require_env('KOMOS_TASK_ID')
payload = {
'inputs': {
'example': 'hello from Python',
},
'clientRequestId': 'python-example-run',
}
request = urllib.request.Request(
f'{API_BASE_URL}/tasks/{task_id}/runs',
data=json.dumps(payload).encode('utf-8'),
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
},
method='POST',
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
body = response.read().decode('utf-8')
print(json.dumps(json.loads(body), indent=2))
except urllib.error.HTTPError as exc:
error_body = exc.read().decode('utf-8')
print(f'Komos API returned HTTP {exc.code}: {error_body}', file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()