Skip to content

Commit 37e38a3

Browse files
chore: inline wrapper classes (#30)
1 parent 7e5a111 commit 37e38a3

File tree

12 files changed

+244
-509
lines changed

12 files changed

+244
-509
lines changed

src/deno_sandbox/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
AsyncSandboxApi,
1111
SandboxApi,
1212
)
13-
from .console import AsyncConsoleClient, ConsoleClient
13+
from .console import AsyncConsoleClient
1414
from .options import Options, get_internal_options
1515

1616
__all__ = ["DenoDeploy", "AsyncDenoDeploy", "Options"]
@@ -21,13 +21,13 @@ def __init__(self, options: Optional[Options] = None):
2121
internal_options = get_internal_options(options)
2222
bridge = AsyncBridge()
2323

24-
client = ConsoleClient(internal_options, bridge)
25-
self.apps = Apps(client)
26-
self.revisions = Revisions(client)
27-
self.timelines = Timelines(client)
24+
client = AsyncConsoleClient(internal_options)
25+
self.apps = Apps(client, bridge)
26+
self.revisions = Revisions(client, bridge)
27+
self.timelines = Timelines(client, bridge)
2828
self.sandbox = SandboxApi(client, bridge)
29-
self.snapshots = Snapshots(client)
30-
self.volumes = Volumes(client)
29+
self.snapshots = Snapshots(client, bridge)
30+
self.volumes = Volumes(client, bridge)
3131

3232

3333
class AsyncDenoDeploy:

src/deno_sandbox/apps.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
AppUpdate,
1111
)
1212
from .utils import convert_to_snake_case
13+
from .bridge import AsyncBridge
1314
from .console import (
1415
PaginatedList,
15-
ConsoleClient,
1616
AsyncConsoleClient,
1717
AsyncPaginatedList,
1818
)
@@ -56,30 +56,31 @@ async def delete(self, app: str) -> None:
5656

5757

5858
class Apps:
59-
def __init__(self, client: ConsoleClient):
59+
def __init__(self, client: AsyncConsoleClient, bridge: AsyncBridge):
6060
self._client = client
61-
self._async = AsyncApps(client._async)
61+
self._bridge = bridge
62+
self._async = AsyncApps(client)
6263

6364
def get(self, id_or_slug: str) -> App | None:
6465
"""Get an app by its ID or slug."""
65-
return self._client._bridge.run(self._async.get(id_or_slug))
66+
return self._bridge.run(self._async.get(id_or_slug))
6667

6768
def list(
6869
self, options: Optional[AppListOptions] = None
6970
) -> PaginatedList[App, AppListOptions]:
7071
"""List apps of an org."""
7172

72-
paginated = self._client._bridge.run(self._async.list(options))
73-
return PaginatedList(self._client._bridge, paginated)
73+
paginated = self._bridge.run(self._async.list(options))
74+
return PaginatedList(self._bridge, paginated)
7475

7576
def create(self, options: Optional[AppInit] = None) -> App:
7677
"""Create a new app."""
77-
return self._client._bridge.run(self._async.create(options))
78+
return self._bridge.run(self._async.create(options))
7879

7980
def update(self, app: str, update: AppUpdate) -> App:
8081
"""Update an existing app."""
81-
return self._client._bridge.run(self._async.update(app, update))
82+
return self._bridge.run(self._async.update(app, update))
8283

8384
def delete(self, app: str) -> None:
8485
"""Delete an app by its ID or slug."""
85-
self._client._bridge.run(self._async.delete(app))
86+
self._bridge.run(self._async.delete(app))

src/deno_sandbox/console.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from __future__ import annotations
22

3-
from datetime import datetime, timezone
43
from typing import Any, Generic, Literal, Optional, TypedDict, TypeVar, cast
54
import httpx
65

76
from .api_types_generated import (
87
RevisionWithoutTimelines,
9-
SandboxListOptions,
10-
SandboxMeta,
118
Timeline,
129
)
1310
from .bridge import AsyncBridge
@@ -29,10 +26,6 @@ class ExposeSSHResult(TypedDict):
2926
port: int
3027

3128

32-
class ExposeHTTPResult(TypedDict):
33-
domain: str
34-
35-
3629
class AsyncPaginatedList(Generic[T, O]):
3730
def __init__(
3831
self,
@@ -218,82 +211,5 @@ async def __aenter__(self):
218211
async def __aexit__(self, exc_type, exc_val, exc_tb):
219212
await self.close()
220213

221-
# Sandbox-related methods (used by sandbox.py)
222-
async def _sandboxes_list(
223-
self, options: Optional[SandboxListOptions] = None
224-
) -> AsyncPaginatedList[SandboxMeta, SandboxListOptions]:
225-
sandboxes: AsyncPaginatedList[
226-
SandboxMeta, SandboxListOptions
227-
] = await self.get_paginated(
228-
path="/api/v3/sandboxes", cursor=None, params=options
229-
)
230-
return sandboxes
231-
232-
async def _kill_sandbox(self, sandbox_id: str) -> None:
233-
await self.delete(f"/api/v3/sandboxes/{sandbox_id}")
234-
235-
async def _extend_timeout(self, sandbox_id: str, stop_at_ms: int) -> datetime:
236-
url = self._options["sandbox_url"].join(f"/api/v3/sandbox/{sandbox_id}")
237-
238-
result = await self._request("PATCH", url, {"stop_at_ms": stop_at_ms})
239-
240-
data = result.json()
241-
242-
return datetime.fromtimestamp(data["stop_at_ms"] / 1000, tz=timezone.utc)
243-
244-
async def _expose_http(self, sandbox_id: str, params: dict[str, int]) -> str:
245-
url = self._options["sandbox_url"].join(
246-
f"/api/v3/sandbox/{sandbox_id}/expose/http"
247-
)
248-
249-
result = await self._request("POST", url, params)
250-
251-
data = cast(ExposeHTTPResult, result.json())
252-
return data["domain"]
253-
254-
async def _expose_ssh(self, sandbox_id: str) -> ExposeSSHResult:
255-
url = self._options["sandbox_url"].join(
256-
f"/api/v3/sandbox/{sandbox_id}/expose/ssh"
257-
)
258-
response = await self._request("POST", url, {})
259-
260-
return cast(ExposeSSHResult, response.json())
261-
262-
263-
class ConsoleClient:
264-
def __init__(self, options: InternalOptions, bridge: AsyncBridge):
265-
self._async = AsyncConsoleClient(options)
266-
self._bridge = bridge
267-
268-
def close(self):
269-
self._bridge.run(self._async.close())
270-
271-
def __enter__(self):
272-
return self
273-
274-
def __exit__(self, exc_type, exc_val, exc_tb):
275-
self._bridge.run(self._async.__aexit__(exc_type, exc_val, exc_tb))
276-
277-
# Sandbox-related methods (used by sandbox.py)
278-
def _sandboxes_list(
279-
self, options: Optional[SandboxListOptions] = None
280-
) -> PaginatedList[SandboxMeta, SandboxListOptions]:
281-
paginated: AsyncPaginatedList[SandboxMeta, SandboxListOptions] = (
282-
self._bridge.run(self._async._sandboxes_list(options))
283-
)
284-
return PaginatedList(self._bridge, paginated)
285-
286-
def _kill_sandbox(self, sandbox_id: str) -> None:
287-
self._bridge.run(self._async._kill_sandbox(sandbox_id))
288-
289-
def _extend_timeout(self, sandbox_id: str, stop_at_ms: int) -> None:
290-
self._bridge.run(self._async._extend_timeout(sandbox_id, stop_at_ms))
291-
292-
def _expose_http(self, sandbox_id: str, params: dict[str, int]) -> str:
293-
return self._bridge.run(self._async._expose_http(sandbox_id, params))
294-
295-
def _expose_ssh(self, sandbox_id: str) -> ExposeSSHResult:
296-
return self._bridge.run(self._async._expose_ssh(sandbox_id))
297-
298214

299-
__all__ = ["AsyncConsoleClient", "ConsoleClient"]
215+
__all__ = ["AsyncConsoleClient"]

src/deno_sandbox/env.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
from typing import TYPE_CHECKING
44

55
if TYPE_CHECKING:
6-
from .console import AsyncConsoleClient, ConsoleClient
7-
from .rpc import AsyncRpcClient, RpcClient
6+
from .rpc import AsyncRpcClient
7+
from .bridge import AsyncBridge
88

99

1010
class AsyncSandboxEnv:
11-
def __init__(self, client: AsyncConsoleClient, rpc: AsyncRpcClient):
12-
self._client = client
11+
def __init__(self, rpc: AsyncRpcClient):
1312
self._rpc = rpc
1413

1514
async def get(self, key: str) -> str:
@@ -42,34 +41,23 @@ async def delete(self, key: str) -> None:
4241

4342

4443
class SandboxEnv:
45-
def __init__(self, client: ConsoleClient, rpc: RpcClient):
46-
self._client = client
44+
def __init__(self, rpc: AsyncRpcClient, bridge: AsyncBridge):
4745
self._rpc = rpc
46+
self._bridge = bridge
47+
self._async = AsyncSandboxEnv(rpc)
4848

4949
def get(self, key: str) -> str:
5050
"""Get the value of an environment variable."""
51-
52-
params = {"key": key}
53-
result = self._rpc.call("envGet", params)
54-
55-
return result
51+
return self._bridge.run(self._async.get(key))
5652

5753
def set(self, key: str, value: str) -> None:
5854
"""Set the value of an environment variable."""
59-
60-
params = {"key": key, "value": value}
61-
self._rpc.call("envSet", params)
55+
self._bridge.run(self._async.set(key, value))
6256

6357
def to_object(self) -> dict[str, str]:
6458
"""Get all environment variables."""
65-
66-
params = {}
67-
result = self._rpc.call("envToObject", params)
68-
69-
return result
59+
return self._bridge.run(self._async.to_object())
7060

7161
def delete(self, key: str) -> None:
7262
"""Delete an environment variable."""
73-
74-
params = {"key": key}
75-
self._rpc.call("envDelete", params)
63+
self._bridge.run(self._async.delete(key))

0 commit comments

Comments
 (0)