Skip to content

Commit dd58d25

Browse files
committed
refactor!: Adapt to apify-client v3
1 parent 94bdd37 commit dd58d25

51 files changed

Lines changed: 1299 additions & 824 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/02_concepts/code/07_webhook.py

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

3-
from apify import Actor, Webhook, WebhookEventType
3+
from apify import Actor, Webhook
44

55

66
async def main() -> None:
77
async with Actor:
88
# Create a webhook that will be triggered when the Actor run fails.
99
webhook = Webhook(
10-
event_types=[WebhookEventType.ACTOR_RUN_FAILED],
10+
event_types=['ACTOR.RUN.FAILED'],
1111
request_url='https://example.com/run-failed',
1212
)
1313

docs/02_concepts/code/07_webhook_preventing.py

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

3-
from apify import Actor, Webhook, WebhookEventType
3+
from apify import Actor, Webhook
44

55

66
async def main() -> None:
77
async with Actor:
8-
# Create a webhook that will be triggered when the Actor run fails.
8+
# Create a webhook with an idempotency key to prevent duplicates on retries.
99
webhook = Webhook(
10-
event_types=[WebhookEventType.ACTOR_RUN_FAILED],
10+
event_types=['ACTOR.RUN.FAILED'],
1111
request_url='https://example.com/run-failed',
12+
idempotency_key=Actor.configuration.actor_run_id,
1213
)
1314

1415
# Add the webhook to the Actor.
15-
await Actor.add_webhook(webhook, idempotency_key=Actor.configuration.actor_run_id)
16+
await Actor.add_webhook(webhook)
1617

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

docs/04_upgrading/upgrading_to_v4.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,78 @@ title: Upgrading to v4
44
description: Breaking changes and migration guide from Apify SDK v3.x to v4.0.
55
---
66

7-
This guide lists the breaking changes between Apify Python SDK v3.x and v4.0.
7+
This page summarizes the breaking changes between Apify Python SDK v3.x and v4.0.
88

99
## Python 3.11+ required
1010

11-
Support for Python 3.10 has been dropped. The Apify Python SDK v4.x now requires Python 3.11 or later — make sure your environment is on a compatible version before upgrading.
11+
Support for Python 3.10 has been dropped. Apify Python SDK v4.x requires Python 3.11 or later.
12+
13+
## `apify-client` v3 required
14+
15+
The SDK is now built on `apify-client` v3 and no longer depends on `apify-shared`. Three changes are user-visible:
16+
17+
- `Actor.start`, `Actor.call`, `Actor.call_task`, and `Actor.metamorph` return `apify_client._models.Run` instead of the SDK-side `ActorRun`. The shape is equivalent — only the import path changes.
18+
- `apify.WebhookEventType` is now a `Literal[...]` instead of a `StrEnum`. Use plain string values (`'ACTOR.RUN.FAILED'`) instead of enum members.
19+
- `apify_shared.consts.ActorEventTypes` (a `StrEnum`) is replaced by `apify.ActorEventTypes`, now a `Literal['systemInfo', 'persistState', 'migrating', 'aborting']`. For runtime values, use `apify.Event` (re-exported from Crawlee) instead of enum members.
20+
21+
**Before (v3.x):**
22+
23+
```python
24+
from apify import Actor
25+
from apify_shared.consts import ActorEventTypes
26+
27+
Actor.on(ActorEventTypes.SYSTEM_INFO, callback)
28+
```
29+
30+
**Now (v4.0):**
31+
32+
```python
33+
from apify import Actor, Event
34+
35+
Actor.on(Event.SYSTEM_INFO, callback)
36+
```
37+
38+
## `Webhook` API simplified
39+
40+
The `Webhook` model has been slimmed down to only the fields a user sets when defining a webhook. Server-populated response fields (`id`, `created_at`, `modified_at`, `user_id`, `is_ad_hoc`, `condition`, `last_dispatch`, `stats`) and the unused `WebhookCondition` helper class have been removed. `Webhook` is now a plain `@dataclass` instead of a Pydantic `BaseModel` — construct it with snake_case kwargs; `.model_dump()` / `.model_validate()` are gone.
41+
42+
The retry and idempotency kwargs that used to live on `Actor.add_webhook` have moved onto the `Webhook` instance itself.
43+
44+
**Before (v3.x):**
45+
46+
```python
47+
from apify import Actor, Webhook
48+
49+
await Actor.add_webhook(
50+
Webhook(event_types=['ACTOR.RUN.FAILED'], request_url='https://example.com'),
51+
ignore_ssl_errors=False,
52+
do_not_retry=False,
53+
idempotency_key='my-key',
54+
)
55+
```
56+
57+
**Now (v4.0):**
58+
59+
```python
60+
from apify import Actor, Webhook
61+
62+
await Actor.add_webhook(
63+
Webhook(
64+
event_types=['ACTOR.RUN.FAILED'],
65+
request_url='https://example.com',
66+
ignore_ssl_errors=False,
67+
do_not_retry=False,
68+
idempotency_key='my-key',
69+
)
70+
)
71+
```
72+
73+
The `idempotency_key` kwarg form on `Actor.add_webhook` still works for one more release but emits a `DeprecationWarning` and will be removed in v5.0. The `ignore_ssl_errors` and `do_not_retry` kwargs have been removed outright — set them on the `Webhook` instance.
74+
75+
`apify.WebhookCondition` is no longer exported; the SDK now binds the webhook to the current Actor run internally.
76+
77+
The `webhooks` argument on `Actor.start`, `Actor.call`, and `Actor.call_task` still accepts `list[Webhook]` and the fields used at the call site (`event_types`, `request_url`, `payload_template`, `headers_template`) are unchanged.
78+
79+
## `Actor.new_client``timeout` scales all tiers
80+
81+
`apify-client` v3 split its single timeout into four tiers (short / medium / long / max). `Actor.new_client(timeout=...)` still takes a single `timedelta`; the SDK uses it as the medium-tier baseline and scales the other tiers proportionally (short = `timeout / 6`, long = `timeout * 12`, max = `timeout * 24`). The public signature is unchanged — no migration needed.

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ keywords = [
3434
"scraping",
3535
]
3636
dependencies = [
37-
"apify-client>=2.3.0,<3.0.0",
38-
"apify-shared>=2.0.0,<3.0.0",
37+
"apify-client>=3.0.0,<4.0.0",
3938
"crawlee>=1.0.4,<2.0.0",
4039
"cachetools>=5.5.0",
4140
"cryptography>=42.0.0",
4241
"impit>=0.8.0",
4342
"lazy-object-proxy>=1.11.0",
4443
"more_itertools>=10.2.0",
45-
"pydantic>=2.11.0",
44+
"pydantic[email]>=2.11.0",
4645
"typing-extensions>=4.1.0",
4746
"websockets>=14.0",
4847
"yarl>=1.18.0",
@@ -197,7 +196,7 @@ builtins-ignorelist = ["id"]
197196

198197
[tool.ruff.lint.isort]
199198
known-local-folder = ["apify"]
200-
known-first-party = ["apify_client", "apify_shared", "crawlee"]
199+
known-first-party = ["apify_client", "crawlee"]
201200

202201
[tool.ruff.lint.pylint]
203202
max-branches = 18

src/apify/__init__.py

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

3-
from apify_shared.consts import WebhookEventType
3+
from apify_client._literals import WebhookEventType
44
from crawlee import Request
55
from crawlee.events import (
66
Event,
@@ -14,13 +14,15 @@
1414

1515
from apify._actor import Actor
1616
from apify._configuration import Configuration
17-
from apify._models import Webhook
1817
from apify._proxy_configuration import ProxyConfiguration, ProxyInfo
18+
from apify._webhook import Webhook
19+
from apify.events._types import ActorEventTypes
1920

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

2223
__all__ = [
2324
'Actor',
25+
'ActorEventTypes',
2426
'Configuration',
2527
'Event',
2628
'EventAbortingData',

0 commit comments

Comments
 (0)