Skip to content

Commit 67af5ac

Browse files
committed
chore: Add tunnel prop to object-oriented SDK
1 parent d55fceb commit 67af5ac

File tree

4 files changed

+93
-21
lines changed

4 files changed

+93
-21
lines changed

src/runloop_api_client/sdk/async_.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def create(
8282
devbox_view = await self._client.devboxes.create_and_await_running(
8383
**params,
8484
)
85-
return AsyncDevbox(self._client, devbox_view.id)
85+
return AsyncDevbox(self._client, devbox_view)
8686

8787
async def create_from_blueprint_id(
8888
self,
@@ -101,7 +101,7 @@ async def create_from_blueprint_id(
101101
blueprint_id=blueprint_id,
102102
**params,
103103
)
104-
return AsyncDevbox(self._client, devbox_view.id)
104+
return AsyncDevbox(self._client, devbox_view)
105105

106106
async def create_from_blueprint_name(
107107
self,
@@ -120,7 +120,7 @@ async def create_from_blueprint_name(
120120
blueprint_name=blueprint_name,
121121
**params,
122122
)
123-
return AsyncDevbox(self._client, devbox_view.id)
123+
return AsyncDevbox(self._client, devbox_view)
124124

125125
async def create_from_snapshot(
126126
self,
@@ -139,9 +139,9 @@ async def create_from_snapshot(
139139
snapshot_id=snapshot_id,
140140
**params,
141141
)
142-
return AsyncDevbox(self._client, devbox_view.id)
142+
return AsyncDevbox(self._client, devbox_view)
143143

144-
def from_id(self, devbox_id: str) -> AsyncDevbox:
144+
async def from_id(self, devbox_id: str) -> AsyncDevbox:
145145
"""Attach to an existing devbox by ID.
146146
147147
Returns immediately without waiting for the devbox to reach ``running``
@@ -153,7 +153,8 @@ def from_id(self, devbox_id: str) -> AsyncDevbox:
153153
:return: Wrapper bound to the requested devbox
154154
:rtype: AsyncDevbox
155155
"""
156-
return AsyncDevbox(self._client, devbox_id)
156+
devbox_view = await self._client.devboxes.retrieve(devbox_id)
157+
return AsyncDevbox(self._client, devbox_view)
157158

158159
async def list(
159160
self,
@@ -168,7 +169,7 @@ async def list(
168169
page = await self._client.devboxes.list(
169170
**params,
170171
)
171-
return [AsyncDevbox(self._client, item.id) for item in page.devboxes]
172+
return [AsyncDevbox(self._client, item) for item in page.devboxes]
172173

173174

174175
class AsyncSnapshotOps:

src/runloop_api_client/sdk/async_devbox.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ class AsyncDevbox:
6464
# Devbox is automatically shut down on exit
6565
"""
6666

67-
def __init__(self, client: AsyncRunloop, devbox_id: str) -> None:
67+
def __init__(
68+
self,
69+
client: AsyncRunloop,
70+
devbox_view: DevboxView,
71+
) -> None:
6872
"""Initialize the wrapper.
6973
7074
:param client: Generated async Runloop client
7175
:type client: AsyncRunloop
72-
:param devbox_id: Devbox identifier returned by the API
73-
:type devbox_id: str
76+
:param devbox_view: DevboxView from the API
77+
:type devbox_view: DevboxView
7478
"""
7579
self._client = client
76-
self._id = devbox_id
80+
self._id = devbox_view.id
81+
self._tunnel: Optional[TunnelView] = devbox_view.tunnel
7782
self._logger = logging.getLogger(__name__)
7883

7984
@override
@@ -104,6 +109,18 @@ def id(self) -> str:
104109
"""
105110
return self._id
106111

112+
@property
113+
def tunnel(self) -> Optional[TunnelView]:
114+
"""Return the cached tunnel info, if available.
115+
116+
This returns the tunnel info cached at creation time. For the latest
117+
tunnel state, use :meth:`get_info` or :meth:`net.view_tunnel`.
118+
119+
:return: Cached tunnel info, or None if no tunnel was enabled at creation
120+
:rtype: TunnelView | None
121+
"""
122+
return self._tunnel
123+
107124
async def get_info(
108125
self,
109126
**options: Unpack[BaseRequestOptions],
@@ -776,6 +793,24 @@ async def enable_tunnel(
776793
**params,
777794
)
778795

796+
async def view_tunnel(
797+
self,
798+
**options: Unpack[BaseRequestOptions],
799+
) -> Optional[TunnelView]:
800+
"""Retrieve the current tunnel info for this devbox, if one exists.
801+
802+
:param options: Optional request configuration
803+
:return: Current tunnel info, or None if no tunnel is enabled
804+
:rtype: TunnelView | None
805+
806+
Example:
807+
>>> tunnel = await devbox.net.view_tunnel()
808+
>>> if tunnel:
809+
... print(f"Tunnel key: {tunnel.tunnel_key}")
810+
"""
811+
info = await self._devbox.get_info(**options)
812+
return info.tunnel
813+
779814
async def remove_tunnel(
780815
self,
781816
**params: Unpack[SDKDevboxRemoveTunnelParams],

src/runloop_api_client/sdk/devbox.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,21 @@ class Devbox:
6363
# Devbox is automatically shutdown on exit
6464
"""
6565

66-
def __init__(self, client: Runloop, devbox_id: str) -> None:
66+
def __init__(
67+
self,
68+
client: Runloop,
69+
devbox_view: DevboxView,
70+
) -> None:
6771
"""Initialize the wrapper.
6872
6973
:param client: Generated Runloop client
7074
:type client: Runloop
71-
:param devbox_id: Devbox identifier returned by the API
72-
:type devbox_id: str
75+
:param devbox_view: DevboxView from the API
76+
:type devbox_view: DevboxView
7377
"""
7478
self._client = client
75-
self._id = devbox_id
79+
self._id = devbox_view.id
80+
self._tunnel: Optional[TunnelView] = devbox_view.tunnel
7681
self._logger = logging.getLogger(__name__)
7782

7883
@override
@@ -103,6 +108,18 @@ def id(self) -> str:
103108
"""
104109
return self._id
105110

111+
@property
112+
def tunnel(self) -> Optional[TunnelView]:
113+
"""Return the cached tunnel info, if available.
114+
115+
This returns the tunnel info cached at creation time. For the latest
116+
tunnel state, use :meth:`get_info` or :meth:`net.view_tunnel`.
117+
118+
:return: Cached tunnel info, or None if no tunnel was enabled at creation
119+
:rtype: TunnelView | None
120+
"""
121+
return self._tunnel
122+
106123
def get_info(
107124
self,
108125
**options: Unpack[BaseRequestOptions],
@@ -779,6 +796,24 @@ def enable_tunnel(
779796
**params,
780797
)
781798

799+
def view_tunnel(
800+
self,
801+
**options: Unpack[BaseRequestOptions],
802+
) -> Optional[TunnelView]:
803+
"""Retrieve the current tunnel info for this devbox, if one exists.
804+
805+
:param options: Optional request configuration
806+
:return: Current tunnel info, or None if no tunnel is enabled
807+
:rtype: TunnelView | None
808+
809+
Example:
810+
>>> tunnel = devbox.net.view_tunnel()
811+
>>> if tunnel:
812+
... print(f"Tunnel key: {tunnel.tunnel_key}")
813+
"""
814+
info = self._devbox.get_info(**options)
815+
return info.tunnel
816+
782817
def remove_tunnel(
783818
self,
784819
**params: Unpack[SDKDevboxRemoveTunnelParams],

src/runloop_api_client/sdk/sync.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def create(
8181
devbox_view = self._client.devboxes.create_and_await_running(
8282
**params,
8383
)
84-
return Devbox(self._client, devbox_view.id)
84+
return Devbox(self._client, devbox_view)
8585

8686
def create_from_blueprint_id(
8787
self,
@@ -100,7 +100,7 @@ def create_from_blueprint_id(
100100
blueprint_id=blueprint_id,
101101
**params,
102102
)
103-
return Devbox(self._client, devbox_view.id)
103+
return Devbox(self._client, devbox_view)
104104

105105
def create_from_blueprint_name(
106106
self,
@@ -119,7 +119,7 @@ def create_from_blueprint_name(
119119
blueprint_name=blueprint_name,
120120
**params,
121121
)
122-
return Devbox(self._client, devbox_view.id)
122+
return Devbox(self._client, devbox_view)
123123

124124
def create_from_snapshot(
125125
self,
@@ -138,7 +138,7 @@ def create_from_snapshot(
138138
snapshot_id=snapshot_id,
139139
**params,
140140
)
141-
return Devbox(self._client, devbox_view.id)
141+
return Devbox(self._client, devbox_view)
142142

143143
def from_id(self, devbox_id: str) -> Devbox:
144144
"""Attach to an existing devbox by ID.
@@ -152,7 +152,8 @@ def from_id(self, devbox_id: str) -> Devbox:
152152
:rtype: Devbox
153153
"""
154154
self._client.devboxes.await_running(devbox_id)
155-
return Devbox(self._client, devbox_id)
155+
devbox_view = self._client.devboxes.retrieve(devbox_id)
156+
return Devbox(self._client, devbox_view)
156157

157158
def list(
158159
self,
@@ -167,7 +168,7 @@ def list(
167168
page = self._client.devboxes.list(
168169
**params,
169170
)
170-
return [Devbox(self._client, item.id) for item in page.devboxes]
171+
return [Devbox(self._client, item) for item in page.devboxes]
171172

172173

173174
class SnapshotOps:

0 commit comments

Comments
 (0)