|
| 1 | +import requests |
| 2 | +from sempy_labs._helper_functions import ( |
| 3 | + _get_url_prefix, |
| 4 | + resolve_workspace_name_and_id, |
| 5 | + resolve_dataset_name_and_id, |
| 6 | +) |
| 7 | +from typing import Optional |
| 8 | +import sempy_labs._icons as icons |
| 9 | +from sempy._utils._log import log |
| 10 | +from uuid import UUID |
| 11 | +from sempy.fabric.exceptions import FabricHTTPException |
| 12 | + |
| 13 | + |
| 14 | +@log |
| 15 | +def set_autosync( |
| 16 | + dataset: str | UUID, workspace: Optional[str | UUID] = None, enable: bool = True |
| 17 | +): |
| 18 | + """ |
| 19 | + Enables or disables AutoSync for a Direct Lake semantic model. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + dataset : str | uuid.UUID |
| 24 | + Name or ID of the semantic model. |
| 25 | + workspace : str | uuid.UUID, default=None |
| 26 | + The Fabric workspace name or ID. |
| 27 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 28 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 29 | + enable : bool, default=True |
| 30 | + Whether to enable (True) or disable (False) AutoSync. |
| 31 | + """ |
| 32 | + |
| 33 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 34 | + (dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id) |
| 35 | + |
| 36 | + import notebookutils |
| 37 | + |
| 38 | + token = notebookutils.credentials.getToken("pbi") |
| 39 | + headers = {"Authorization": f"Bearer {token}"} |
| 40 | + |
| 41 | + prefix = _get_url_prefix() |
| 42 | + |
| 43 | + response = requests.get( |
| 44 | + url=f"{prefix}/metadata/models/{dataset_id}", headers=headers |
| 45 | + ) |
| 46 | + id = response.json().get("model", {}).get("id") |
| 47 | + |
| 48 | + payload = {"directLakeAutoSync": enable} |
| 49 | + response = requests.post( |
| 50 | + url=f"{prefix}/metadata/models/{id}/settings", headers=headers, json=payload |
| 51 | + ) |
| 52 | + |
| 53 | + if response.status_code != 204: |
| 54 | + raise FabricHTTPException(f"Failed to retrieve labels: {response.text}") |
| 55 | + |
| 56 | + print( |
| 57 | + f"{icons.green_dot} Direct Lake AutoSync has been {'enabled' if enable else 'disabled'} for the '{dataset_name}' semantic model within the '{workspace_name}' workspace." |
| 58 | + ) |
0 commit comments