Skip to content

Commit 723ec23

Browse files
vdusekclaude
andcommitted
refactor!: re-use apify-client models/literals instead of SDK duplicates
Drop SDK-side model/literal classes that the apify-client v3 already provides to keep the SDK from drifting out of sync with the API schema: - `apify.Webhook` is now an alias for `apify_client._models.WebhookRepresentation`. - `ActorPermissionLevel`, `WebhookCondition`, `WebhookCreate`, `WebhookRepresentation` are re-exported from `apify` for convenience. - `Actor.add_webhook` takes a `WebhookCreate` instance instead of the SDK `Webhook` plus separate kwargs; this also fixes a silent bug where `headers_template` was discarded when forwarding to the API. - Internal manual webhook serialization in `Actor.start/call/call_task` was dropped — the client v3 accepts `WebhookRepresentation` directly. - `RequestQueueStats` in storage_clients re-exports the client model (fields are now `int | None`; arithmetic in tests guarded with `or 0`). - Deleted unused dead code: `ProlongRequestLockResponse`, `KeyValueStoreKeyInfo`, `KeyValueStoreListKeysPage`, and the now-empty `apify/_models.py` module. `PricingModel` Literal moved to `_charging.py` (its only consumer). BREAKING CHANGE: `Actor.add_webhook` signature changed; `apify.Webhook` no longer has all the optional fields it used to expose. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent cf1ce01 commit 723ec23

13 files changed

Lines changed: 89 additions & 169 deletions

File tree

‎docs/02_concepts/code/07_webhook.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import asyncio
22

3-
from apify import Actor, Webhook
3+
from apify import Actor, WebhookCondition, WebhookCreate
44

55

66
async def main() -> None:
77
async with Actor:
88
# Create a webhook that will be triggered when the Actor run fails.
9-
webhook = Webhook(
9+
webhook = WebhookCreate(
1010
event_types=['ACTOR.RUN.FAILED'],
1111
request_url='https://example.com/run-failed',
12+
condition=WebhookCondition(),
1213
)
1314

1415
# Add the webhook to the Actor.

‎docs/02_concepts/code/07_webhook_preventing.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import asyncio
22

3-
from apify import Actor, Webhook
3+
from apify import Actor, WebhookCondition, WebhookCreate
44

55

66
async def main() -> None:
77
async with Actor:
88
# Create a webhook that will be triggered when the Actor run fails.
9-
webhook = Webhook(
9+
webhook = WebhookCreate(
1010
event_types=['ACTOR.RUN.FAILED'],
1111
request_url='https://example.com/run-failed',
12+
condition=WebhookCondition(),
13+
idempotency_key=Actor.configuration.actor_run_id,
1214
)
1315

1416
# Add the webhook to the Actor.
15-
await Actor.add_webhook(webhook, idempotency_key=Actor.configuration.actor_run_id)
17+
await Actor.add_webhook(webhook)
1618

1719
# Raise an error to simulate a failed run.
1820
raise RuntimeError('I am an error and I know it!')

‎docs/04_upgrading/upgrading_to_v3.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ Some changes in the related model classes:
6767
- `stats` field in `RequestQueueMetadata` - removed as it was unused.
6868
- `RequestQueueHead` - replaced by `RequestQueueHeadWithLocks`.
6969

70+
## Webhook model changes
71+
72+
The SDK no longer ships its own `Webhook` Pydantic model. The webhook models from `apify-client` are now used directly:
73+
74+
- `apify.Webhook` is now an alias of `apify_client._models.WebhookRepresentation`. It still has the same shape used in `Actor.start`, `Actor.call`, and `Actor.call_task` (`event_types`, `request_url`, `payload_template`, `headers_template`).
75+
- `Actor.add_webhook` now accepts an `apify.WebhookCreate` instance instead of `apify.Webhook` + separate kwargs. All fields (`ignore_ssl_errors`, `do_not_retry`, `idempotency_key`, `headers_template`, etc.) move onto the `WebhookCreate` instance. `condition` is required by the underlying type — pass `WebhookCondition()` when you do not need to scope to other resources.
76+
77+
**Before (v2.x):**
78+
79+
```python
80+
from apify import Actor, Webhook
81+
82+
await Actor.add_webhook(
83+
Webhook(event_types=['ACTOR.RUN.FAILED'], request_url='https://example.com'),
84+
idempotency_key='my-key',
85+
)
86+
```
87+
88+
**Now (v3.0):**
89+
90+
```python
91+
from apify import Actor, WebhookCondition, WebhookCreate
92+
93+
await Actor.add_webhook(
94+
WebhookCreate(
95+
event_types=['ACTOR.RUN.FAILED'],
96+
request_url='https://example.com',
97+
condition=WebhookCondition(),
98+
idempotency_key='my-key',
99+
)
100+
)
101+
```
102+
103+
`WebhookCondition`, `WebhookCreate`, `WebhookRepresentation`, `WebhookEventType`, and `ActorPermissionLevel` are all re-exported from `apify` for convenience.
104+
70105
## Removed Actor.config property
71106
- `Actor.config` property has been removed. Use `Actor.configuration` instead.
72107

‎src/apify/__init__.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from importlib import metadata
22

3-
from apify_client._literals import WebhookEventType
3+
from apify_client._literals import ActorPermissionLevel, WebhookEventType
4+
from apify_client._models import WebhookCondition, WebhookCreate, WebhookRepresentation
5+
from apify_client._models import WebhookRepresentation as Webhook
46
from crawlee import Request
57
from crawlee.events import (
68
Event,
@@ -14,13 +16,13 @@
1416

1517
from apify._actor import Actor
1618
from apify._configuration import Configuration
17-
from apify._models import Webhook
1819
from apify._proxy_configuration import ProxyConfiguration, ProxyInfo
1920

2021
__version__ = metadata.version('apify')
2122

2223
__all__ = [
2324
'Actor',
25+
'ActorPermissionLevel',
2426
'Configuration',
2527
'Event',
2628
'EventAbortingData',
@@ -33,6 +35,9 @@
3335
'ProxyInfo',
3436
'Request',
3537
'Webhook',
38+
'WebhookCondition',
39+
'WebhookCreate',
3640
'WebhookEventType',
41+
'WebhookRepresentation',
3742
'__version__',
3843
]

‎src/apify/_actor.py‎

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@
4444
from types import TracebackType
4545
from typing import Self
4646

47-
from apify_client._models import Run
47+
from apify_client._literals import ActorPermissionLevel
48+
from apify_client._models import Run, WebhookCreate
49+
from apify_client._models import WebhookRepresentation as Webhook
4850
from crawlee._types import JsonSerializable
4951
from crawlee.proxy_configuration import _NewUrlFunction
5052

51-
from apify._consts import ActorPermissionLevel
52-
from apify._models import Webhook
53-
5453
MainReturnType = TypeVar('MainReturnType')
5554

5655
_ensure_context = ensure_context('_active')
@@ -907,13 +906,6 @@ async def start(
907906
"""
908907
client = self.new_client(token=token) if token else self.apify_client
909908

910-
if webhooks:
911-
serialized_webhooks = [
912-
hook.model_dump(by_alias=True, exclude_unset=True, exclude_defaults=True) for hook in webhooks
913-
]
914-
else:
915-
serialized_webhooks = None
916-
917909
if timeout in {'inherit', 'RemainingTime'}:
918910
if timeout == 'RemainingTime':
919911
warnings.warn(
@@ -942,7 +934,7 @@ async def start(
942934
run_timeout=actor_start_timeout,
943935
force_permission_level=force_permission_level,
944936
wait_for_finish=wait_for_finish,
945-
webhooks=serialized_webhooks,
937+
webhooks=webhooks,
946938
)
947939

948940
if run is None:
@@ -1040,13 +1032,6 @@ async def call(
10401032
"""
10411033
client = self.new_client(token=token) if token else self.apify_client
10421034

1043-
if webhooks:
1044-
serialized_webhooks = [
1045-
hook.model_dump(by_alias=True, exclude_unset=True, exclude_defaults=True) for hook in webhooks
1046-
]
1047-
else:
1048-
serialized_webhooks = None
1049-
10501035
if timeout in {'inherit', 'RemainingTime'}:
10511036
if timeout == 'RemainingTime':
10521037
warnings.warn(
@@ -1075,7 +1060,7 @@ async def call(
10751060
memory_mbytes=memory_mbytes,
10761061
run_timeout=actor_call_timeout,
10771062
force_permission_level=force_permission_level,
1078-
webhooks=serialized_webhooks,
1063+
webhooks=webhooks,
10791064
wait_duration=wait,
10801065
logger=logger,
10811066
)
@@ -1131,13 +1116,6 @@ async def call_task(
11311116
"""
11321117
client = self.new_client(token=token) if token else self.apify_client
11331118

1134-
if webhooks:
1135-
serialized_webhooks = [
1136-
hook.model_dump(by_alias=True, exclude_unset=True, exclude_defaults=True) for hook in webhooks
1137-
]
1138-
else:
1139-
serialized_webhooks = None
1140-
11411119
if timeout == 'inherit':
11421120
task_call_timeout = self._get_remaining_time()
11431121
elif timeout is None:
@@ -1154,7 +1132,7 @@ async def call_task(
11541132
restart_on_error=restart_on_error,
11551133
memory_mbytes=memory_mbytes,
11561134
run_timeout=task_call_timeout,
1157-
webhooks=serialized_webhooks,
1135+
webhooks=webhooks,
11581136
wait_duration=wait,
11591137
)
11601138

@@ -1276,14 +1254,7 @@ async def reboot(
12761254
await asyncio.sleep(custom_after_sleep.total_seconds())
12771255

12781256
@_ensure_context
1279-
async def add_webhook(
1280-
self,
1281-
webhook: Webhook,
1282-
*,
1283-
ignore_ssl_errors: bool | None = None,
1284-
do_not_retry: bool | None = None,
1285-
idempotency_key: str | None = None,
1286-
) -> None:
1257+
async def add_webhook(self, webhook: WebhookCreate) -> None:
12871258
"""Create an ad-hoc webhook for the current Actor run.
12881259
12891260
This webhook lets you receive a notification when the Actor run finished or failed.
@@ -1294,11 +1265,7 @@ async def add_webhook(
12941265
For more information about Apify Actor webhooks, please see the [documentation](https://docs.apify.com/webhooks).
12951266
12961267
Args:
1297-
webhook: The webhook to be added
1298-
ignore_ssl_errors: Whether the webhook should ignore SSL errors returned by request_url
1299-
do_not_retry: Whether the webhook should retry sending the payload to request_url upon failure.
1300-
idempotency_key: A unique identifier of a webhook. You can use it to ensure that you won't create
1301-
the same webhook multiple times.
1268+
webhook: The webhook to be added. The `condition.actor_run_id` is automatically set to the current run.
13021269
13031270
Returns:
13041271
The created webhook.
@@ -1316,9 +1283,11 @@ async def add_webhook(
13161283
event_types=webhook.event_types,
13171284
request_url=webhook.request_url,
13181285
payload_template=webhook.payload_template,
1319-
ignore_ssl_errors=ignore_ssl_errors,
1320-
do_not_retry=do_not_retry,
1321-
idempotency_key=idempotency_key,
1286+
headers_template=webhook.headers_template,
1287+
ignore_ssl_errors=webhook.ignore_ssl_errors,
1288+
do_not_retry=webhook.do_not_retry,
1289+
idempotency_key=webhook.idempotency_key,
1290+
is_ad_hoc=webhook.is_ad_hoc if webhook.is_ad_hoc is not None else True,
13221291
)
13231292

13241293
@_ensure_context

‎src/apify/_charging.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass
66
from datetime import UTC, datetime
77
from decimal import Decimal
8-
from typing import TYPE_CHECKING, Protocol, TypedDict
8+
from typing import TYPE_CHECKING, Literal, Protocol, TypedDict
99

1010
from apify_client._models import (
1111
FlatPricePerMonthActorPricingInfo,
@@ -24,7 +24,9 @@
2424
from apify_client import ApifyClientAsync
2525

2626
from apify._configuration import Configuration
27-
from apify._models import PricingModel
27+
28+
PricingModel = Literal['PAY_PER_EVENT', 'PRICE_PER_DATASET_ITEM', 'FLAT_PRICE_PER_MONTH', 'FREE']
29+
"""Pricing model for an Actor."""
2830

2931
DEFAULT_DATASET_ITEM_EVENT = 'apify-default-dataset-item'
3032

‎src/apify/_consts.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import re
44
from datetime import timedelta
55
from enum import Enum, StrEnum
6-
from typing import Literal
7-
8-
ActorPermissionLevel = Literal['LIMITED_PERMISSIONS', 'FULL_PERMISSIONS']
9-
"""Determines permissions that the Actor requires to run. See the Apify Actor permissions documentation."""
106

117
EVENT_LISTENERS_TIMEOUT = timedelta(seconds=5)
128
"""Timeout for waiting on event listeners to finish during Actor exit."""

‎src/apify/_models.py‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎src/apify/storage_clients/_apify/_key_value_store_client.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ async def iterate_keys(
157157
list_key_page = await self._api_client.list_keys(exclusive_start_key=exclusive_start_key)
158158

159159
for item in list_key_page.items:
160-
# Convert KeyValueStoreKeyInfo to KeyValueStoreRecordMetadata
161160
record_metadata = KeyValueStoreRecordMetadata(
162161
key=item.key,
163162
size=int(item.size),

0 commit comments

Comments
 (0)