Skip to content

Commit 73f7275

Browse files
committed
refactor(sdks): add api version to base url
1 parent 54ad4e0 commit 73f7275

File tree

8 files changed

+24
-14
lines changed

8 files changed

+24
-14
lines changed

sdks/sandbox/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/config/ConnectionConfig.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ConnectionConfig private constructor(
5050
private const val ENV_DOMAIN = "OPEN_SANDBOX_DOMAIN"
5151

5252
private const val DEFAULT_USER_AGENT = "OpenSandbox-Kotlin-SDK/1.0.1"
53+
private const val API_VERSION = "v1"
5354

5455
@JvmStatic
5556
fun builder(): Builder = Builder()
@@ -69,7 +70,7 @@ class ConnectionConfig private constructor(
6970
if (currentDomain.startsWith("http://") || currentDomain.startsWith("https://")) {
7071
return currentDomain
7172
}
72-
return "$protocol://$currentDomain"
73+
return "$protocol://$currentDomain/$API_VERSION"
7374
}
7475

7576
/**

sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/infrastructure/adapters/service/SandboxesAdapterTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SandboxesAdapterTest {
9494
// Verify request
9595
val request = mockWebServer.takeRequest()
9696
assertEquals("POST", request.method)
97-
assertEquals("/sandboxes", request.path)
97+
assertEquals("/v1/sandboxes", request.path)
9898
val requestBody = request.body.readUtf8()
9999
assertTrue(requestBody.isNotBlank(), "request body should not be blank")
100100

@@ -140,7 +140,7 @@ class SandboxesAdapterTest {
140140
assertEquals("ubuntu:latest", result.image.image)
141141

142142
val request = mockWebServer.takeRequest()
143-
assertEquals("/sandboxes/$sandboxId", request.path)
143+
assertEquals("/v1/sandboxes/$sandboxId", request.path)
144144
}
145145

146146
@Test

sdks/sandbox/python/src/opensandbox/adapters/converter/execution_converter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def to_api_run_command_request(command: str, opts: RunCommandOpts) -> ApiRunComm
5050
if opts.working_directory:
5151
cwd = opts.working_directory
5252

53-
# Convert background. Domain uses bool (default False). When false, omit it from the API request.
5453
background = UNSET
5554
if opts.background:
5655
background = opts.background

sdks/sandbox/python/src/opensandbox/config/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class ConnectionConfig(BaseModel):
8282
_ENV_API_KEY = "OPEN_SANDBOX_API_KEY"
8383
_ENV_DOMAIN = "OPEN_SANDBOX_DOMAIN"
8484
_DEFAULT_DOMAIN = "localhost:8080"
85+
_API_VERSION = "v1"
8586

8687
def model_post_init(self, __context: object) -> None:
8788
# If the user explicitly provided `transport`, the SDK must not close it.
@@ -128,10 +129,10 @@ def get_domain(self) -> str:
128129

129130
def get_base_url(self) -> str:
130131
"""Get the full base URL for API requests."""
131-
current_domain = self.get_domain()
132+
domain = self.get_domain()
132133
# Allow domain to override protocol if it explicitly starts with a scheme
133-
if current_domain.startswith("http://") or current_domain.startswith(
134+
if domain.startswith("http://") or domain.startswith(
134135
"https://"
135136
):
136-
return current_domain
137-
return f"{self.protocol}://{current_domain}"
137+
return f"{domain}/{self._API_VERSION}"
138+
return f"{self.protocol}://{domain}/{self._API_VERSION}"

sdks/sandbox/python/src/opensandbox/config/connection_sync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ConnectionConfigSync(BaseModel):
6868
_ENV_API_KEY = "OPEN_SANDBOX_API_KEY"
6969
_ENV_DOMAIN = "OPEN_SANDBOX_DOMAIN"
7070
_DEFAULT_DOMAIN = "localhost:8080"
71+
_API_VERSION = "v1"
7172

7273
def model_post_init(self, __context: object) -> None:
7374
self._owns_transport = "transport" not in self.model_fields_set
@@ -105,5 +106,5 @@ def get_domain(self) -> str:
105106
def get_base_url(self) -> str:
106107
domain = self.get_domain()
107108
if domain.startswith("http://") or domain.startswith("https://"):
108-
return domain
109-
return f"{self.protocol}://{domain}"
109+
return f"{domain}/{self._API_VERSION}"
110+
return f"{self.protocol}://{domain}/{self._API_VERSION}"

sdks/sandbox/python/tests/test_connection_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def test_protocol_validation() -> None:
2929

3030
def test_get_base_url_with_domain_and_protocol() -> None:
3131
cfg = ConnectionConfig(domain="example.com:1234", protocol="https")
32-
assert cfg.get_base_url() == "https://example.com:1234"
32+
assert cfg.get_base_url() == "https://example.com:1234/v1"
3333

3434

3535
def test_get_base_url_domain_can_include_scheme() -> None:
3636
cfg = ConnectionConfig(domain="https://example.com:9999", protocol="http")
37-
assert cfg.get_base_url() == "https://example.com:9999"
37+
assert cfg.get_base_url() == "https://example.com:9999/v1"
3838

3939

4040
@pytest.mark.asyncio

sdks/sandbox/python/tests/test_converters_and_error_handling.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def test_execution_converter_to_api_run_command_request() -> None:
102102
)
103103
d2 = api2.to_dict()
104104
assert d2["cwd"] == "/tmp"
105-
assert d2.get("background", UNSET) is not UNSET
105+
# background defaults to False in domain opts; when False we omit it from the API request.
106+
assert d2.get("background", UNSET) is UNSET
106107

107108

108109
def test_filesystem_and_metrics_converters() -> None:

sdks/sandbox/python/tests/test_sandbox_service_adapter_lifecycle.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,15 @@ async def test_renew_sandbox_expiration_sends_timezone_aware(monkeypatch: pytest
197197
captured = {}
198198

199199
async def _fake_asyncio_detailed(*, client, sandbox_id, body):
200+
from opensandbox.api.lifecycle.models.renew_sandbox_expiration_response import (
201+
RenewSandboxExpirationResponse,
202+
)
203+
200204
captured["expires_at"] = body.expires_at
201-
return _Resp(status_code=204, parsed=None)
205+
return _Resp(
206+
status_code=200,
207+
parsed=RenewSandboxExpirationResponse(expires_at=body.expires_at),
208+
)
202209

203210
monkeypatch.setattr(
204211
"opensandbox.api.lifecycle.api.sandboxes.post_sandboxes_sandbox_id_renew_expiration.asyncio_detailed",

0 commit comments

Comments
 (0)