Skip to content

Commit 8b29370

Browse files
committed
fixed fabric exception, fixed issue with get_measure_dependencies
1 parent e7b69d6 commit 8b29370

File tree

9 files changed

+252
-277
lines changed

9 files changed

+252
-277
lines changed

src/sempy_labs/_connections.py

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sempy.fabric as fabric
22
import pandas as pd
3-
import sempy_labs._icons as icons
3+
from sempy.fabric.exceptions import FabricHTTPException
44

55

66
def create_connection_cloud(
@@ -56,33 +56,30 @@ def create_connection_cloud(
5656

5757
response = client.post("/v1/connections", json=request_body)
5858

59-
if response.status_code == 200:
60-
o = response.json()
61-
new_data = {
62-
"Connection Id": o.get("id"),
63-
"Connection Name": o.get("name"),
64-
"Connectivity Type": o.get("connectivityType"),
65-
"Connection Type": o.get("connectionDetails", {}).get("type"),
66-
"Connection Path": o.get("connectionDetails", {}).get("path"),
67-
"Privacy Level": o.get("privacyLevel"),
68-
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
69-
"Single Sign On Type": o.get("credentialDetails", {}).get(
70-
"singleSignOnType"
71-
),
72-
"Connection Encryption": o.get("credentialDetails", {}).get(
73-
"connectionEncryption"
74-
),
75-
"Skip Test Connection": o.get("credentialDetails", {}).get(
76-
"skipTestConnection"
77-
),
78-
}
79-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
80-
81-
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
82-
83-
return df
84-
else:
85-
print(f"{icons.red_dot} {response.status_code}")
59+
if response.status_code != 200:
60+
raise FabricHTTPException(response)
61+
o = response.json()
62+
new_data = {
63+
"Connection Id": o.get("id"),
64+
"Connection Name": o.get("name"),
65+
"Connectivity Type": o.get("connectivityType"),
66+
"Connection Type": o.get("connectionDetails", {}).get("type"),
67+
"Connection Path": o.get("connectionDetails", {}).get("path"),
68+
"Privacy Level": o.get("privacyLevel"),
69+
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
70+
"Single Sign On Type": o.get("credentialDetails", {}).get("singleSignOnType"),
71+
"Connection Encryption": o.get("credentialDetails", {}).get(
72+
"connectionEncryption"
73+
),
74+
"Skip Test Connection": o.get("credentialDetails", {}).get(
75+
"skipTestConnection"
76+
),
77+
}
78+
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
79+
80+
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
81+
82+
return df
8683

8784

8885
def create_connection_on_prem(
@@ -137,34 +134,31 @@ def create_connection_on_prem(
137134

138135
response = client.post("/v1/connections", json=request_body)
139136

140-
if response.status_code == 200:
141-
o = response.json()
142-
new_data = {
143-
"Connection Id": o.get("id"),
144-
"Connection Name": o.get("name"),
145-
"Gateway ID": o.get("gatewayId"),
146-
"Connectivity Type": o.get("connectivityType"),
147-
"Connection Type": o.get("connectionDetails", {}).get("type"),
148-
"Connection Path": o.get("connectionDetails", {}).get("path"),
149-
"Privacy Level": o.get("privacyLevel"),
150-
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
151-
"Single Sign On Type": o.get("credentialDetails", {}).get(
152-
"singleSignOnType"
153-
),
154-
"Connection Encryption": o.get("credentialDetails", {}).get(
155-
"connectionEncryption"
156-
),
157-
"Skip Test Connection": o.get("credentialDetails", {}).get(
158-
"skipTestConnection"
159-
),
160-
}
161-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
162-
163-
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
164-
165-
return df
166-
else:
167-
print(f"{icons.red_dot} {response.status_code}")
137+
if response.status_code != 200:
138+
raise FabricHTTPException(response)
139+
o = response.json()
140+
new_data = {
141+
"Connection Id": o.get("id"),
142+
"Connection Name": o.get("name"),
143+
"Gateway ID": o.get("gatewayId"),
144+
"Connectivity Type": o.get("connectivityType"),
145+
"Connection Type": o.get("connectionDetails", {}).get("type"),
146+
"Connection Path": o.get("connectionDetails", {}).get("path"),
147+
"Privacy Level": o.get("privacyLevel"),
148+
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
149+
"Single Sign On Type": o.get("credentialDetails", {}).get("singleSignOnType"),
150+
"Connection Encryption": o.get("credentialDetails", {}).get(
151+
"connectionEncryption"
152+
),
153+
"Skip Test Connection": o.get("credentialDetails", {}).get(
154+
"skipTestConnection"
155+
),
156+
}
157+
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
158+
159+
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
160+
161+
return df
168162

169163

170164
def create_connection_vnet(
@@ -221,31 +215,28 @@ def create_connection_vnet(
221215

222216
response = client.post("/v1/connections", json=request_body)
223217

224-
if response.status_code == 200:
225-
o = response.json()
226-
new_data = {
227-
"Connection Id": o.get("id"),
228-
"Connection Name": o.get("name"),
229-
"Gateway ID": o.get("gatewayId"),
230-
"Connectivity Type": o.get("connectivityType"),
231-
"Connection Type": o.get("connectionDetails", {}).get("type"),
232-
"Connection Path": o.get("connectionDetails", {}).get("path"),
233-
"Privacy Level": o.get("privacyLevel"),
234-
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
235-
"Single Sign On Type": o.get("credentialDetails", {}).get(
236-
"singleSignOnType"
237-
),
238-
"Connection Encryption": o.get("credentialDetails", {}).get(
239-
"connectionEncryption"
240-
),
241-
"Skip Test Connection": o.get("credentialDetails", {}).get(
242-
"skipTestConnection"
243-
),
244-
}
245-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
246-
247-
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
248-
249-
return df
250-
else:
251-
print(f"{icons.red_dot} {response.status_code}")
218+
if response.status_code != 200:
219+
raise FabricHTTPException(response)
220+
o = response.json()
221+
new_data = {
222+
"Connection Id": o.get("id"),
223+
"Connection Name": o.get("name"),
224+
"Gateway ID": o.get("gatewayId"),
225+
"Connectivity Type": o.get("connectivityType"),
226+
"Connection Type": o.get("connectionDetails", {}).get("type"),
227+
"Connection Path": o.get("connectionDetails", {}).get("path"),
228+
"Privacy Level": o.get("privacyLevel"),
229+
"Credential Type": o.get("credentialDetails", {}).get("credentialType"),
230+
"Single Sign On Type": o.get("credentialDetails", {}).get("singleSignOnType"),
231+
"Connection Encryption": o.get("credentialDetails", {}).get(
232+
"connectionEncryption"
233+
),
234+
"Skip Test Connection": o.get("credentialDetails", {}).get(
235+
"skipTestConnection"
236+
),
237+
}
238+
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
239+
240+
df["Skip Test Connection"] = df["Skip Test Connection"].astype(bool)
241+
242+
return df

src/sempy_labs/_generate_semantic_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from sempy_labs.lakehouse._lakehouse import lakehouse_attached
1313
import sempy_labs._icons as icons
14+
from sempy.fabric.exceptions import FabricHTTPException
1415

1516

1617
def create_blank_semantic_model(

src/sempy_labs/_list_functions.py

Lines changed: 73 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,18 +1083,15 @@ def update_item(
10831083
f"/v1/workspaces/{workspace_id}/{itemType}/{itemId}", json=request_body
10841084
)
10851085

1086-
if response.status_code == 200:
1087-
if description is None:
1088-
print(
1089-
f"{icons.green_dot} The '{current_name}' {item_type} within the '{workspace}' workspace has been updated to be named '{new_name}'"
1090-
)
1091-
else:
1092-
print(
1093-
f"{icons.green_dot} The '{current_name}' {item_type} within the '{workspace}' workspace has been updated to be named '{new_name}' and have a description of '{description}'"
1094-
)
1086+
if response.status_code != 200:
1087+
raise FabricHTTPException(response)
1088+
if description is None:
1089+
print(
1090+
f"{icons.green_dot} The '{current_name}' {item_type} within the '{workspace}' workspace has been updated to be named '{new_name}'"
1091+
)
10951092
else:
1096-
raise ValueError(
1097-
f"{icons.red_dot}: The '{current_name}' {item_type} within the '{workspace}' workspace was not updateds."
1093+
print(
1094+
f"{icons.green_dot} The '{current_name}' {item_type} within the '{workspace}' workspace has been updated to be named '{new_name}' and have a description of '{description}'"
10981095
)
10991096

11001097

@@ -1536,50 +1533,47 @@ def list_shortcuts(
15361533
response = client.get(
15371534
f"/v1/workspaces/{workspace_id}/items/{lakehouse_id}/shortcuts"
15381535
)
1539-
if response.status_code == 200:
1540-
for s in response.json()["value"]:
1541-
shortcutName = s.get("name")
1542-
shortcutPath = s.get("path")
1543-
source = list(s["target"].keys())[0]
1544-
(
1545-
sourceLakehouseName,
1546-
sourceWorkspaceName,
1547-
sourcePath,
1548-
connectionId,
1549-
location,
1550-
subpath,
1551-
) = (None, None, None, None, None, None)
1552-
if source == "oneLake":
1553-
sourceLakehouseId = s.get("target", {}).get(source, {}).get("itemId")
1554-
sourcePath = s.get("target", {}).get(source, {}).get("path")
1555-
sourceWorkspaceId = (
1556-
s.get("target", {}).get(source, {}).get("workspaceId")
1557-
)
1558-
sourceWorkspaceName = fabric.resolve_workspace_name(sourceWorkspaceId)
1559-
sourceLakehouseName = resolve_lakehouse_name(
1560-
sourceLakehouseId, sourceWorkspaceName
1561-
)
1562-
else:
1563-
connectionId = s.get("target", {}).get(source, {}).get("connectionId")
1564-
location = s.get("target", {}).get(source, {}).get("location")
1565-
subpath = s.get("target", {}).get(source, {}).get("subpath")
15661536

1567-
new_data = {
1568-
"Shortcut Name": shortcutName,
1569-
"Shortcut Path": shortcutPath,
1570-
"Source": source,
1571-
"Source Lakehouse Name": sourceLakehouseName,
1572-
"Source Workspace Name": sourceWorkspaceName,
1573-
"Source Path": sourcePath,
1574-
"Source Connection ID": connectionId,
1575-
"Source Location": location,
1576-
"Source SubPath": subpath,
1577-
}
1578-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
1537+
if response.status_code != 200:
1538+
raise FabricHTTPException(response)
1539+
for s in response.json()["value"]:
1540+
shortcutName = s.get("name")
1541+
shortcutPath = s.get("path")
1542+
source = list(s["target"].keys())[0]
1543+
(
1544+
sourceLakehouseName,
1545+
sourceWorkspaceName,
1546+
sourcePath,
1547+
connectionId,
1548+
location,
1549+
subpath,
1550+
) = (None, None, None, None, None, None)
1551+
if source == "oneLake":
1552+
sourceLakehouseId = s.get("target", {}).get(source, {}).get("itemId")
1553+
sourcePath = s.get("target", {}).get(source, {}).get("path")
1554+
sourceWorkspaceId = s.get("target", {}).get(source, {}).get("workspaceId")
1555+
sourceWorkspaceName = fabric.resolve_workspace_name(sourceWorkspaceId)
1556+
sourceLakehouseName = resolve_lakehouse_name(
1557+
sourceLakehouseId, sourceWorkspaceName
1558+
)
1559+
else:
1560+
connectionId = s.get("target", {}).get(source, {}).get("connectionId")
1561+
location = s.get("target", {}).get(source, {}).get("location")
1562+
subpath = s.get("target", {}).get(source, {}).get("subpath")
1563+
1564+
new_data = {
1565+
"Shortcut Name": shortcutName,
1566+
"Shortcut Path": shortcutPath,
1567+
"Source": source,
1568+
"Source Lakehouse Name": sourceLakehouseName,
1569+
"Source Workspace Name": sourceWorkspaceName,
1570+
"Source Path": sourcePath,
1571+
"Source Connection ID": connectionId,
1572+
"Source Location": location,
1573+
"Source SubPath": subpath,
1574+
}
1575+
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
15791576

1580-
print(
1581-
f"{icons.warning} This function relies on an API which is not yet official as of May 21, 2024. Once the API becomes official this function will work as expected."
1582-
)
15831577
return df
15841578

15851579

@@ -2109,12 +2103,11 @@ def update_spark_settings(
21092103
f"/v1/workspaces/{workspace_id}/spark/settings", json=request_body
21102104
)
21112105

2112-
if response.status_code == 200:
2113-
print(
2114-
f"{icons.green_dot} The spark settings within the '{workspace}' workspace have been updated accordingly."
2115-
)
2116-
else:
2117-
raise ValueError(f"{icons.red_dot} {response.status_code}")
2106+
if response.status_code != 200:
2107+
raise FabricHTTPException(response)
2108+
print(
2109+
f"{icons.green_dot} The spark settings within the '{workspace}' workspace have been updated accordingly."
2110+
)
21182111

21192112

21202113
def add_user_to_workspace(
@@ -2156,12 +2149,11 @@ def add_user_to_workspace(
21562149
f"/v1.0/myorg/groups/{workspace_id}/users", json=request_body
21572150
)
21582151

2159-
if response.status_code == 200:
2160-
print(
2161-
f"{icons.green_dot} The '{email_address}' user has been added as a{plural} '{role_name}' within the '{workspace}' workspace."
2162-
)
2163-
else:
2164-
print(f"{icons.red_dot} {response.status_code}")
2152+
if response.status_code != 200:
2153+
raise FabricHTTPException(response)
2154+
print(
2155+
f"{icons.green_dot} The '{email_address}' user has been added as a{plural} '{role_name}' within the '{workspace}' workspace."
2156+
)
21652157

21662158

21672159
def delete_user_from_workspace(email_address: str, workspace: Optional[str] = None):
@@ -2186,12 +2178,11 @@ def delete_user_from_workspace(email_address: str, workspace: Optional[str] = No
21862178
client = fabric.PowerBIRestClient()
21872179
response = client.delete(f"/v1.0/myorg/groups/{workspace_id}/users/{email_address}")
21882180

2189-
if response.status_code == 200:
2190-
print(
2191-
f"{icons.green_dot} The '{email_address}' user has been removed from accessing the '{workspace}' workspace."
2192-
)
2193-
else:
2194-
print(f"{icons.red_dot} {response.status_code}")
2181+
if response.status_code != 200:
2182+
raise FabricHTTPException(response)
2183+
print(
2184+
f"{icons.green_dot} The '{email_address}' user has been removed from accessing the '{workspace}' workspace."
2185+
)
21952186

21962187

21972188
def update_workspace_user(
@@ -2229,12 +2220,11 @@ def update_workspace_user(
22292220
client = fabric.PowerBIRestClient()
22302221
response = client.put(f"/v1.0/myorg/groups/{workspace_id}/users", json=request_body)
22312222

2232-
if response.status_code == 200:
2233-
print(
2234-
f"{icons.green_dot} The '{email_address}' user has been updated to a '{role_name}' within the '{workspace}' workspace."
2235-
)
2236-
else:
2237-
print(f"{icons.red_dot} {response.status_code}")
2223+
if response.status_code != 200:
2224+
raise FabricHTTPException(response)
2225+
print(
2226+
f"{icons.green_dot} The '{email_address}' user has been updated to a '{role_name}' within the '{workspace}' workspace."
2227+
)
22382228

22392229

22402230
def list_workspace_users(workspace: Optional[str] = None) -> pd.DataFrame:
@@ -2307,12 +2297,12 @@ def assign_workspace_to_dataflow_storage(
23072297
response = client.post(
23082298
f"/v1.0/myorg/groups/{workspace_id}/AssignToDataflowStorage", json=request_body
23092299
)
2310-
if response.status_code == 200:
2311-
print(
2312-
f"{icons.green_dot} The '{dataflow_storage_account}' dataflow storage account has been assigned to the '{workspace}' workspacce."
2313-
)
2314-
else:
2315-
print(f"{icons.red_dot} {response.status_code}")
2300+
2301+
if response.status_code != 200:
2302+
raise FabricHTTPException(response)
2303+
print(
2304+
f"{icons.green_dot} The '{dataflow_storage_account}' dataflow storage account has been assigned to the '{workspace}' workspacce."
2305+
)
23162306

23172307

23182308
def list_capacities() -> pd.DataFrame:

0 commit comments

Comments
 (0)