Skip to content

Commit 0f7ed88

Browse files
committed
feat: support region
1 parent 8bd706d commit 0f7ed88

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

tests/test_connection_config.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
def test_api_url_defaults_correctly(monkeypatch):
55
monkeypatch.setenv("AGENTBOX_DOMAIN", "")
6+
monkeypatch.delenv("AGENTBOX_REGION", raising=False)
67

78
config = ConnectionConfig()
8-
assert config.api_url == "https://api.sandbox.ucloudai.com"
9+
assert config.api_url == "https://api.cn-wlcb.sandbox.ucloudai.com"
910

1011

1112
def test_api_url_in_args():
@@ -25,3 +26,45 @@ def test_api_url_has_correct_priority(monkeypatch):
2526

2627
config = ConnectionConfig(api_url="http://localhost:8080")
2728
assert config.api_url == "http://localhost:8080"
29+
30+
31+
def test_region_cn_wlcb(monkeypatch):
32+
monkeypatch.delenv("AGENTBOX_DOMAIN", raising=False)
33+
monkeypatch.setenv("AGENTBOX_REGION", "cn-wlcb")
34+
35+
config = ConnectionConfig()
36+
assert config.domain == "cn-wlcb.sandbox.ucloudai.com"
37+
assert config.api_url == "https://api.cn-wlcb.sandbox.ucloudai.com"
38+
39+
40+
def test_region_us_ca(monkeypatch):
41+
monkeypatch.delenv("AGENTBOX_DOMAIN", raising=False)
42+
monkeypatch.setenv("AGENTBOX_REGION", "us-ca")
43+
44+
config = ConnectionConfig()
45+
assert config.domain == "us-ca.sandbox.ucloudai.com"
46+
assert config.api_url == "https://api.us-ca.sandbox.ucloudai.com"
47+
48+
49+
def test_domain_takes_priority_over_region(monkeypatch):
50+
monkeypatch.setenv("AGENTBOX_DOMAIN", "custom.example.com")
51+
monkeypatch.setenv("AGENTBOX_REGION", "us-ca")
52+
53+
config = ConnectionConfig()
54+
assert config.domain == "custom.example.com"
55+
assert config.api_url == "https://api.custom.example.com"
56+
57+
58+
def test_domain_arg_takes_priority_over_region(monkeypatch):
59+
monkeypatch.setenv("AGENTBOX_REGION", "us-ca")
60+
61+
config = ConnectionConfig(domain="override.example.com")
62+
assert config.domain == "override.example.com"
63+
64+
65+
def test_default_region_is_cn_wlcb(monkeypatch):
66+
monkeypatch.delenv("AGENTBOX_DOMAIN", raising=False)
67+
monkeypatch.delenv("AGENTBOX_REGION", raising=False)
68+
69+
config = ConnectionConfig()
70+
assert config.domain == "cn-wlcb.sandbox.ucloudai.com"

ucloud_sandbox/connection_config.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
KEEPALIVE_PING_INTERVAL_SEC = 50 # 50 seconds
1313
KEEPALIVE_PING_HEADER = "Keepalive-Ping-Interval"
1414

15+
REGION_DOMAIN_MAP = {
16+
"cn-wlcb": "cn-wlcb.sandbox.ucloudai.com",
17+
"us-ca": "us-ca.sandbox.ucloudai.com",
18+
}
19+
20+
DEFAULT_REGION = "cn-wlcb"
21+
1522

1623
class ApiParams(TypedDict, total=False):
1724
"""
@@ -30,7 +37,7 @@ class ApiParams(TypedDict, total=False):
3037
"""AgentBox API Key to use for authentication, defaults to `AGENTBOX_API_KEY` environment variable."""
3138

3239
domain: Optional[str]
33-
"""AgentBox domain to use, defaults to `AGENTBOX_DOMAIN` environment variable."""
40+
"""AgentBox domain to use, defaults to `AGENTBOX_DOMAIN` environment variable. If not set, derived from `AGENTBOX_REGION`."""
3441

3542
api_url: Optional[str]
3643
"""URL to use for the API, defaults to `https://api.<domain>`. For internal use only."""
@@ -52,9 +59,17 @@ class ConnectionConfig:
5259

5360
envd_port = 49983
5461

62+
@staticmethod
63+
def _region():
64+
return os.getenv("AGENTBOX_REGION") or DEFAULT_REGION
65+
5566
@staticmethod
5667
def _domain():
57-
return os.getenv("AGENTBOX_DOMAIN") or "sandbox.ucloudai.com"
68+
explicit_domain = os.getenv("AGENTBOX_DOMAIN")
69+
if explicit_domain:
70+
return explicit_domain
71+
region = ConnectionConfig._region()
72+
return REGION_DOMAIN_MAP.get(region, REGION_DOMAIN_MAP[DEFAULT_REGION])
5873

5974
@staticmethod
6075
def _debug():

ucloud_sandbox/template_async/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ async def build(
219219
)
220220
)
221221

222-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
223222
config = ConnectionConfig(
224223
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
225224
)
@@ -309,7 +308,6 @@ async def build_in_background(
309308
)
310309
```
311310
"""
312-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
313311
config = ConnectionConfig(
314312
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
315313
)
@@ -353,7 +351,6 @@ async def get_build_status(
353351
status = await AsyncTemplate.get_build_status(build_info, logs_offset=0)
354352
```
355353
"""
356-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
357354
config = ConnectionConfig(
358355
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
359356
)

ucloud_sandbox/template_sync/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ def build(
219219
)
220220
)
221221

222-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
223222
config = ConnectionConfig(
224223
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
225224
)
@@ -309,7 +308,6 @@ def build_in_background(
309308
)
310309
```
311310
"""
312-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
313311
config = ConnectionConfig(
314312
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
315313
)
@@ -353,7 +351,6 @@ def get_build_status(
353351
status = Template.get_build_status(build_info, logs_offset=0)
354352
```
355353
"""
356-
domain = domain or os.environ.get("AGENTBOX_DOMAIN", "sandbox.ucloudai.com")
357354
config = ConnectionConfig(
358355
domain=domain, api_key=api_key or os.environ.get("AGENTBOX_API_KEY")
359356
)

0 commit comments

Comments
 (0)