Skip to content

Commit 4929b47

Browse files
authored
feat: added catalog functions (#3)
* feat: catalog functions implemented * fix: search variables returned type * chore: code review
1 parent d7fda7c commit 4929b47

4 files changed

Lines changed: 472 additions & 393 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
install:
22
uv sync --all-extras
33

4+
update:
5+
rm -f uv.lock
6+
uv sync
7+
48
test:
59
uv run --all-extras pytest
610

datashield_opal/impl.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ def __init__(self, name: str, loginInfo: OpalClient.LoginInfo, profile: str = "d
142142
self.rsession = None
143143
self.rsession_started = False
144144

145+
def get_name(self) -> str:
146+
"""Get the name of the connection."""
147+
return self.name
148+
145149
def check_user(self) -> bool:
146150
"""Check if the user can authenticate by trying to retrieve the current subject profile."""
147151
try:
@@ -165,10 +169,40 @@ def list_tables(self) -> list:
165169
return names
166170

167171
def has_table(self, name: str) -> bool:
172+
# name is in format "datasource.table"
173+
if "." not in name:
174+
raise OpalDSError(ValueError(f"Invalid table name: {name}. Expected format 'datasource.table'"))
168175
parts = name.split(".")
169176
response = self._get(UriBuilder(["datasource", parts[0], "table", parts[1]]).build()).send()
170177
return response.code == 200
171178

179+
def list_table_variables(self, table) -> list:
180+
# table is in format "datasource.table"
181+
if "." not in table:
182+
raise OpalDSError(ValueError(f"Invalid table name: {table}. Expected format 'datasource.table'"))
183+
tokens = table.split(".")
184+
project_name = tokens[0]
185+
table_name = tokens[1]
186+
return (
187+
self
188+
._get(UriBuilder(["datasource", project_name, "table", table_name, "variables"]).build())
189+
.fail_on_error()
190+
.send()
191+
.from_json()
192+
)
193+
194+
def list_taxonomies(self) -> list:
195+
return self._get(UriBuilder(["system", "conf", "taxonomies"]).build()).fail_on_error().send().from_json()
196+
197+
def search_variables(self, query) -> dict:
198+
return (
199+
self
200+
._get(UriBuilder(["datasources", "variables", "_search"]).query("query", query).build())
201+
.fail_on_error()
202+
.send()
203+
.from_json()
204+
)
205+
172206
def list_resources(self) -> list:
173207
response = self._get("/projects").fail_on_error().send()
174208
projects = response.from_json()
@@ -181,6 +215,8 @@ def list_resources(self) -> list:
181215
return names
182216

183217
def has_resource(self, name: str) -> bool:
218+
if "." not in name:
219+
raise OpalDSError(ValueError(f"Invalid resource name: {name}. Expected format 'project.resource'"))
184220
parts = name.split(".")
185221
response = self._get(UriBuilder(["project", parts[0], "resource", parts[1]]).build()).send()
186222
return response.code == 200

tests/test_impl.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def test_tables(self):
8585
assert "CNSIM.CNSIM1" in tables
8686
assert conn.has_table("CNSIM.CNSIM1")
8787

88+
@pytest.mark.integration
89+
def test_table_variables(self):
90+
conn = self.conn
91+
variables = conn.list_table_variables("CNSIM.CNSIM1")
92+
assert type(variables) is list
93+
assert "LAB_TSC" in [v.get("name") for v in variables]
94+
8895
@pytest.mark.integration
8996
def test_resources(self):
9097
conn = self.conn

0 commit comments

Comments
 (0)