-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
62 lines (46 loc) · 1.65 KB
/
env.py
File metadata and controls
62 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations
import os
from urllib.parse import urlparse
from hotdata import ApiClient, Configuration
from hotdata.api.workspaces_api import WorkspacesApi
def normalize_host(url: str) -> str:
u = url.rstrip("/")
if u.endswith("/v1"):
u = u[:-3]
parsed = urlparse(u)
if not parsed.scheme or not parsed.netloc:
return u
return f"{parsed.scheme}://{parsed.netloc}"
def default_api_key() -> str:
return os.environ.get("HOTDATA_API_KEY", "") or os.environ.get(
"HOTDATA_TOKEN", ""
)
def explicit_workspace_id() -> str | None:
return os.environ.get("HOTDATA_WORKSPACE") or os.environ.get(
"HOTDATA_WORKSPACE_ID"
)
def default_host() -> str:
raw = os.environ.get("HOTDATA_API_URL", "https://api.hotdata.dev")
return normalize_host(raw)
def default_session_id() -> str | None:
return os.environ.get("HOTDATA_SANDBOX")
def list_workspaces(api_key: str, host: str, session_id: str | None):
cfg = Configuration(
host=host,
api_key=api_key,
workspace_id=None,
session_id=session_id,
)
with ApiClient(cfg) as api:
listing = WorkspacesApi(api).list_workspaces()
return listing.workspaces
def pick_workspace(api_key: str, host: str, session_id: str | None) -> str:
explicit = explicit_workspace_id()
if explicit:
return explicit
workspaces = list_workspaces(api_key, host, session_id)
if not workspaces:
raise RuntimeError("No Hotdata workspaces found for this API key.")
active = [w for w in workspaces if w.active]
chosen = active[0] if active else workspaces[0]
return chosen.public_id