Skip to content
Merged
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
48 changes: 21 additions & 27 deletions tests/integration/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

from __future__ import annotations

from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from apify_client import ApifyClient, ApifyClientAsync
from apify_client._models import ListOfWebhookDispatches, ListOfWebhooks, Run, Webhook, WebhookDispatch


from ._utils import maybe_await
Expand All @@ -24,19 +23,24 @@ async def _get_finished_run_id(client: ApifyClient | ApifyClientAsync) -> str:
"""Get the ID of an already-completed run of the hello-world actor.

Using a finished run's ID for webhook conditions ensures the webhook will never actually fire,
since a completed run won't emit new events.
since a completed run won't emit new events. If no completed runs exist, starts a new run and
waits for it to finish.
"""
runs_page = await maybe_await(client.actor(HELLO_WORLD_ACTOR).runs().list(limit=1, status=ActorJobStatus.SUCCEEDED))
assert runs_page is not None
assert len(runs_page.items) > 0, 'No completed runs found for hello-world actor'
run = cast('Run', runs_page.items[0])

if len(runs_page.items) > 0:
return runs_page.items[0].id

# No completed runs found - start one and wait for it to finish
run = await maybe_await(client.actor(HELLO_WORLD_ACTOR).call())
assert run is not None
return run.id


async def test_list_webhooks(client: ApifyClient | ApifyClientAsync) -> None:
"""Test listing webhooks."""
result = await maybe_await(client.webhooks().list(limit=10))
webhooks_page = cast('ListOfWebhooks', result)
webhooks_page = await maybe_await(client.webhooks().list(limit=10))

assert webhooks_page is not None
assert webhooks_page.items is not None
Expand All @@ -46,8 +50,7 @@ async def test_list_webhooks(client: ApifyClient | ApifyClientAsync) -> None:

async def test_list_webhooks_pagination(client: ApifyClient | ApifyClientAsync) -> None:
"""Test listing webhooks with pagination."""
result = await maybe_await(client.webhooks().list(limit=5, offset=0))
webhooks_page = cast('ListOfWebhooks', result)
webhooks_page = await maybe_await(client.webhooks().list(limit=5, offset=0))

assert webhooks_page is not None
assert webhooks_page.items is not None
Expand All @@ -59,24 +62,22 @@ async def test_webhook_create_and_get(client: ApifyClient | ApifyClientAsync) ->
run_id = await _get_finished_run_id(client)

# Create webhook bound to a finished run (will never fire)
result = await maybe_await(
created_webhook = await maybe_await(
client.webhooks().create(
event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED],
request_url='https://httpbin.org/post',
actor_run_id=run_id,
is_ad_hoc=True,
)
)
created_webhook = cast('Webhook', result)
webhook_client = client.webhook(created_webhook.id)

try:
assert created_webhook is not None
assert created_webhook.id is not None

# Get the same webhook
result = await maybe_await(webhook_client.get())
retrieved_webhook = cast('Webhook', result)
retrieved_webhook = await maybe_await(webhook_client.get())
assert retrieved_webhook is not None
assert retrieved_webhook.id == created_webhook.id
finally:
Expand All @@ -88,26 +89,24 @@ async def test_webhook_update(client: ApifyClient | ApifyClientAsync) -> None:
run_id = await _get_finished_run_id(client)

# Create webhook bound to a finished run
result = await maybe_await(
created_webhook = await maybe_await(
client.webhooks().create(
event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED],
request_url='https://httpbin.org/post',
actor_run_id=run_id,
is_ad_hoc=True,
)
)
created_webhook = cast('Webhook', result)
webhook_client = client.webhook(created_webhook.id)

try:
# Update webhook
result = await maybe_await(
updated_webhook = await maybe_await(
webhook_client.update(
request_url='https://httpbin.org/anything',
actor_run_id=run_id,
)
)
updated_webhook = cast('Webhook', result)
assert str(updated_webhook.request_url) == 'https://httpbin.org/anything'
finally:
await maybe_await(webhook_client.delete())
Expand All @@ -118,21 +117,19 @@ async def test_webhook_test(client: ApifyClient | ApifyClientAsync) -> None:
run_id = await _get_finished_run_id(client)

# Create webhook bound to a finished run
result = await maybe_await(
created_webhook = await maybe_await(
client.webhooks().create(
event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED],
request_url='https://httpbin.org/post',
actor_run_id=run_id,
is_ad_hoc=True,
)
)
created_webhook = cast('Webhook', result)
webhook_client = client.webhook(created_webhook.id)

try:
# Test webhook (creates a dispatch with dummy payload)
result = await maybe_await(webhook_client.test())
dispatch = cast('WebhookDispatch', result)
dispatch = await maybe_await(webhook_client.test())
assert dispatch is not None
assert dispatch.id is not None
finally:
Expand All @@ -144,24 +141,22 @@ async def test_webhook_dispatches(client: ApifyClient | ApifyClientAsync) -> Non
run_id = await _get_finished_run_id(client)

# Create webhook bound to a finished run
result = await maybe_await(
created_webhook = await maybe_await(
client.webhooks().create(
event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED],
request_url='https://httpbin.org/post',
actor_run_id=run_id,
is_ad_hoc=True,
)
)
created_webhook = cast('Webhook', result)
webhook_client = client.webhook(created_webhook.id)

try:
# Test webhook to create a dispatch
await maybe_await(webhook_client.test())

# List dispatches for this webhook
result = await maybe_await(webhook_client.dispatches().list())
dispatches = cast('ListOfWebhookDispatches', result)
dispatches = await maybe_await(webhook_client.dispatches().list())
assert dispatches is not None
assert dispatches.items is not None
assert len(dispatches.items) > 0
Expand All @@ -174,15 +169,14 @@ async def test_webhook_delete(client: ApifyClient | ApifyClientAsync) -> None:
run_id = await _get_finished_run_id(client)

# Create webhook bound to a finished run
result = await maybe_await(
created_webhook = await maybe_await(
client.webhooks().create(
event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED],
request_url='https://httpbin.org/post',
actor_run_id=run_id,
is_ad_hoc=True,
)
)
created_webhook = cast('Webhook', result)
webhook_client = client.webhook(created_webhook.id)

# Delete webhook
Expand Down