microcld 0.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.
microcld/aio.py ADDED
@@ -0,0 +1,307 @@
1
+ """Async client and sandbox - the same methods as in `client`, with `await`.
2
+
3
+ async with await AsyncSandbox.create(template="python") as sb:
4
+ r = await sb.exec("python3 -c 'print(2+2)'")
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import contextlib
10
+ from typing import AsyncIterator, Callable, Mapping, Sequence
11
+
12
+ import httpx
13
+
14
+ from . import _api
15
+ from ._types import CodeError, CodeResult, ExecResult, OutputChunk, SandboxInfo, UsageReport
16
+ from .errors import SandboxError, ServerError, from_response
17
+ from .hosting import Account, Apps, Buckets, Databases
18
+
19
+
20
+ class AsyncClient:
21
+ def __init__(
22
+ self,
23
+ api_key: str | None = None,
24
+ base_url: str | None = None,
25
+ timeout: float = 60.0,
26
+ transport: httpx.AsyncBaseTransport | None = None,
27
+ ):
28
+ key, url = _api.config(api_key, base_url)
29
+ self._http = httpx.AsyncClient(
30
+ base_url=url,
31
+ headers={"Authorization": f"Bearer {key}"},
32
+ timeout=timeout,
33
+ transport=transport,
34
+ )
35
+ self.apps = Apps(self)
36
+ self.databases = Databases(self)
37
+ self.buckets = Buckets(self)
38
+ self.account = Account(self)
39
+
40
+ async def _run(self, req: _api.Req, parse):
41
+ return parse(await self.request(req))
42
+
43
+ async def request(self, req: _api.Req) -> httpx.Response:
44
+ try:
45
+ resp = await self._http.request(
46
+ req.method,
47
+ req.path,
48
+ json=req.json,
49
+ params=req.params,
50
+ content=req.content,
51
+ timeout=req.timeout if req.timeout is not None else httpx.USE_CLIENT_DEFAULT,
52
+ )
53
+ except httpx.HTTPError as e:
54
+ raise ServerError(f"API unavailable: {e}") from e
55
+ if resp.is_error:
56
+ raise from_response(resp.status_code, resp.content)
57
+ return resp
58
+
59
+ @contextlib.asynccontextmanager
60
+ async def stream(self, req: _api.Req) -> AsyncIterator[httpx.Response]:
61
+ try:
62
+ async with self._http.stream(
63
+ req.method,
64
+ req.path,
65
+ json=req.json,
66
+ timeout=req.timeout if req.timeout is not None else httpx.USE_CLIENT_DEFAULT,
67
+ ) as resp:
68
+ if resp.is_error:
69
+ await resp.aread()
70
+ raise from_response(resp.status_code, resp.content)
71
+ yield resp
72
+ except httpx.HTTPError as e:
73
+ raise ServerError(f"API unavailable: {e}") from e
74
+
75
+ async def create(
76
+ self,
77
+ template: str | None = None,
78
+ *,
79
+ cpus: int | None = None,
80
+ memory_mb: int | None = None,
81
+ disk_gb: int | None = None,
82
+ idle_timeout_secs: int | None = None,
83
+ port: int | None = None,
84
+ egress: _api.Egress | None = None,
85
+ ttl_secs: int | None = None,
86
+ bucket: str | None = None,
87
+ bucket_mode: str | None = None,
88
+ ) -> "AsyncSandbox":
89
+ req = _api.create(
90
+ template, cpus, memory_mb, disk_gb, idle_timeout_secs, port, egress, ttl_secs, bucket, bucket_mode
91
+ )
92
+ return AsyncSandbox(self, SandboxInfo.from_json((await self.request(req)).json()))
93
+
94
+ async def get(self, id: str) -> "AsyncSandbox":
95
+ return AsyncSandbox(self, SandboxInfo.from_json((await self.request(_api.get(id))).json()))
96
+
97
+ async def list(self) -> list["AsyncSandbox"]:
98
+ return [AsyncSandbox(self, SandboxInfo.from_json(d)) for d in (await self.request(_api.list_())).json()]
99
+
100
+ async def usage(self, start: str | None = None, end: str | None = None) -> UsageReport:
101
+ return UsageReport.from_json((await self.request(_api.usage(start, end))).json())
102
+
103
+ async def aclose(self) -> None:
104
+ await self._http.aclose()
105
+
106
+ async def __aenter__(self) -> "AsyncClient":
107
+ return self
108
+
109
+ async def __aexit__(self, *exc: object) -> None:
110
+ await self.aclose()
111
+
112
+
113
+ class AsyncExecStream:
114
+ """`async for chunk in sb.exec_stream(...)`; then `.result`."""
115
+
116
+ def __init__(self, client: AsyncClient, req: _api.Req):
117
+ self._client = client
118
+ self._req = req
119
+ self.result: ExecResult | None = None
120
+
121
+ async def __aiter__(self) -> AsyncIterator[OutputChunk]:
122
+ acc = _api.StreamAcc()
123
+ async with self._client.stream(self._req) as resp:
124
+ async for line in resp.aiter_lines():
125
+ if line and (chunk := acc.feed(line)) is not None:
126
+ yield chunk
127
+ self.result = acc.finish()
128
+
129
+
130
+ class AsyncFiles:
131
+ def __init__(self, sb: "AsyncSandbox"):
132
+ self._sb = sb
133
+
134
+ async def write(self, path: str, data: bytes | str, mode: int | None = None) -> None:
135
+ await self._sb._client.request(_api.write_file(self._sb.id, path, _api.as_bytes(data), mode))
136
+
137
+ async def read(self, path: str) -> bytes:
138
+ return (await self._sb._client.request(_api.read_file(self._sb.id, path))).content
139
+
140
+ async def read_text(self, path: str, encoding: str = "utf-8") -> str:
141
+ return (await self.read(path)).decode(encoding)
142
+
143
+
144
+ class AsyncSandbox:
145
+ def __init__(self, client: AsyncClient, info: SandboxInfo, *, owns_client: bool = False):
146
+ self._client = client
147
+ self._owns_client = owns_client
148
+ self.info = info
149
+ self.files = AsyncFiles(self)
150
+
151
+ @classmethod
152
+ async def create(
153
+ cls,
154
+ template: str | None = None,
155
+ *,
156
+ cpus: int | None = None,
157
+ memory_mb: int | None = None,
158
+ disk_gb: int | None = None,
159
+ idle_timeout_secs: int | None = None,
160
+ port: int | None = None,
161
+ egress: _api.Egress | None = None,
162
+ ttl_secs: int | None = None,
163
+ bucket: str | None = None,
164
+ bucket_mode: str | None = None,
165
+ api_key: str | None = None,
166
+ base_url: str | None = None,
167
+ client: AsyncClient | None = None,
168
+ ) -> "AsyncSandbox":
169
+ own = client is None
170
+ c = client or AsyncClient(api_key, base_url)
171
+ try:
172
+ sb = await c.create(
173
+ template,
174
+ cpus=cpus,
175
+ memory_mb=memory_mb,
176
+ disk_gb=disk_gb,
177
+ idle_timeout_secs=idle_timeout_secs,
178
+ port=port,
179
+ egress=egress,
180
+ ttl_secs=ttl_secs,
181
+ bucket=bucket,
182
+ bucket_mode=bucket_mode,
183
+ )
184
+ except BaseException:
185
+ if own:
186
+ await c.aclose()
187
+ raise
188
+ sb._owns_client = own
189
+ return sb
190
+
191
+ @classmethod
192
+ async def connect(cls, id: str, *, api_key: str | None = None, base_url: str | None = None) -> "AsyncSandbox":
193
+ c = AsyncClient(api_key, base_url)
194
+ try:
195
+ sb = await c.get(id)
196
+ except BaseException:
197
+ await c.aclose()
198
+ raise
199
+ sb._owns_client = True
200
+ return sb
201
+
202
+ @property
203
+ def id(self) -> str:
204
+ return self.info.id
205
+
206
+ @property
207
+ def state(self) -> str:
208
+ return self.info.state
209
+
210
+ async def refresh(self) -> SandboxInfo:
211
+ self.info = SandboxInfo.from_json((await self._client.request(_api.get(self.id))).json())
212
+ return self.info
213
+
214
+ async def exec(
215
+ self,
216
+ cmd: str | Sequence[str],
217
+ *,
218
+ env: Mapping[str, str] | None = None,
219
+ cwd: str | None = None,
220
+ timeout: float = 60,
221
+ stdin: str | None = None,
222
+ on_stdout: Callable[[str], object] | None = None,
223
+ on_stderr: Callable[[str], object] | None = None,
224
+ ) -> ExecResult:
225
+ if on_stdout or on_stderr:
226
+ stream = self.exec_stream(cmd, env=env, cwd=cwd, timeout=timeout, stdin=stdin)
227
+ async for chunk in stream:
228
+ cb = on_stdout if chunk.stream == "stdout" else on_stderr
229
+ if cb:
230
+ cb(chunk.data)
231
+ assert stream.result is not None
232
+ return stream.result
233
+ req = _api.exec_(self.id, cmd, env, cwd, timeout, stdin)
234
+ return ExecResult.from_json((await self._client.request(req)).json())
235
+
236
+ def exec_stream(
237
+ self,
238
+ cmd: str | Sequence[str],
239
+ *,
240
+ env: Mapping[str, str] | None = None,
241
+ cwd: str | None = None,
242
+ timeout: float = 60,
243
+ stdin: str | None = None,
244
+ ) -> AsyncExecStream:
245
+ return AsyncExecStream(self._client, _api.exec_stream(self.id, cmd, env, cwd, timeout, stdin))
246
+
247
+ async def run_code(self, code: str, *, timeout: float = 60) -> CodeResult:
248
+ stdin = _api.interpreter_request(code, timeout)
249
+ r = await self.exec(_api.INTERPRETER, stdin=stdin, timeout=timeout + _api.INTERPRETER_SLACK)
250
+ d = _api.parse_interpreter(r.exit_code, r.stdout, r.stderr)
251
+ err = d.get("error")
252
+ return CodeResult(
253
+ stdout=d.get("stdout", ""),
254
+ stderr=d.get("stderr", ""),
255
+ result=d.get("result"),
256
+ error=CodeError(err["name"], err["value"], err.get("traceback", "")) if err else None,
257
+ images=[await self.files.read(p) for p in d.get("images", [])],
258
+ duration_ms=d.get("duration_ms", 0),
259
+ )
260
+
261
+ async def reset_code(self) -> None:
262
+ r = await self.exec(_api.INTERPRETER, stdin=_api.interpreter_request(None, 0, reset=True), timeout=30)
263
+ _api.parse_interpreter(r.exit_code, r.stdout, r.stderr)
264
+
265
+ async def sleep(self) -> None:
266
+ self._set((await self._client.request(_api.lifecycle(self.id, "sleep"))).json())
267
+
268
+ async def wake(self) -> None:
269
+ self._set((await self._client.request(_api.lifecycle(self.id, "wake"))).json())
270
+
271
+ async def fork(self, snapshot: str | None = None, *, ttl_secs: int | None = None) -> "AsyncSandbox":
272
+ info = SandboxInfo.from_json((await self._client.request(_api.fork(self.id, snapshot, ttl_secs))).json())
273
+ return AsyncSandbox(self._client, info)
274
+
275
+ async def snapshot(self, name: str) -> None:
276
+ await self._client.request(_api.snapshot(self.id, name))
277
+
278
+ async def set_idle_timeout(self, secs: int | None) -> None:
279
+ self._set((await self._client.request(_api.update(self.id, {"idle_timeout_secs": secs}))).json())
280
+
281
+ async def set_ttl(self, ttl_secs: int | None) -> None:
282
+ self._set((await self._client.request(_api.set_ttl(self.id, ttl_secs))).json())
283
+
284
+ async def set_egress(self, egress: _api.Egress) -> None:
285
+ self._set((await self._client.request(_api.set_egress(self.id, egress))).json())
286
+
287
+ async def delete(self) -> None:
288
+ try:
289
+ await self._client.request(_api.delete(self.id))
290
+ except SandboxError as e:
291
+ if e.status != 404:
292
+ raise
293
+ finally:
294
+ if self._owns_client:
295
+ await self._client.aclose()
296
+
297
+ def _set(self, d: dict) -> None:
298
+ self.info = SandboxInfo.from_json(d)
299
+
300
+ async def __aenter__(self) -> "AsyncSandbox":
301
+ return self
302
+
303
+ async def __aexit__(self, *exc: object) -> None:
304
+ await self.delete()
305
+
306
+ def __repr__(self) -> str:
307
+ return f"AsyncSandbox(id={self.id!r}, state={self.state!r}, template={self.info.template!r})"