From 8ff13f820d9086f9fdb702e84bc75911ab0282b0 Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sat, 21 Nov 2026 20:52:00 -0600 Subject: [PATCH] fix: return a WebhookTestResultItem from webhooks.test webhooks.test handed back the raw response. It now parses webhookTestResult into the destination id, HTTP status, and body. --- tableauserverclient/__init__.py | 2 ++ tableauserverclient/models/__init__.py | 3 +- tableauserverclient/models/webhook_item.py | 29 +++++++++++++++++++ .../server/endpoint/webhooks_endpoint.py | 14 +++++---- test/test_webhook.py | 16 ++++++++-- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/tableauserverclient/__init__.py b/tableauserverclient/__init__.py index 819cfcfa6..b882b150a 100644 --- a/tableauserverclient/__init__.py +++ b/tableauserverclient/__init__.py @@ -57,6 +57,7 @@ ViewItem, VirtualConnectionItem, WebhookItem, + WebhookTestResultItem, WeeklyInterval, WorkbookItem, ) @@ -159,6 +160,7 @@ "ViewItem", "VirtualConnectionItem", "WebhookItem", + "WebhookTestResultItem", "WeeklyInterval", "WorkbookItem", ] diff --git a/tableauserverclient/models/__init__.py b/tableauserverclient/models/__init__.py index 5eb79f8e8..ee7eac9c6 100644 --- a/tableauserverclient/models/__init__.py +++ b/tableauserverclient/models/__init__.py @@ -55,7 +55,7 @@ from tableauserverclient.models.user_item import UserItem from tableauserverclient.models.view_item import ViewItem from tableauserverclient.models.virtual_connection_item import VirtualConnectionItem -from tableauserverclient.models.webhook_item import WebhookItem +from tableauserverclient.models.webhook_item import WebhookItem, WebhookTestResultItem from tableauserverclient.models.workbook_item import WorkbookItem from tableauserverclient.models.extract_item import ExtractItem @@ -117,6 +117,7 @@ "ViewItem", "VirtualConnectionItem", "WebhookItem", + "WebhookTestResultItem", "WorkbookItem", "LinkedTaskItem", "LinkedTaskStepItem", diff --git a/tableauserverclient/models/webhook_item.py b/tableauserverclient/models/webhook_item.py index a8dfe3ef0..ba819c342 100644 --- a/tableauserverclient/models/webhook_item.py +++ b/tableauserverclient/models/webhook_item.py @@ -120,3 +120,32 @@ def _parse_element(webhook_xml: ET.Element, ns) -> tuple: def __repr__(self) -> str: return f"" + + +class WebhookTestResultItem: + """ + The result of testing a webhook. + + The Test Webhook method sends an empty payload to the webhook destination + and returns what that destination sent back. ``id`` is the webhook id, + ``status`` is the destination's HTTP status, and ``body`` is the + destination's response body. + """ + + def __init__(self, id: str | None, status: str | None, body: str | None) -> None: + self.id = id + self.status = status + self.body = body + + def __repr__(self) -> str: + return f"" + + @classmethod + def from_response(cls, resp: bytes | str, ns) -> "WebhookTestResultItem": + parsed = fromstring(resp) + element = parsed.find(".//t:webhookTestResult", namespaces=ns) + if element is None: + raise ValueError("Webhook test response did not include a webhookTestResult element") + body_element = element.find("t:body", namespaces=ns) + body = None if body_element is None else body_element.text + return cls(element.get("id"), element.get("status"), body) diff --git a/tableauserverclient/server/endpoint/webhooks_endpoint.py b/tableauserverclient/server/endpoint/webhooks_endpoint.py index 449fdbdf5..3f58ebcb0 100644 --- a/tableauserverclient/server/endpoint/webhooks_endpoint.py +++ b/tableauserverclient/server/endpoint/webhooks_endpoint.py @@ -2,7 +2,7 @@ from .endpoint import Endpoint, api from tableauserverclient.server import RequestFactory -from tableauserverclient.models import WebhookItem, PaginationItem +from tableauserverclient.models import PaginationItem, WebhookItem, WebhookTestResultItem from tableauserverclient.helpers.logging import logger @@ -119,7 +119,7 @@ def create(self, webhook_item: WebhookItem) -> WebhookItem: return new_webhook @api(version="3.6") - def test(self, webhook_id: str): + def test(self, webhook_id: str) -> WebhookTestResultItem: """ Tests the specified webhook. Sends an empty payload to the configured destination URL of the webhook and returns the response from the server. @@ -135,13 +135,15 @@ def test(self, webhook_id: str): Returns ------- - XML Response + WebhookTestResultItem + The destination's status and body, parsed from webhookTestResult. """ if not webhook_id: error = "Webhook ID undefined." raise ValueError(error) url = f"{self.baseurl}/{webhook_id}/test" - testOutcome = self.get_request(url) - logger.info(f"Testing webhook (ID: {webhook_id} returned {testOutcome})") - return testOutcome + server_response = self.get_request(url) + result = WebhookTestResultItem.from_response(server_response.content, self.parent_srv.namespace) + logger.info(f"Testing webhook (ID: {webhook_id}) returned status {result.status}") + return result diff --git a/test/test_webhook.py b/test/test_webhook.py index 4fa011da0..54d34212e 100644 --- a/test/test_webhook.py +++ b/test/test_webhook.py @@ -61,9 +61,21 @@ def test_delete_missing_id(server: TSC.Server) -> None: def test_test(server: TSC.Server) -> None: + response_xml = """ + + + {"result": "success"} + + + """ with requests_mock.mock() as m: - m.get(server.webhooks.baseurl + "/ee8c6e70-43b6-11e6-af4f-f7b0d8e20760/test", status_code=200) - server.webhooks.test("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + m.get(server.webhooks.baseurl + "/ee8c6e70-43b6-11e6-af4f-f7b0d8e20760/test", text=response_xml) + result = server.webhooks.test("ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + + assert isinstance(result, TSC.WebhookTestResultItem) + assert result.id == "ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" + assert result.status == "200" + assert result.body == '{"result": "success"}' def test_create(server: TSC.Server) -> None: