Skip to content

Commit 8536361

Browse files
committed
Add log streaming (follow=True) to async client
1 parent c127d87 commit 8536361

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/toq/_client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ def status(self) -> dict:
143143
def shutdown(self, graceful: bool = True) -> None:
144144
self._request("POST", "/v1/daemon/shutdown", json={"graceful": graceful})
145145

146-
def logs(self) -> list:
146+
def logs(self, follow: bool = False) -> list:
147+
if follow:
148+
raise ToqError("Use connect_async() for log streaming")
147149
return self._request("GET", "/v1/logs").json()["entries"]
148150

149151
def clear_logs(self) -> None:
@@ -349,9 +351,20 @@ async def status(self) -> dict:
349351
async def shutdown(self, graceful: bool = True) -> None:
350352
await self._request("POST", "/v1/daemon/shutdown", json={"graceful": graceful})
351353

352-
async def logs(self) -> list:
354+
async def logs(self, follow: bool = False) -> Any:
355+
"""Get log entries. With follow=True, returns an async iterator of entries."""
356+
if follow:
357+
return self._follow_logs()
353358
return (await self._request("GET", "/v1/logs")).json()["entries"]
354359

360+
async def _follow_logs(self) -> AsyncIterator[dict]:
361+
async with httpx_sse.aconnect_sse(
362+
self._http, "GET", "/v1/logs", params={"follow": "true"}
363+
) as source:
364+
async for event in source.aiter_sse():
365+
if event.data:
366+
yield json.loads(event.data)
367+
355368
async def clear_logs(self) -> None:
356369
await self._request("DELETE", "/v1/logs")
357370

0 commit comments

Comments
 (0)