Skip to content

Commit ec0614e

Browse files
authored
#1090 add check for 409 error code (#1733)
1 parent 6282cdb commit ec0614e

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tableauserverclient.server.endpoint.exceptions import (
1515
InternalServerError,
1616
MissingRequiredFieldError,
17+
ServerResponseError,
1718
UnsupportedAttributeError,
1819
)
1920
from tableauserverclient.server.endpoint.permissions_endpoint import _PermissionsEndpoint
@@ -125,7 +126,7 @@ def get_by_id(self, workbook_id: str) -> WorkbookItem:
125126
return WorkbookItem.from_response(server_response.content, self.parent_srv.namespace)[0]
126127

127128
@api(version="2.8")
128-
def refresh(self, workbook_item: Union[WorkbookItem, str], incremental: bool = False) -> JobItem:
129+
def refresh(self, workbook_item: Union[WorkbookItem, str], incremental: bool = False) -> JobItem | None:
129130
"""
130131
Refreshes the extract of an existing workbook.
131132
@@ -138,13 +139,19 @@ def refresh(self, workbook_item: Union[WorkbookItem, str], incremental: bool = F
138139
139140
Returns
140141
-------
141-
JobItem
142-
The job item.
142+
JobItem | None
143+
The job item, or None if a refresh job is already queued for this workbook.
143144
"""
144145
id_ = getattr(workbook_item, "id", workbook_item)
145146
url = f"{self.baseurl}/{id_}/refresh"
146147
refresh_req = RequestFactory.Task.refresh_req(incremental, self.parent_srv)
147-
server_response = self.post_request(url, refresh_req)
148+
try:
149+
server_response = self.post_request(url, refresh_req)
150+
except ServerResponseError as e:
151+
if e.code.startswith("409") and "already" in e.detail:
152+
logger.warning(f"{e.summary} {e.detail}")
153+
return None
154+
raise
148155
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
149156
return new_job
150157

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api https://help.tableau.com/samples/en-us/rest_api/ts-api_3_27.xsd">
3+
<error code="409093"><summary>Resource Conflict</summary><detail>Job for \'extract\' is already queued. Not queuing a duplicate.</detail></error></tsResponse>

test/test_workbook.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
PUBLISH_XML = TEST_ASSET_DIR / "workbook_publish.xml"
3535
PUBLISH_ASYNC_XML = TEST_ASSET_DIR / "workbook_publish_async.xml"
3636
REFRESH_XML = TEST_ASSET_DIR / "workbook_refresh.xml"
37+
WORKBOOK_REFRESH_DUPLICATE_XML = TEST_ASSET_DIR / "workbook_refresh_duplicate.xml"
3738
REVISION_XML = TEST_ASSET_DIR / "workbook_revision.xml"
3839
UPDATE_XML = TEST_ASSET_DIR / "workbook_update.xml"
3940
UPDATE_PERMISSIONS = TEST_ASSET_DIR / "workbook_update_permissions.xml"
@@ -178,6 +179,20 @@ def test_refresh_id(server: TSC.Server) -> None:
178179
server.workbooks.refresh("3cc6cd06-89ce-4fdc-b935-5294135d6d42")
179180

180181

182+
def test_refresh_already_running(server: TSC.Server) -> None:
183+
server.version = "2.8"
184+
server.workbooks.baseurl
185+
response_xml = WORKBOOK_REFRESH_DUPLICATE_XML.read_text()
186+
with requests_mock.mock() as m:
187+
m.post(
188+
server.workbooks.baseurl + "/3cc6cd06-89ce-4fdc-b935-5294135d6d42/refresh",
189+
status_code=409,
190+
text=response_xml,
191+
)
192+
refresh_job = server.workbooks.refresh("3cc6cd06-89ce-4fdc-b935-5294135d6d42")
193+
assert refresh_job is None
194+
195+
181196
def test_refresh_object(server: TSC.Server) -> None:
182197
server.version = "2.8"
183198
server.workbooks.baseurl

0 commit comments

Comments
 (0)