Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tableauserverclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
ViewItem,
VirtualConnectionItem,
WebhookItem,
WebhookTestResultItem,
WeeklyInterval,
WorkbookItem,
)
Expand Down Expand Up @@ -159,6 +160,7 @@
"ViewItem",
"VirtualConnectionItem",
"WebhookItem",
"WebhookTestResultItem",
"WeeklyInterval",
"WorkbookItem",
]
3 changes: 2 additions & 1 deletion tableauserverclient/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -117,6 +117,7 @@
"ViewItem",
"VirtualConnectionItem",
"WebhookItem",
"WebhookTestResultItem",
"WorkbookItem",
"LinkedTaskItem",
"LinkedTaskStepItem",
Expand Down
29 changes: 29 additions & 0 deletions tableauserverclient/models/webhook_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,32 @@ def _parse_element(webhook_xml: ET.Element, ns) -> tuple:

def __repr__(self) -> str:
return f"<Webhook id={self.id} name={self.name} url={self.url} event={self.event}>"


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"<WebhookTestResult id={self.id} status={self.status}>"

@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)
14 changes: 8 additions & 6 deletions tableauserverclient/server/endpoint/webhooks_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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
16 changes: 14 additions & 2 deletions test/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,21 @@ def test_delete_missing_id(server: TSC.Server) -> None:


def test_test(server: TSC.Server) -> None:
response_xml = """<?xml version="1.0" encoding="UTF-8"?>
<tsResponse xmlns="http://tableau.com/api">
<webhookTestResult id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" status="200">
<body>{"result": "success"}</body>
</webhookTestResult>
</tsResponse>
"""
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:
Expand Down