diff --git a/tableauserverclient/models/property_decorators.py b/tableauserverclient/models/property_decorators.py index 050346594..02b20c009 100644 --- a/tableauserverclient/models/property_decorators.py +++ b/tableauserverclient/models/property_decorators.py @@ -32,6 +32,17 @@ def wrapper(self, value): return wrapper +def property_is_boolean_or_none(func): + @wraps(func) + def wrapper(self, value): + if value is not None and not isinstance(value, bool): + error = f"Boolean or None expected for {func.__name__} flag." + raise ValueError(error) + return func(self, value) + + return wrapper + + def property_not_nullable(func): @wraps(func) def wrapper(self, value): diff --git a/tableauserverclient/models/workbook_item.py b/tableauserverclient/models/workbook_item.py index 75ca0034e..59e368245 100644 --- a/tableauserverclient/models/workbook_item.py +++ b/tableauserverclient/models/workbook_item.py @@ -15,6 +15,7 @@ from .permissions_item import PermissionsRule from .property_decorators import ( property_is_boolean, + property_is_boolean_or_none, property_is_data_acceleration_config, ) from .revision_item import RevisionItem @@ -41,6 +42,13 @@ class as arguments. The workbook item specifies the project. show_tabs : bool, optional Determines whether the workbook shows tabs for the view. + bridge_refresh_embedded_files : bool | None, optional + Intended for Tableau Cloud sites that use Bridge. Opts in to bridge refresh + for embedded extracts over file-based connections in the workbook. Sent only + when the workbook is published. If None (the default), the setting is not + sent. Requires REST API 3.30 or later. Publishing with this set raises + EndpointUnavailableError on a lower REST version. + Attributes ---------- connections : list[ConnectionItem] @@ -130,6 +138,7 @@ def __init__( show_tabs: bool = False, thumbnails_user_id: str | None = None, thumbnails_group_id: str | None = None, + bridge_refresh_embedded_files: bool | None = None, ) -> None: self._connections = None self._content_url = None @@ -151,6 +160,7 @@ def __init__( # workaround for Personal Space workbooks without a project self.project_id: str | None = project_id or uuid.uuid4().__str__() self.show_tabs = show_tabs + self.bridge_refresh_embedded_files = bridge_refresh_embedded_files self.hidden_views: list[str] | None = None self.tags: set[str] = set() self.data_acceleration_config = { @@ -263,6 +273,15 @@ def show_tabs(self) -> bool: def show_tabs(self, value: bool): self._show_tabs = value + @property + def bridge_refresh_embedded_files(self) -> bool | None: + return self._bridge_refresh_embedded_files + + @bridge_refresh_embedded_files.setter + @property_is_boolean_or_none + def bridge_refresh_embedded_files(self, value: bool | None): + self._bridge_refresh_embedded_files = value + @property def size(self): return self._size diff --git a/tableauserverclient/server/endpoint/workbooks_endpoint.py b/tableauserverclient/server/endpoint/workbooks_endpoint.py index ada961af1..5e24eac12 100644 --- a/tableauserverclient/server/endpoint/workbooks_endpoint.py +++ b/tableauserverclient/server/endpoint/workbooks_endpoint.py @@ -911,11 +911,18 @@ def publish( ValueError Workbooks cannot be appended. + EndpointUnavailableError + If workbook_item.bridge_refresh_embedded_files is set and the server + REST API version is lower than 3.30. + Returns ------- WorkbookItem | JobItem The workbook item or job item that was published. """ + if workbook_item.bridge_refresh_embedded_files is not None: + self.parent_srv.assert_at_least_version("3.30", "bridge_refresh_embedded_files") + if isinstance(file, (str, os.PathLike)): if not os.path.isfile(file): error = "File path does not lead to an existing file." diff --git a/tableauserverclient/server/request_factory.py b/tableauserverclient/server/request_factory.py index 517a26956..2ee1c4db7 100644 --- a/tableauserverclient/server/request_factory.py +++ b/tableauserverclient/server/request_factory.py @@ -1091,6 +1091,11 @@ def _generate_xml( elif workbook_item.thumbnails_group_id is not None: workbook_element.attrib["thumbnailsGroupId"] = workbook_item.thumbnails_group_id + if workbook_item.bridge_refresh_embedded_files is not None: + workbook_element.attrib["bridgeRefreshEmbeddedFiles"] = str( + workbook_item.bridge_refresh_embedded_files + ).lower() + return ET.tostring(xml_request) def update_req(self, workbook_item, parent_srv: "Server | None" = None): diff --git a/test/request_factory/test_workbook_requests.py b/test/request_factory/test_workbook_requests.py index b114e04a0..a6b6b0b46 100644 --- a/test/request_factory/test_workbook_requests.py +++ b/test/request_factory/test_workbook_requests.py @@ -2,6 +2,7 @@ import tableauserverclient.server.request_factory as TSC_RF from tableauserverclient.helpers.strings import redact_xml import pytest +import re import sys @@ -16,6 +17,25 @@ def test_generate_xml() -> None: TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item) +@pytest.mark.parametrize( + "flag, expected", + [ + (True, rb']*bridgeRefreshEmbeddedFiles="true"'), + (False, rb']*bridgeRefreshEmbeddedFiles="false"'), + ], +) +def test_generate_xml_bridge_refresh_embedded_files(flag: bool, expected: bytes) -> None: + workbook_item = TSC.WorkbookItem("project_id", name="name", bridge_refresh_embedded_files=flag) + request = TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item) + assert re.search(expected, request) + + +def test_generate_xml_omits_bridge_refresh_embedded_files_when_unset() -> None: + workbook_item = TSC.WorkbookItem("project_id", name="name") + request = TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item) + assert b"bridgeRefreshEmbeddedFiles" not in request + + def test_generate_xml_invalid_connection() -> None: workbook_item: TSC.WorkbookItem = TSC.WorkbookItem("name", "project_id") conn = TSC.ConnectionItem() diff --git a/test/test_workbook.py b/test/test_workbook.py index c0965f121..6a7c87690 100644 --- a/test/test_workbook.py +++ b/test/test_workbook.py @@ -12,6 +12,7 @@ from tableauserverclient.datetime_helpers import format_datetime, parse_datetime from tableauserverclient.models import UserItem, GroupItem, PermissionsRule from tableauserverclient.server.endpoint.exceptions import InternalServerError, UnsupportedAttributeError +from tableauserverclient.server.exceptions import EndpointUnavailableError from tableauserverclient.server.request_factory import RequestFactory TEST_ASSET_DIR = Path(__file__).parent / "assets" @@ -650,6 +651,51 @@ def test_publish_description_in_request_xml(server: TSC.Server) -> None: assert re.search(b'description=\\"A great workbook\\"', request_body) +def test_publish_sends_bridge_refresh_embedded_files(server: TSC.Server) -> None: + """The opt-in set on the WorkbookItem must reach the publish request body.""" + server.version = "3.30" + response_xml = PUBLISH_XML.read_text() + with requests_mock.mock() as m: + m.post(server.workbooks.baseurl, text=response_xml) + new_workbook = TSC.WorkbookItem( + name="Sample", + show_tabs=False, + project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", + bridge_refresh_embedded_files=True, + ) + sample_workbook = os.path.join(TEST_ASSET_DIR, "SampleWB.twbx") + server.workbooks.publish(new_workbook, sample_workbook, server.PublishMode.CreateNew) + request_body = m._adapter.request_history[0]._request.body + assert re.search(rb']*bridgeRefreshEmbeddedFiles="true"', request_body) + + +def test_publish_bridge_refresh_embedded_files_requires_rest_3_30(server: TSC.Server) -> None: + """Setting the opt-in against an older REST version raises before any request is made.""" + server.version = "3.29" + with requests_mock.mock() as m: + new_workbook = TSC.WorkbookItem( + name="Sample", + project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760", + bridge_refresh_embedded_files=False, + ) + sample_workbook = os.path.join(TEST_ASSET_DIR, "SampleWB.twbx") + with pytest.raises(EndpointUnavailableError, match="bridge_refresh_embedded_files"): + server.workbooks.publish(new_workbook, sample_workbook, server.PublishMode.CreateNew) + assert m.call_count == 0 + + +def test_publish_without_bridge_refresh_embedded_files_needs_no_version_check(server: TSC.Server) -> None: + """Leaving the opt-in unset must keep working on older REST versions.""" + server.version = "3.29" + response_xml = PUBLISH_XML.read_text() + with requests_mock.mock() as m: + m.post(server.workbooks.baseurl, text=response_xml) + new_workbook = TSC.WorkbookItem(name="Sample", project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + sample_workbook = os.path.join(TEST_ASSET_DIR, "SampleWB.twbx") + server.workbooks.publish(new_workbook, sample_workbook, server.PublishMode.CreateNew) + assert b"bridgeRefreshEmbeddedFiles" not in m._adapter.request_history[0]._request.body + + def test_publish_a_packaged_file_object(server: TSC.Server) -> None: response_xml = PUBLISH_XML.read_text() with requests_mock.mock() as m: diff --git a/test/test_workbook_model.py b/test/test_workbook_model.py index 3d6f31a7a..b7bffb923 100644 --- a/test/test_workbook_model.py +++ b/test/test_workbook_model.py @@ -10,3 +10,24 @@ def test_invalid_show_tabs(): with pytest.raises(ValueError): workbook.show_tabs = None + + +@pytest.mark.parametrize("value", [True, False, None]) +def test_bridge_refresh_embedded_files_accepts_bool_or_none(value: bool | None) -> None: + workbook = TSC.WorkbookItem("project_id", name="wb", bridge_refresh_embedded_files=value) + assert workbook.bridge_refresh_embedded_files is value + + +def test_bridge_refresh_embedded_files_can_be_cleared() -> None: + workbook = TSC.WorkbookItem("project_id", name="wb", bridge_refresh_embedded_files=True) + workbook.bridge_refresh_embedded_files = None + assert workbook.bridge_refresh_embedded_files is None + + +@pytest.mark.parametrize("value", ["yes", "true", 1, 0]) +def test_bridge_refresh_embedded_files_rejects_non_boolean(value: object) -> None: + workbook = TSC.WorkbookItem("project_id", name="wb") + with pytest.raises(ValueError): + workbook.bridge_refresh_embedded_files = value # type: ignore[assignment] + with pytest.raises(ValueError): + TSC.WorkbookItem("project_id", name="wb", bridge_refresh_embedded_files=value) # type: ignore[arg-type] diff --git a/test_e2e/assets/WorkbookWithExcelExtract.twbx b/test_e2e/assets/WorkbookWithExcelExtract.twbx new file mode 100644 index 000000000..dd7851c02 Binary files /dev/null and b/test_e2e/assets/WorkbookWithExcelExtract.twbx differ diff --git a/test_e2e/test_publish.py b/test_e2e/test_publish.py new file mode 100644 index 000000000..7d3527c8b --- /dev/null +++ b/test_e2e/test_publish.py @@ -0,0 +1,47 @@ +""" +E2E test for publishing a workbook with the bridge_refresh_embedded_files opt-in. + +Run with: + TABLEAU_SERVER=https://... TABLEAU_SITE=mysite TABLEAU_TOKEN=... TABLEAU_TOKEN_NAME=... \ + pytest -n0 test_e2e/test_publish.py -v + +Use -n0 (no xdist). The repo's pytest config runs tests in parallel by default, and each worker signs in +with the same personal access token. A token allows one active session, so a parallel run fails with a 401. + +This checks that the server accepts the publish request with the attribute. The server does not return +the value, so the test cannot check that it was stored. +""" + +import os +import uuid +from pathlib import Path + +import pytest +import tableauserverclient as TSC + +SAMPLE_WORKBOOK = Path(__file__).parent / "assets" / "WorkbookWithExcelExtract.twbx" + +pytestmark = pytest.mark.e2e + + +@pytest.mark.parametrize("opt_in", [True, False]) +def test_publish_with_bridge_refresh_embedded_files(server, opt_in): + """The server must accept a publish request that carries bridgeRefreshEmbeddedFiles.""" + project_name = os.environ.get("TABLEAU_PROJECT", "Default") + opts = TSC.RequestOptions() + opts.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, project_name)) + projects, _ = server.projects.get(opts) + if not projects: + pytest.skip(f"Project {project_name!r} not found — set TABLEAU_PROJECT env var") + + wb_item = TSC.WorkbookItem( + name=f"tsc-e2e-bridge-refresh-{str(opt_in).lower()}-{uuid.uuid4().hex[:8]}", + project_id=projects[0].id, + bridge_refresh_embedded_files=opt_in, + ) + published = server.workbooks.publish(wb_item, SAMPLE_WORKBOOK, TSC.Server.PublishMode.CreateNew) + try: + assert published.id, "publish returned no workbook id" + assert published.name == wb_item.name, f"published name {published.name!r} != requested {wb_item.name!r}" + finally: + server.workbooks.delete(published.id)