Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/preset_cli/api/clients/superset.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def _run_query(
limit: int = 1000,
) -> Dict[str, Any]:
url = self.baseurl / "api/v1/sqllab/execute/"
url = str(url) + "/"
data = {
"client_id": shortid()[:10],
"database_id": database_id,
Expand Down Expand Up @@ -332,7 +333,7 @@ def get_data( # pylint: disable=too-many-locals, too-many-arguments
]
if len(time_columns) > 1:
options = ", ".join(time_columns)
raise Exception(
raise Exception( # pylint: disable=broad-exception-raised
f"Unable to determine time column, please pass `time_series` "
f"as one of: {options}",
)
Expand Down Expand Up @@ -439,7 +440,11 @@ def get_resources(self, resource_name: str, **kwargs: Any) -> List[Any]:
query = prison.dumps(
{
"filters": [
dict(col=col, opr=value.operator, value=value.value)
dict( # pylint: disable=use-dict-literal
col=col,
opr=value.operator,
value=value.value,
)
for col, value in operations.items()
],
"order_column": "changed_on_delta_humanized",
Expand All @@ -449,6 +454,7 @@ def get_resources(self, resource_name: str, **kwargs: Any) -> List[Any]:
},
)
url = self.baseurl / "api/v1" / resource_name / "" % {"q": query}
url = str(url).replace("?", "/?").replace("//?", "/?")

_logger.debug("GET %s", url)
response = self.session.get(url)
Expand All @@ -469,6 +475,7 @@ def create_resource(self, resource_name: str, **kwargs: Any) -> Any:
Create a resource.
"""
url = self.baseurl / "api/v1" / resource_name / ""
url = str(url) + "/"

_logger.debug("POST %s\n%s", url, json.dumps(kwargs, indent=4))
response = self.session.post(url, json=kwargs)
Expand All @@ -491,6 +498,7 @@ def update_resource(
url = self.baseurl / "api/v1" / resource_name / str(resource_id)
if query_args:
url %= query_args
url = str(url)

response = self.session.put(url, json=kwargs)
validate_response(response)
Expand Down Expand Up @@ -688,6 +696,7 @@ def export_zip(self, resource_name: str, ids: List[int]) -> BytesIO:
Export one or more of a resource.
"""
url = self.baseurl / "api/v1" / resource_name / "export/"
url = str(url) + "/"

buf = BytesIO()
with ZipFile(buf, "w") as bundle:
Expand Down Expand Up @@ -715,6 +724,7 @@ def get_uuids(self, resource_name: str) -> Dict[int, UUID]:
between IDs and UUIDs in older versions of Superset.
"""
url = self.baseurl / "api/v1" / resource_name / "export/"
url = str(url) + "/"

uuids: Dict[int, UUID] = {}
for resource in self.get_resources(resource_name):
Expand Down Expand Up @@ -744,6 +754,7 @@ def import_zip(
key = "bundle" if resource_name == "assets" else "formData"
files = {key: form_data}
url = self.baseurl / "api/v1" / resource_name / "import/"
url = str(url) + "/"

self.session.headers.update({"Accept": "application/json"})
data = {"overwrite": json.dumps(overwrite)}
Expand Down Expand Up @@ -1074,16 +1085,20 @@ def import_rls(self, rls: RuleType) -> None: # pylint: disable=too-many-locals
datasets = self.get_datasets(table_name=table)

if not datasets:
raise Exception(f"Cannot find table: {table}")
raise Exception( # pylint: disable=broad-exception-raised
f"Cannot find table: {table}",
)
if len(datasets) > 1:
raise Exception(f"More than one table found: {table}")
raise Exception( # pylint: disable=broad-exception-raised
f"More than one table found: {table}",
)
table_ids.append(datasets[0]["id"])

role_ids: List[int] = []
for role_name in rls["roles"]:
role_id = self.get_role_id(role_name)
if self.get_role_permissions(role_id):
raise Exception(
raise Exception( # pylint: disable=broad-exception-raised
f"Role {role_name} currently has permissions associated with it. To "
"use it with RLS it should have no permissions.",
)
Expand Down Expand Up @@ -1130,12 +1145,18 @@ def get_role_id(self, role_name: str) -> int:
soup = BeautifulSoup(response.text, features="html.parser")
tables = soup.find_all("table")
if len(tables) < 2:
raise Exception(f"Cannot find role: {role_name}")
raise Exception( # pylint: disable=broad-exception-raised
f"Cannot find role: {role_name}",
)
trs = tables[1].find_all("tr")
if len(trs) == 1:
raise Exception(f"Cannot find role: {role_name}")
raise Exception( # pylint: disable=broad-exception-raised
f"Cannot find role: {role_name}",
)
if len(trs) > 2:
raise Exception(f"More than one role found: {role_name}")
raise Exception( # pylint: disable=broad-exception-raised
f"More than one role found: {role_name}",
)

tds = trs[1].find_all("td")
td = tds[0] # pylint: disable=invalid-name
Expand Down
4 changes: 3 additions & 1 deletion src/preset_cli/auth/superset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def get_csrf_token(self, jwt: str) -> str:
"""
Get a CSRF token.
"""
url = self.baseurl / "api/v1/security/csrf_token/"
url = str(url) + "/"
response = self.session.get(
self.baseurl / "api/v1/security/csrf_token/", # type: ignore
url,
headers={"Authorization": f"Bearer {jwt}"},
)
response.raise_for_status()
Expand Down