hot-dev 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hot/__init__.py +10 -0
- hot/_transport.py +157 -0
- hot/client.py +154 -0
- hot/errors.py +54 -0
- hot/py.typed +1 -0
- hot/resources.py +617 -0
- hot/sse.py +51 -0
- hot/types.py +11 -0
- hot_dev-1.1.0.dist-info/METADATA +202 -0
- hot_dev-1.1.0.dist-info/RECORD +12 -0
- hot_dev-1.1.0.dist-info/WHEEL +4 -0
- hot_dev-1.1.0.dist-info/licenses/LICENSE +190 -0
hot/resources.py
ADDED
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import time
|
|
5
|
+
from collections.abc import AsyncIterator, Iterator, Mapping
|
|
6
|
+
from typing import Any
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
from ._transport import AsyncTransport, SyncTransport, raise_for_status, without_none
|
|
10
|
+
from .sse import consume_sse_blocks
|
|
11
|
+
from .types import StreamEvent
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def enc(value: str) -> str:
|
|
15
|
+
return quote(value, safe="")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def enc_path(path: str) -> str:
|
|
19
|
+
return "/".join(quote(part, safe="") for part in path.split("/"))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _run_id_of(event: Any) -> str | None:
|
|
23
|
+
if not isinstance(event, dict):
|
|
24
|
+
return None
|
|
25
|
+
run = event.get("run")
|
|
26
|
+
if isinstance(run, dict):
|
|
27
|
+
run_id = run.get("run_id")
|
|
28
|
+
if isinstance(run_id, str):
|
|
29
|
+
return run_id
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class EventsResource:
|
|
34
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
35
|
+
self._transport = transport
|
|
36
|
+
|
|
37
|
+
def publish(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
38
|
+
return self._transport.request_json("POST", "/events", json=dict(body))["data"]
|
|
39
|
+
|
|
40
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
41
|
+
return self._transport.request_json("GET", "/events", params=query)
|
|
42
|
+
|
|
43
|
+
def get(self, event_id: str) -> dict[str, Any]:
|
|
44
|
+
return self._transport.request_json("GET", f"/events/{enc(event_id)}")["data"]
|
|
45
|
+
|
|
46
|
+
def get_runs(self, event_id: str) -> dict[str, Any]:
|
|
47
|
+
return self._transport.request_json("GET", f"/events/{enc(event_id)}/runs")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class RunsResource:
|
|
51
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
52
|
+
self._transport = transport
|
|
53
|
+
|
|
54
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
55
|
+
return self._transport.request_json("GET", "/runs", params=query)
|
|
56
|
+
|
|
57
|
+
def stats(self) -> dict[str, Any]:
|
|
58
|
+
return self._transport.request_json("GET", "/runs/stats")["data"]
|
|
59
|
+
|
|
60
|
+
def get(self, run_id: str) -> dict[str, Any]:
|
|
61
|
+
return self._transport.request_json("GET", f"/runs/{enc(run_id)}")["data"]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class EnvResource:
|
|
65
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
66
|
+
self._transport = transport
|
|
67
|
+
|
|
68
|
+
def get(self) -> dict[str, Any]:
|
|
69
|
+
return self._transport.request_json("GET", "/env")["data"]
|
|
70
|
+
|
|
71
|
+
def subscribe(self) -> Iterator[StreamEvent]:
|
|
72
|
+
yield from iter_stream(self._transport, "GET", "/env/subscribe")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class OrgResource:
|
|
76
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
77
|
+
self._transport = transport
|
|
78
|
+
|
|
79
|
+
def usage(self) -> dict[str, Any]:
|
|
80
|
+
return self._transport.request_json("GET", "/org/usage")["data"]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class StreamsResource:
|
|
84
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
85
|
+
self._transport = transport
|
|
86
|
+
|
|
87
|
+
def subscribe(self, stream_id: str, *, project: str | None = None) -> Iterator[StreamEvent]:
|
|
88
|
+
yield from iter_stream(
|
|
89
|
+
self._transport,
|
|
90
|
+
"GET",
|
|
91
|
+
f"/streams/{enc(stream_id)}/subscribe",
|
|
92
|
+
params={"project": project},
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def subscribe_post(
|
|
96
|
+
self,
|
|
97
|
+
stream_id: str,
|
|
98
|
+
*,
|
|
99
|
+
project: str | None = None,
|
|
100
|
+
) -> Iterator[StreamEvent]:
|
|
101
|
+
yield from iter_stream(
|
|
102
|
+
self._transport,
|
|
103
|
+
"POST",
|
|
104
|
+
f"/streams/{enc(stream_id)}/subscribe",
|
|
105
|
+
params={"project": project},
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
def subscribe_with_event(
|
|
109
|
+
self,
|
|
110
|
+
body: Mapping[str, Any],
|
|
111
|
+
*,
|
|
112
|
+
project: str | None = None,
|
|
113
|
+
reconnect: bool = True,
|
|
114
|
+
max_attempts: int = 5,
|
|
115
|
+
) -> Iterator[StreamEvent]:
|
|
116
|
+
"""Atomically subscribe then publish.
|
|
117
|
+
|
|
118
|
+
The Hot API closes SSE subscriptions after 5 minutes of idle time. When
|
|
119
|
+
``reconnect=True`` (the default), the SDK resubscribes to the same
|
|
120
|
+
``stream_id`` and continues yielding events until a terminal
|
|
121
|
+
``run:stop``/``run:fail``/``run:cancel`` arrives. Replayed
|
|
122
|
+
``run:start`` and terminal events are deduped by ``run_id``.
|
|
123
|
+
``stream:data`` chunks are not replayed and may be lost across the
|
|
124
|
+
disconnect window.
|
|
125
|
+
|
|
126
|
+
Pass ``reconnect=False`` for the 1.0.x single-connection behavior.
|
|
127
|
+
"""
|
|
128
|
+
if not reconnect:
|
|
129
|
+
yield from self._subscribe_with_event_once(body, project=project)
|
|
130
|
+
return
|
|
131
|
+
|
|
132
|
+
seen_start: set[str] = set()
|
|
133
|
+
seen_terminal: set[str] = set()
|
|
134
|
+
stream_id: str | None = None
|
|
135
|
+
attempts = 0
|
|
136
|
+
|
|
137
|
+
while True:
|
|
138
|
+
if stream_id is None:
|
|
139
|
+
iterator = self._subscribe_with_event_once(body, project=project)
|
|
140
|
+
else:
|
|
141
|
+
iterator = self.subscribe(stream_id, project=project)
|
|
142
|
+
|
|
143
|
+
terminal = False
|
|
144
|
+
try:
|
|
145
|
+
for event in iterator:
|
|
146
|
+
event_type = event.get("type") if isinstance(event, dict) else None
|
|
147
|
+
|
|
148
|
+
if event_type == "event:published":
|
|
149
|
+
published = event.get("stream_id") if isinstance(event, dict) else None
|
|
150
|
+
if isinstance(published, str):
|
|
151
|
+
stream_id = published
|
|
152
|
+
|
|
153
|
+
if event_type == "run:start":
|
|
154
|
+
run_id = _run_id_of(event)
|
|
155
|
+
if run_id and run_id in seen_start:
|
|
156
|
+
continue
|
|
157
|
+
if run_id:
|
|
158
|
+
seen_start.add(run_id)
|
|
159
|
+
|
|
160
|
+
if event_type in ("run:stop", "run:fail", "run:cancel"):
|
|
161
|
+
run_id = _run_id_of(event)
|
|
162
|
+
if run_id and run_id in seen_terminal:
|
|
163
|
+
continue
|
|
164
|
+
if run_id:
|
|
165
|
+
seen_terminal.add(run_id)
|
|
166
|
+
terminal = True
|
|
167
|
+
|
|
168
|
+
yield event
|
|
169
|
+
except Exception:
|
|
170
|
+
if stream_id is None or attempts >= max_attempts:
|
|
171
|
+
raise
|
|
172
|
+
|
|
173
|
+
if terminal:
|
|
174
|
+
return
|
|
175
|
+
if stream_id is None:
|
|
176
|
+
raise RuntimeError("stream ended before event:published was received")
|
|
177
|
+
|
|
178
|
+
attempts += 1
|
|
179
|
+
time.sleep(min(0.25 * attempts, 2.0))
|
|
180
|
+
|
|
181
|
+
def _subscribe_with_event_once(
|
|
182
|
+
self,
|
|
183
|
+
body: Mapping[str, Any],
|
|
184
|
+
*,
|
|
185
|
+
project: str | None = None,
|
|
186
|
+
) -> Iterator[StreamEvent]:
|
|
187
|
+
yield from iter_stream(
|
|
188
|
+
self._transport,
|
|
189
|
+
"POST",
|
|
190
|
+
"/streams/subscribe-with-event",
|
|
191
|
+
json=dict(body),
|
|
192
|
+
params={"project": project},
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class FilesResource:
|
|
197
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
198
|
+
self._transport = transport
|
|
199
|
+
|
|
200
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
201
|
+
return self._transport.request_json("GET", "/files", params=query)
|
|
202
|
+
|
|
203
|
+
def get(self, file_id: str) -> dict[str, Any]:
|
|
204
|
+
return self._transport.request_json("GET", f"/files/{enc(file_id)}")["data"]
|
|
205
|
+
|
|
206
|
+
def delete(self, file_id: str) -> None:
|
|
207
|
+
self._transport.request_raw("DELETE", f"/files/{enc(file_id)}")
|
|
208
|
+
|
|
209
|
+
def download(self, file_id: str):
|
|
210
|
+
return self._transport.request_raw("GET", f"/files/{enc(file_id)}/download")
|
|
211
|
+
|
|
212
|
+
def upload(
|
|
213
|
+
self,
|
|
214
|
+
path: str,
|
|
215
|
+
content: bytes | str,
|
|
216
|
+
*,
|
|
217
|
+
content_type: str | None = None,
|
|
218
|
+
) -> dict[str, Any]:
|
|
219
|
+
response = self._transport.request_raw(
|
|
220
|
+
"PUT",
|
|
221
|
+
f"/files/upload/{enc_path(path)}",
|
|
222
|
+
content=content,
|
|
223
|
+
headers={
|
|
224
|
+
"Accept": "application/json",
|
|
225
|
+
**({"Content-Type": content_type} if content_type else {}),
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
return response.json()["data"]
|
|
229
|
+
|
|
230
|
+
def initiate_upload(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
231
|
+
return self._transport.request_json("POST", "/files/uploads", json=dict(body))["data"]
|
|
232
|
+
|
|
233
|
+
def upload_part(self, upload_id: str, part_number: int, content: bytes | str) -> dict[str, Any]:
|
|
234
|
+
response = self._transport.request_raw(
|
|
235
|
+
"PUT",
|
|
236
|
+
f"/files/uploads/{enc(upload_id)}/{part_number}",
|
|
237
|
+
content=content,
|
|
238
|
+
headers={"Accept": "application/json"},
|
|
239
|
+
)
|
|
240
|
+
return response.json()["data"]
|
|
241
|
+
|
|
242
|
+
def complete_upload(self, upload_id: str) -> dict[str, Any]:
|
|
243
|
+
return self._transport.request_json(
|
|
244
|
+
"POST",
|
|
245
|
+
f"/files/uploads/{enc(upload_id)}/complete",
|
|
246
|
+
)["data"]
|
|
247
|
+
|
|
248
|
+
def abort_upload(self, upload_id: str) -> None:
|
|
249
|
+
self._transport.request_raw("DELETE", f"/files/uploads/{enc(upload_id)}")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
class ProjectsResource:
|
|
253
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
254
|
+
self._transport = transport
|
|
255
|
+
|
|
256
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
257
|
+
return self._transport.request_json("GET", "/projects", params=query)
|
|
258
|
+
|
|
259
|
+
def create(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
260
|
+
return self._transport.request_json("POST", "/projects", json=dict(body))["data"]
|
|
261
|
+
|
|
262
|
+
def get(self, project: str) -> dict[str, Any]:
|
|
263
|
+
return self._transport.request_json("GET", f"/projects/{enc(project)}")["data"]
|
|
264
|
+
|
|
265
|
+
def update(self, project: str, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
266
|
+
return self._transport.request_json(
|
|
267
|
+
"PATCH",
|
|
268
|
+
f"/projects/{enc(project)}",
|
|
269
|
+
json=dict(body),
|
|
270
|
+
)["data"]
|
|
271
|
+
|
|
272
|
+
def delete(self, project: str) -> None:
|
|
273
|
+
self._transport.request_raw("DELETE", f"/projects/{enc(project)}")
|
|
274
|
+
|
|
275
|
+
def activate(self, project: str) -> dict[str, Any]:
|
|
276
|
+
return self._transport.request_json("POST", f"/projects/{enc(project)}/activate")["data"]
|
|
277
|
+
|
|
278
|
+
def deactivate(self, project: str) -> dict[str, Any]:
|
|
279
|
+
return self._transport.request_json("POST", f"/projects/{enc(project)}/deactivate")[
|
|
280
|
+
"data"
|
|
281
|
+
]
|
|
282
|
+
|
|
283
|
+
def event_handlers(self, project: str) -> dict[str, Any]:
|
|
284
|
+
return self._transport.request_json("GET", f"/projects/{enc(project)}/event-handlers")
|
|
285
|
+
|
|
286
|
+
def schedules(self, project: str) -> dict[str, Any]:
|
|
287
|
+
return self._transport.request_json("GET", f"/projects/{enc(project)}/schedules")
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class BuildsResource:
|
|
291
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
292
|
+
self._transport = transport
|
|
293
|
+
|
|
294
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
295
|
+
return self._transport.request_json("GET", "/builds", params=query)
|
|
296
|
+
|
|
297
|
+
def list_for_project(self, project: str, **query: Any) -> dict[str, Any]:
|
|
298
|
+
return self._transport.request_json(
|
|
299
|
+
"GET",
|
|
300
|
+
f"/projects/{enc(project)}/builds",
|
|
301
|
+
params=query,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def get(self, project: str, build_id: str) -> dict[str, Any]:
|
|
305
|
+
return self._transport.request_json(
|
|
306
|
+
"GET",
|
|
307
|
+
f"/projects/{enc(project)}/builds/{enc(build_id)}",
|
|
308
|
+
)["data"]
|
|
309
|
+
|
|
310
|
+
def deployed(self, project: str) -> dict[str, Any]:
|
|
311
|
+
return self._transport.request_json(
|
|
312
|
+
"GET",
|
|
313
|
+
f"/projects/{enc(project)}/builds/deployed",
|
|
314
|
+
)["data"]
|
|
315
|
+
|
|
316
|
+
def live(self, project: str) -> dict[str, Any]:
|
|
317
|
+
return self._transport.request_json("GET", f"/projects/{enc(project)}/builds/live")[
|
|
318
|
+
"data"
|
|
319
|
+
]
|
|
320
|
+
|
|
321
|
+
def upload(
|
|
322
|
+
self,
|
|
323
|
+
project: str,
|
|
324
|
+
*,
|
|
325
|
+
file: Any,
|
|
326
|
+
hash: str,
|
|
327
|
+
build_id: str | None = None,
|
|
328
|
+
) -> dict[str, Any]:
|
|
329
|
+
data = {"hash": hash, **({"build_id": build_id} if build_id else {})}
|
|
330
|
+
response = self._transport.request_raw(
|
|
331
|
+
"POST",
|
|
332
|
+
f"/projects/{enc(project)}/builds",
|
|
333
|
+
data=data,
|
|
334
|
+
files={"file": file},
|
|
335
|
+
headers={"Accept": "application/json"},
|
|
336
|
+
)
|
|
337
|
+
return response.json()["data"]
|
|
338
|
+
|
|
339
|
+
def download(self, project: str, build_id: str):
|
|
340
|
+
return self._transport.request_raw(
|
|
341
|
+
"GET",
|
|
342
|
+
f"/projects/{enc(project)}/builds/{enc(build_id)}/download",
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
def deploy(self, project: str, build_id: str) -> dict[str, Any]:
|
|
346
|
+
return self._transport.request_json(
|
|
347
|
+
"POST",
|
|
348
|
+
f"/projects/{enc(project)}/builds/{enc(build_id)}/deploy",
|
|
349
|
+
)["data"]
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
class ContextResource:
|
|
353
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
354
|
+
self._transport = transport
|
|
355
|
+
|
|
356
|
+
def list(self, project: str) -> dict[str, Any]:
|
|
357
|
+
return self._transport.request_json("GET", f"/projects/{enc(project)}/context")
|
|
358
|
+
|
|
359
|
+
def create(self, project: str, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
360
|
+
return self._transport.request_json(
|
|
361
|
+
"POST",
|
|
362
|
+
f"/projects/{enc(project)}/context",
|
|
363
|
+
json=dict(body),
|
|
364
|
+
)["data"]
|
|
365
|
+
|
|
366
|
+
def update(self, project: str, key: str, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
367
|
+
return self._transport.request_json(
|
|
368
|
+
"PUT",
|
|
369
|
+
f"/projects/{enc(project)}/context/{enc(key)}",
|
|
370
|
+
json=dict(body),
|
|
371
|
+
)["data"]
|
|
372
|
+
|
|
373
|
+
def delete(self, project: str, key: str) -> None:
|
|
374
|
+
self._transport.request_raw("DELETE", f"/projects/{enc(project)}/context/{enc(key)}")
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
class DomainsResource:
|
|
378
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
379
|
+
self._transport = transport
|
|
380
|
+
|
|
381
|
+
def create(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
382
|
+
return self._transport.request_json("POST", "/domains", json=dict(body))["data"]
|
|
383
|
+
|
|
384
|
+
def list(self) -> dict[str, Any]:
|
|
385
|
+
return self._transport.request_json("GET", "/domains")
|
|
386
|
+
|
|
387
|
+
def get(self, domain_id: str) -> dict[str, Any]:
|
|
388
|
+
return self._transport.request_json("GET", f"/domains/{enc(domain_id)}")["data"]
|
|
389
|
+
|
|
390
|
+
def delete(self, domain_id: str) -> None:
|
|
391
|
+
self._transport.request_raw("DELETE", f"/domains/{enc(domain_id)}")
|
|
392
|
+
|
|
393
|
+
def verify(self, domain_id: str) -> dict[str, Any]:
|
|
394
|
+
return self._transport.request_json("POST", f"/domains/{enc(domain_id)}/verify")["data"]
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
class SessionsResource:
|
|
398
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
399
|
+
self._transport = transport
|
|
400
|
+
|
|
401
|
+
def create(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
402
|
+
return self._transport.request_json("POST", "/sessions", json=dict(body))["data"]
|
|
403
|
+
|
|
404
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
405
|
+
return self._transport.request_json("GET", "/sessions", params=query)
|
|
406
|
+
|
|
407
|
+
def revoke(self, session_id: str) -> None:
|
|
408
|
+
self._transport.request_raw("DELETE", f"/sessions/{enc(session_id)}")
|
|
409
|
+
|
|
410
|
+
def revoke_all(self) -> dict[str, Any]:
|
|
411
|
+
return self._transport.request_json("DELETE", "/sessions")["data"]
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
class ServiceKeysResource:
|
|
415
|
+
def __init__(self, transport: SyncTransport) -> None:
|
|
416
|
+
self._transport = transport
|
|
417
|
+
|
|
418
|
+
def create(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
419
|
+
return self._transport.request_json("POST", "/service-keys", json=dict(body))["data"]
|
|
420
|
+
|
|
421
|
+
def list(self, **query: Any) -> dict[str, Any]:
|
|
422
|
+
return self._transport.request_json("GET", "/service-keys", params=query)
|
|
423
|
+
|
|
424
|
+
def get(self, service_key_id: str) -> dict[str, Any]:
|
|
425
|
+
return self._transport.request_json(
|
|
426
|
+
"GET",
|
|
427
|
+
f"/service-keys/{enc(service_key_id)}",
|
|
428
|
+
)["data"]
|
|
429
|
+
|
|
430
|
+
def update(self, service_key_id: str, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
431
|
+
return self._transport.request_json(
|
|
432
|
+
"PATCH",
|
|
433
|
+
f"/service-keys/{enc(service_key_id)}",
|
|
434
|
+
json=dict(body),
|
|
435
|
+
)["data"]
|
|
436
|
+
|
|
437
|
+
def revoke(self, service_key_id: str) -> None:
|
|
438
|
+
self._transport.request_raw("DELETE", f"/service-keys/{enc(service_key_id)}")
|
|
439
|
+
|
|
440
|
+
def revoke_all(self) -> dict[str, Any]:
|
|
441
|
+
return self._transport.request_json("DELETE", "/service-keys")["data"]
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def iter_stream(
|
|
445
|
+
transport: SyncTransport,
|
|
446
|
+
method: str,
|
|
447
|
+
path: str,
|
|
448
|
+
*,
|
|
449
|
+
json: Mapping[str, Any] | None = None,
|
|
450
|
+
params: Mapping[str, Any] | None = None,
|
|
451
|
+
) -> Iterator[StreamEvent]:
|
|
452
|
+
with transport.client.stream(
|
|
453
|
+
method,
|
|
454
|
+
f"{transport.api_base_url}{path}",
|
|
455
|
+
headers=transport.headers(
|
|
456
|
+
{
|
|
457
|
+
"Accept": "text/event-stream",
|
|
458
|
+
**({"Content-Type": "application/json"} if json is not None else {}),
|
|
459
|
+
}
|
|
460
|
+
),
|
|
461
|
+
json=dict(json) if json is not None else None,
|
|
462
|
+
params=without_none(params),
|
|
463
|
+
) as response:
|
|
464
|
+
raise_for_status(response)
|
|
465
|
+
buffer = ""
|
|
466
|
+
for chunk in response.iter_text():
|
|
467
|
+
buffer += chunk
|
|
468
|
+
events, buffer = consume_sse_blocks(buffer)
|
|
469
|
+
yield from events
|
|
470
|
+
events, _ = consume_sse_blocks(f"{buffer}\n\n")
|
|
471
|
+
yield from events
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
class AsyncEventsResource:
|
|
475
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
476
|
+
self._transport = transport
|
|
477
|
+
|
|
478
|
+
async def publish(self, body: Mapping[str, Any]) -> dict[str, Any]:
|
|
479
|
+
return (await self._transport.request_json("POST", "/events", json=dict(body)))["data"]
|
|
480
|
+
|
|
481
|
+
async def list(self, **query: Any) -> dict[str, Any]:
|
|
482
|
+
return await self._transport.request_json("GET", "/events", params=query)
|
|
483
|
+
|
|
484
|
+
async def get(self, event_id: str) -> dict[str, Any]:
|
|
485
|
+
return (await self._transport.request_json("GET", f"/events/{enc(event_id)}"))["data"]
|
|
486
|
+
|
|
487
|
+
async def get_runs(self, event_id: str) -> dict[str, Any]:
|
|
488
|
+
return await self._transport.request_json("GET", f"/events/{enc(event_id)}/runs")
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
class AsyncStreamsResource:
|
|
492
|
+
def __init__(self, transport: AsyncTransport) -> None:
|
|
493
|
+
self._transport = transport
|
|
494
|
+
|
|
495
|
+
async def subscribe(
|
|
496
|
+
self,
|
|
497
|
+
stream_id: str,
|
|
498
|
+
*,
|
|
499
|
+
project: str | None = None,
|
|
500
|
+
) -> AsyncIterator[StreamEvent]:
|
|
501
|
+
async for event in aiter_stream(
|
|
502
|
+
self._transport,
|
|
503
|
+
"GET",
|
|
504
|
+
f"/streams/{enc(stream_id)}/subscribe",
|
|
505
|
+
params={"project": project},
|
|
506
|
+
):
|
|
507
|
+
yield event
|
|
508
|
+
|
|
509
|
+
async def subscribe_with_event(
|
|
510
|
+
self,
|
|
511
|
+
body: Mapping[str, Any],
|
|
512
|
+
*,
|
|
513
|
+
project: str | None = None,
|
|
514
|
+
reconnect: bool = True,
|
|
515
|
+
max_attempts: int = 5,
|
|
516
|
+
) -> AsyncIterator[StreamEvent]:
|
|
517
|
+
"""Async variant of :meth:`StreamsResource.subscribe_with_event`."""
|
|
518
|
+
if not reconnect:
|
|
519
|
+
async for event in self._subscribe_with_event_once(body, project=project):
|
|
520
|
+
yield event
|
|
521
|
+
return
|
|
522
|
+
|
|
523
|
+
seen_start: set[str] = set()
|
|
524
|
+
seen_terminal: set[str] = set()
|
|
525
|
+
stream_id: str | None = None
|
|
526
|
+
attempts = 0
|
|
527
|
+
|
|
528
|
+
while True:
|
|
529
|
+
if stream_id is None:
|
|
530
|
+
source = self._subscribe_with_event_once(body, project=project)
|
|
531
|
+
else:
|
|
532
|
+
source = self.subscribe(stream_id, project=project)
|
|
533
|
+
|
|
534
|
+
terminal = False
|
|
535
|
+
try:
|
|
536
|
+
async for event in source:
|
|
537
|
+
event_type = event.get("type") if isinstance(event, dict) else None
|
|
538
|
+
|
|
539
|
+
if event_type == "event:published":
|
|
540
|
+
published = event.get("stream_id") if isinstance(event, dict) else None
|
|
541
|
+
if isinstance(published, str):
|
|
542
|
+
stream_id = published
|
|
543
|
+
|
|
544
|
+
if event_type == "run:start":
|
|
545
|
+
run_id = _run_id_of(event)
|
|
546
|
+
if run_id and run_id in seen_start:
|
|
547
|
+
continue
|
|
548
|
+
if run_id:
|
|
549
|
+
seen_start.add(run_id)
|
|
550
|
+
|
|
551
|
+
if event_type in ("run:stop", "run:fail", "run:cancel"):
|
|
552
|
+
run_id = _run_id_of(event)
|
|
553
|
+
if run_id and run_id in seen_terminal:
|
|
554
|
+
continue
|
|
555
|
+
if run_id:
|
|
556
|
+
seen_terminal.add(run_id)
|
|
557
|
+
terminal = True
|
|
558
|
+
|
|
559
|
+
yield event
|
|
560
|
+
except Exception:
|
|
561
|
+
if stream_id is None or attempts >= max_attempts:
|
|
562
|
+
raise
|
|
563
|
+
|
|
564
|
+
if terminal:
|
|
565
|
+
return
|
|
566
|
+
if stream_id is None:
|
|
567
|
+
raise RuntimeError("stream ended before event:published was received")
|
|
568
|
+
|
|
569
|
+
attempts += 1
|
|
570
|
+
await asyncio.sleep(min(0.25 * attempts, 2.0))
|
|
571
|
+
|
|
572
|
+
async def _subscribe_with_event_once(
|
|
573
|
+
self,
|
|
574
|
+
body: Mapping[str, Any],
|
|
575
|
+
*,
|
|
576
|
+
project: str | None = None,
|
|
577
|
+
) -> AsyncIterator[StreamEvent]:
|
|
578
|
+
async for event in aiter_stream(
|
|
579
|
+
self._transport,
|
|
580
|
+
"POST",
|
|
581
|
+
"/streams/subscribe-with-event",
|
|
582
|
+
json=dict(body),
|
|
583
|
+
params={"project": project},
|
|
584
|
+
):
|
|
585
|
+
yield event
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
async def aiter_stream(
|
|
589
|
+
transport: AsyncTransport,
|
|
590
|
+
method: str,
|
|
591
|
+
path: str,
|
|
592
|
+
*,
|
|
593
|
+
json: Mapping[str, Any] | None = None,
|
|
594
|
+
params: Mapping[str, Any] | None = None,
|
|
595
|
+
) -> AsyncIterator[StreamEvent]:
|
|
596
|
+
async with transport.client.stream(
|
|
597
|
+
method,
|
|
598
|
+
f"{transport.api_base_url}{path}",
|
|
599
|
+
headers=transport.headers(
|
|
600
|
+
{
|
|
601
|
+
"Accept": "text/event-stream",
|
|
602
|
+
**({"Content-Type": "application/json"} if json is not None else {}),
|
|
603
|
+
}
|
|
604
|
+
),
|
|
605
|
+
json=dict(json) if json is not None else None,
|
|
606
|
+
params=without_none(params),
|
|
607
|
+
) as response:
|
|
608
|
+
raise_for_status(response)
|
|
609
|
+
buffer = ""
|
|
610
|
+
async for chunk in response.aiter_text():
|
|
611
|
+
buffer += chunk
|
|
612
|
+
events, buffer = consume_sse_blocks(buffer)
|
|
613
|
+
for event in events:
|
|
614
|
+
yield event
|
|
615
|
+
events, _ = consume_sse_blocks(f"{buffer}\n\n")
|
|
616
|
+
for event in events:
|
|
617
|
+
yield event
|
hot/sse.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import Iterable, Iterator
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from .types import StreamEvent
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def consume_sse_blocks(buffer: str) -> tuple[list[StreamEvent], str]:
|
|
11
|
+
events: list[StreamEvent] = []
|
|
12
|
+
remaining = buffer
|
|
13
|
+
|
|
14
|
+
while "\n\n" in remaining:
|
|
15
|
+
block, remaining = remaining.split("\n\n", 1)
|
|
16
|
+
data_payload = ""
|
|
17
|
+
has_data = False
|
|
18
|
+
|
|
19
|
+
for line in block.splitlines():
|
|
20
|
+
if line.startswith("data: "):
|
|
21
|
+
data_payload += line[6:]
|
|
22
|
+
has_data = True
|
|
23
|
+
elif line.startswith("data:"):
|
|
24
|
+
data_payload += line[5:]
|
|
25
|
+
has_data = True
|
|
26
|
+
|
|
27
|
+
if not has_data:
|
|
28
|
+
continue
|
|
29
|
+
|
|
30
|
+
try:
|
|
31
|
+
parsed: Any = json.loads(data_payload)
|
|
32
|
+
except ValueError:
|
|
33
|
+
continue
|
|
34
|
+
|
|
35
|
+
if isinstance(parsed, dict) and isinstance(parsed.get("type"), str):
|
|
36
|
+
events.append(parsed)
|
|
37
|
+
|
|
38
|
+
return events, remaining
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def iter_sse_events(lines: Iterable[str]) -> Iterator[StreamEvent]:
|
|
42
|
+
buffer = ""
|
|
43
|
+
for line in lines:
|
|
44
|
+
buffer += line
|
|
45
|
+
if not line.endswith("\n"):
|
|
46
|
+
buffer += "\n"
|
|
47
|
+
events, buffer = consume_sse_blocks(buffer)
|
|
48
|
+
yield from events
|
|
49
|
+
|
|
50
|
+
events, _ = consume_sse_blocks(f"{buffer}\n\n")
|
|
51
|
+
yield from events
|
hot/types.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
from typing import Any, TypeAlias
|
|
5
|
+
|
|
6
|
+
JsonValue: TypeAlias = None | bool | int | float | str | list["JsonValue"] | dict[str, "JsonValue"]
|
|
7
|
+
JsonObject: TypeAlias = dict[str, JsonValue]
|
|
8
|
+
ApiResponse: TypeAlias = dict[str, Any]
|
|
9
|
+
ApiListResponse: TypeAlias = dict[str, Any]
|
|
10
|
+
StreamEvent: TypeAlias = dict[str, Any]
|
|
11
|
+
StreamEvents: TypeAlias = Iterator[StreamEvent]
|