Skip to content

Commit 76f6769

Browse files
authored
feat: extend status parameter to an array of possible statuses (#455)
The `status` parameter in the Apify Core API has been extended on the following endpoints: - `/v2/acts/:actorId/runs` - `/v2/actor-runs` - `/v2/actor-tasks/:actorTaskId/runs` It is now a comma-separated string of possible statuses. This PR updates the python client accordingly by modifying the `RunCollectionClient.list()` and `RunCollectionClientAsync.list()` methods to reflect the API change. Test case `TestRunCollectionSync` was added to cover the change
1 parent ba9bb46 commit 76f6769

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

src/apify_client/clients/resource_clients/run_collection.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def list(
2626
limit: int | None = None,
2727
offset: int | None = None,
2828
desc: bool | None = None,
29-
status: ActorJobStatus | None = None,
29+
status: ActorJobStatus | list[ActorJobStatus] | None = None,
3030
) -> ListPage[dict]:
3131
"""List all Actor runs.
3232
@@ -40,16 +40,21 @@ def list(
4040
limit: How many runs to retrieve.
4141
offset: What run to include as first when retrieving the list.
4242
desc: Whether to sort the runs in descending order based on their start date.
43-
status: Retrieve only runs with the provided status.
43+
status: Retrieve only runs with the provided statuses.
4444
4545
Returns:
4646
The retrieved Actor runs.
4747
"""
48+
if isinstance(status, list):
49+
status_param = [maybe_extract_enum_member_value(s) for s in status]
50+
else:
51+
status_param = maybe_extract_enum_member_value(status)
52+
4853
return self._list(
4954
limit=limit,
5055
offset=offset,
5156
desc=desc,
52-
status=maybe_extract_enum_member_value(status),
57+
status=status_param,
5358
)
5459

5560

@@ -67,7 +72,7 @@ async def list(
6772
limit: int | None = None,
6873
offset: int | None = None,
6974
desc: bool | None = None,
70-
status: ActorJobStatus | None = None,
75+
status: ActorJobStatus | list[ActorJobStatus] | None = None,
7176
) -> ListPage[dict]:
7277
"""List all Actor runs.
7378
@@ -81,14 +86,19 @@ async def list(
8186
limit: How many runs to retrieve.
8287
offset: What run to include as first when retrieving the list.
8388
desc: Whether to sort the runs in descending order based on their start date.
84-
status: Retrieve only runs with the provided status.
89+
status: Retrieve only runs with the provided statuses.
8590
8691
Returns:
8792
The retrieved Actor runs.
8893
"""
94+
if isinstance(status, list):
95+
status_param = [maybe_extract_enum_member_value(s) for s in status]
96+
else:
97+
status_param = maybe_extract_enum_member_value(status)
98+
8999
return await self._list(
90100
limit=limit,
91101
offset=offset,
92102
desc=desc,
93-
status=maybe_extract_enum_member_value(status),
103+
status=status_param,
94104
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
if TYPE_CHECKING:
8+
from apify_client import ApifyClient
9+
10+
from apify_shared.consts import ActorJobStatus
11+
12+
pytestmark = pytest.mark.integration
13+
14+
15+
class TestRunCollectionSync:
16+
APIFY_HELLO_WORLD_ACTOR = 'apify/hello-world'
17+
created_runs: list[dict]
18+
19+
def setup_runs(self, apify_client: ApifyClient) -> None:
20+
self.created_runs = []
21+
22+
successfull_run = apify_client.actor(self.APIFY_HELLO_WORLD_ACTOR).call()
23+
if successfull_run is not None:
24+
self.created_runs.append(successfull_run)
25+
26+
timed_out_run = apify_client.actor(self.APIFY_HELLO_WORLD_ACTOR).call(timeout_secs=1)
27+
if timed_out_run is not None:
28+
self.created_runs.append(timed_out_run)
29+
30+
def teadown_runs(self, apify_client: ApifyClient) -> None:
31+
for run in self.created_runs:
32+
run_id = run.get('id')
33+
if isinstance(run_id, str):
34+
apify_client.run(run_id).delete()
35+
36+
async def test_run_collection_list_multiple_statuses(self, apify_client: ApifyClient) -> None:
37+
self.setup_runs(apify_client)
38+
39+
run_collection = apify_client.actor(self.APIFY_HELLO_WORLD_ACTOR).runs()
40+
41+
multiple_status_runs = run_collection.list(status=[ActorJobStatus.SUCCEEDED, ActorJobStatus.TIMED_OUT])
42+
single_status_runs = run_collection.list(status=ActorJobStatus.SUCCEEDED)
43+
44+
assert multiple_status_runs is not None
45+
assert single_status_runs is not None
46+
47+
assert hasattr(multiple_status_runs, 'items')
48+
assert hasattr(single_status_runs, 'items')
49+
50+
assert all(
51+
run.get('status') in [ActorJobStatus.SUCCEEDED, ActorJobStatus.TIMED_OUT]
52+
for run in multiple_status_runs.items
53+
)
54+
assert all(run.get('status') == ActorJobStatus.SUCCEEDED for run in single_status_runs.items)
55+
56+
self.teadown_runs(apify_client)

0 commit comments

Comments
 (0)