Skip to content

Commit 0bf14f5

Browse files
committed
feat: default last_run to the last successful run
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ocuALBprYy13aVai5MaVb
1 parent 1790b0e commit 0bf14f5

3 files changed

Lines changed: 78 additions & 8 deletions

File tree

‎src/apify_client/_resource_clients/actor.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,16 @@ def default_build(
536536
def last_run(
537537
self,
538538
*,
539-
status: ActorJobStatus | None = None,
539+
status: ActorJobStatus | None = 'SUCCEEDED',
540540
origin: RunOrigin | None = None,
541541
) -> RunClient:
542542
"""Retrieve the client for the last run of this Actor.
543543
544544
Last run is retrieved based on the start time of the runs.
545545
546546
Args:
547-
status: Consider only runs with this status.
547+
status: Consider only runs with this status. Defaults to SUCCEEDED; pass None to consider runs
548+
of any status.
548549
origin: Consider only runs started with this origin.
549550
550551
Returns:
@@ -1139,15 +1140,16 @@ async def default_build(
11391140
def last_run(
11401141
self,
11411142
*,
1142-
status: ActorJobStatus | None = None,
1143+
status: ActorJobStatus | None = 'SUCCEEDED',
11431144
origin: RunOrigin | None = None,
11441145
) -> RunClientAsync:
11451146
"""Retrieve the client for the last run of this Actor.
11461147
11471148
Last run is retrieved based on the start time of the runs.
11481149
11491150
Args:
1150-
status: Consider only runs with this status.
1151+
status: Consider only runs with this status. Defaults to SUCCEEDED; pass None to consider runs
1152+
of any status.
11511153
origin: Consider only runs started with this origin.
11521154
11531155
Returns:

‎src/apify_client/_resource_clients/task.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,14 @@ def runs(self) -> RunCollectionClient:
429429
**self._base_client_kwargs,
430430
)
431431

432-
def last_run(self, *, status: ActorJobStatus | None = None, origin: RunOrigin | None = None) -> RunClient:
432+
def last_run(self, *, status: ActorJobStatus | None = 'SUCCEEDED', origin: RunOrigin | None = None) -> RunClient:
433433
"""Retrieve the client for the last run of this task.
434434
435435
Last run is retrieved based on the start time of the runs.
436436
437437
Args:
438-
status: Consider only runs with this status.
438+
status: Consider only runs with this status. Defaults to SUCCEEDED; pass None to consider runs
439+
of any status.
439440
origin: Consider only runs started with this origin.
440441
441442
Returns:
@@ -844,13 +845,16 @@ def runs(self) -> RunCollectionClientAsync:
844845
**self._base_client_kwargs,
845846
)
846847

847-
def last_run(self, *, status: ActorJobStatus | None = None, origin: RunOrigin | None = None) -> RunClientAsync:
848+
def last_run(
849+
self, *, status: ActorJobStatus | None = 'SUCCEEDED', origin: RunOrigin | None = None
850+
) -> RunClientAsync:
848851
"""Retrieve the client for the last run of this task.
849852
850853
Last run is retrieved based on the start time of the runs.
851854
852855
Args:
853-
status: Consider only runs with this status.
856+
status: Consider only runs with this status. Defaults to SUCCEEDED; pass None to consider runs
857+
of any status.
854858
origin: Consider only runs started with this origin.
855859
856860
Returns:

‎tests/unit/test_last_run_params.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,67 @@ async def test_last_run_filters_propagate_to_chained_clients_async(
7171
request, _ = httpserver.log[-1]
7272
assert request.args.get('status') == 'SUCCEEDED'
7373
assert request.args.get('origin') == 'WEB'
74+
75+
76+
@pytest.mark.parametrize(
77+
('resource', 'path_prefix'),
78+
[
79+
pytest.param('actor', 'actors', id='actor'),
80+
pytest.param('task', 'actor-tasks', id='task'),
81+
],
82+
)
83+
@pytest.mark.parametrize(
84+
('kwargs', 'expected_status'),
85+
[
86+
pytest.param({}, 'SUCCEEDED', id='default'),
87+
pytest.param({'status': 'FAILED'}, 'FAILED', id='explicit_status'),
88+
pytest.param({'status': None}, None, id='any_status'),
89+
],
90+
)
91+
def test_last_run_defaults_to_succeeded(
92+
httpserver: HTTPServer,
93+
resource: str,
94+
path_prefix: str,
95+
kwargs: dict,
96+
expected_status: str | None,
97+
) -> None:
98+
"""`last_run()` asks for the last successful run unless the caller picks another status or opts out."""
99+
httpserver.expect_request(f'/v2/{path_prefix}/resource-id/runs/last').respond_with_json(_NOT_FOUND_BODY, status=404)
100+
client = ApifyClient(token='test-token', api_url=httpserver.url_for('/').removesuffix('/'))
101+
102+
assert getattr(client, resource)('resource-id').last_run(**kwargs).get() is None
103+
104+
request, _ = httpserver.log[-1]
105+
assert request.args.get('status') == expected_status
106+
107+
108+
@pytest.mark.parametrize(
109+
('resource', 'path_prefix'),
110+
[
111+
pytest.param('actor', 'actors', id='actor'),
112+
pytest.param('task', 'actor-tasks', id='task'),
113+
],
114+
)
115+
@pytest.mark.parametrize(
116+
('kwargs', 'expected_status'),
117+
[
118+
pytest.param({}, 'SUCCEEDED', id='default'),
119+
pytest.param({'status': 'FAILED'}, 'FAILED', id='explicit_status'),
120+
pytest.param({'status': None}, None, id='any_status'),
121+
],
122+
)
123+
async def test_last_run_defaults_to_succeeded_async(
124+
httpserver: HTTPServer,
125+
resource: str,
126+
path_prefix: str,
127+
kwargs: dict,
128+
expected_status: str | None,
129+
) -> None:
130+
"""Async variant of `test_last_run_defaults_to_succeeded`."""
131+
httpserver.expect_request(f'/v2/{path_prefix}/resource-id/runs/last').respond_with_json(_NOT_FOUND_BODY, status=404)
132+
client = ApifyClientAsync(token='test-token', api_url=httpserver.url_for('/').removesuffix('/'))
133+
134+
assert await getattr(client, resource)('resource-id').last_run(**kwargs).get() is None
135+
136+
request, _ = httpserver.log[-1]
137+
assert request.args.get('status') == expected_status

0 commit comments

Comments
 (0)