Skip to content

Commit 1aaa196

Browse files
authored
fix: getting storages of last run (#241)
### Issues - Closes #231 - Closes #240 ### Testing - abort last run Sync Actor - works: ```python from apify_client import ApifyClient TOKEN = '...' ACTOR_ID = '...' def actor_run_abort(apify_client: ApifyClient, actor_id: str) -> None: actor_client = apify_client.actor(actor_id) actor_client.call(wait_secs=1) last_run = actor_client.last_run(status='RUNNING', origin='API') aborted_info = last_run.abort() print(f'aborted_info: {aborted_info}') if __name__ == '__main__': apify_client = ApifyClient(TOKEN) actor_run_abort(apify_client, ACTOR_ID) ``` Async Actor - works: ```python import asyncio from apify_client import ApifyClientAsync TOKEN = '...' ACTOR_ID = '...' async def actor_run_abort_async(apify_client: ApifyClientAsync, actor_id: str) -> None: actor_client = apify_client.actor(actor_id) await actor_client.call(wait_secs=1) last_run = actor_client.last_run(status='RUNNING', origin='API') aborted_info = await last_run.abort() print(f'aborted_info: {aborted_info}') if __name__ == '__main__': apify_client = ApifyClientAsync(TOKEN) asyncio.run(actor_run_abort_async(apify_client, ACTOR_ID)) ``` Sync task - works: ```python from apify_client import ApifyClient TOKEN = '...' TASK_ID = '...' def task_run_abort(apify_client: ApifyClient, task_id: str) -> None: task_client = apify_client.task(task_id) task_client.call(wait_secs=1) last_run = task_client.last_run(status='RUNNING', origin='API') aborted_info = last_run.abort() print(f'aborted_info: {aborted_info}') if __name__ == '__main__': apify_client = ApifyClient(TOKEN) task_run_abort(apify_client, TASK_ID) ``` Async task - works: ```python import asyncio from apify_client import ApifyClientAsync TOKEN = '...' TASK_ID = '...' async def task_run_abort_async(apify_client: ApifyClientAsync, task_id: str) -> None: task_client = apify_client.task(task_id) await task_client.call(wait_secs=1) last_run = task_client.last_run(status='RUNNING', origin='API') aborted_info = await last_run.abort() print(f'aborted_info: {aborted_info}') if __name__ == '__main__': apify_client = ApifyClientAsync(TOKEN) asyncio.run(task_run_abort_async(apify_client, TASK_ID)) ``` ### Testing - get storage of last run Sync Actor - works: ```python from apify_client import ApifyClient TOKEN = '...' ACTOR_ID = '...' def actor_run_storage(apify_client: ApifyClient, actor_id: str) -> None: actor_client = apify_client.actor(actor_id) last_run = actor_client.last_run() last_run_dataset_client = last_run.dataset() info = last_run_dataset_client.get() print(f'info: {info}') if __name__ == '__main__': apify_client = ApifyClient(TOKEN) task_run_storage(apify_client, ACTOR_ID) ``` Async Actor - works: ```python import asyncio from apify_client import ApifyClientAsync TOKEN = '...' ACTOR_ID = '...' async def actor_run_storage(apify_client: ApifyClientAsync, actor_id: str) -> None: actor_client = apify_client.actor(actor_id) last_run = actor_client.last_run() last_run_dataset_client = last_run.dataset() info = await last_run_dataset_client.get() print(f'info: {info}') if __name__ == '__main__': apify_client = ApifyClientAsync(TOKEN) asyncio.run(task_run_storage(apify_client, ACTOR_ID)) ``` Sync task - works: ```python from apify_client import ApifyClient TOKEN = '...' TASK_ID = '...' def task_run_storage(apify_client: ApifyClient, task_id: str) -> None: task_client = apify_client.task(task_id) task_client.call(wait_secs=1) last_run = task_client.last_run() last_run_dataset_client = last_run.dataset() info = last_run_dataset_client.get() print(f'info: {info}') if __name__ == '__main__': apify_client = ApifyClient(TOKEN) task_run_storage(apify_client, TASK_ID) ``` Async task - works: ```python import asyncio from apify_client import ApifyClientAsync TOKEN = '...' TASK_ID = '...' async def task_run_storage(apify_client: ApifyClientAsync, task_id: str) -> None: task_client = apify_client.task(task_id) await task_client.call(wait_secs=1) last_run = task_client.last_run() last_run_dataset_client = last_run.dataset() info = await last_run_dataset_client.get() print(f'info: {info}') if __name__ == '__main__': apify_client = ApifyClientAsync(TOKEN) asyncio.run(task_run_storage(apify_client, TASK_ID)) ```
1 parent a6df6d3 commit 1aaa196

File tree

3 files changed

+10
-91
lines changed

3 files changed

+10
-91
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## [1.7.1](../../releases/tag/v1.7.1) - Unreleased
44

5-
...
5+
### Fixed
6+
7+
- fix breaking change (sync -> async) in 1.7.0
8+
- fix getting storages of last run
69

710
## [1.7.0](../../releases/tag/v1.7.0) - 2024-05-20
811

src/apify_client/clients/resource_clients/actor.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,7 @@ def last_run(
344344
Returns:
345345
RunClient: The resource client for the last run of this actor.
346346
"""
347-
# Note:
348-
# The API does not provide a direct endpoint for aborting the last Actor run using a URL like:
349-
# https://api.apify.com/v2/acts/{actor_id}/runs/last/abort
350-
# To achieve this, we need to implement a workaround using the following URL format:
351-
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort
352-
353-
last_run_client = RunClient(
347+
return RunClient(
354348
**self._sub_resource_init_options(
355349
resource_id='last',
356350
resource_path='runs',
@@ -361,21 +355,6 @@ def last_run(
361355
)
362356
)
363357

364-
last_run_client_info = last_run_client.get()
365-
actor_id = last_run_client_info['actId'] # type: ignore
366-
actor_run_id = last_run_client_info['id'] # type: ignore
367-
368-
return RunClient(
369-
**self._sub_resource_init_options(
370-
base_url='https://api.apify.com/v2',
371-
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
372-
params=self._params(
373-
status=maybe_extract_enum_member_value(status),
374-
origin=maybe_extract_enum_member_value(origin),
375-
),
376-
)
377-
)
378-
379358
def versions(self: ActorClient) -> ActorVersionCollectionClient:
380359
"""Retrieve a client for the versions of this actor."""
381360
return ActorVersionCollectionClient(**self._sub_resource_init_options())
@@ -655,7 +634,7 @@ def runs(self: ActorClientAsync) -> RunCollectionClientAsync:
655634
"""Retrieve a client for the runs of this actor."""
656635
return RunCollectionClientAsync(**self._sub_resource_init_options(resource_path='runs'))
657636

658-
async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
637+
def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
659638
"""Retrieve the client for the last run of this actor.
660639
661640
Last run is retrieved based on the start time of the runs.
@@ -667,13 +646,7 @@ async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = No
667646
Returns:
668647
RunClientAsync: The resource client for the last run of this actor.
669648
"""
670-
# Note:
671-
# The API does not provide a direct endpoint for aborting the last Actor run using a URL like:
672-
# https://api.apify.com/v2/acts/{actor_id}/runs/last/abort
673-
# To achieve this, we need to implement a workaround using the following URL format:
674-
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort
675-
676-
last_run_client = RunClientAsync(
649+
return RunClientAsync(
677650
**self._sub_resource_init_options(
678651
resource_id='last',
679652
resource_path='runs',
@@ -684,21 +657,6 @@ async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = No
684657
)
685658
)
686659

687-
last_run_client_info = await last_run_client.get()
688-
actor_id = last_run_client_info['actId'] # type: ignore
689-
actor_run_id = last_run_client_info['id'] # type: ignore
690-
691-
return RunClientAsync(
692-
**self._sub_resource_init_options(
693-
base_url='https://api.apify.com/v2',
694-
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
695-
params=self._params(
696-
status=maybe_extract_enum_member_value(status),
697-
origin=maybe_extract_enum_member_value(origin),
698-
),
699-
)
700-
)
701-
702660
def versions(self: ActorClientAsync) -> ActorVersionCollectionClientAsync:
703661
"""Retrieve a client for the versions of this actor."""
704662
return ActorVersionCollectionClientAsync(**self._sub_resource_init_options())

src/apify_client/clients/resource_clients/task.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,7 @@ def last_run(self: TaskClient, *, status: ActorJobStatus | None = None, origin:
266266
Returns:
267267
RunClient: The resource client for the last run of this task.
268268
"""
269-
# Note:
270-
# The API does not provide a direct endpoint for aborting the last task run using a URL like:
271-
# https://api.apify.com/v2/actor-tasks/{task_id}/runs/last/abort
272-
# To achieve this, we need to implement a workaround using the following URL format:
273-
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort
274-
275-
last_run_client = RunClient(
269+
return RunClient(
276270
**self._sub_resource_init_options(
277271
resource_id='last',
278272
resource_path='runs',
@@ -283,21 +277,6 @@ def last_run(self: TaskClient, *, status: ActorJobStatus | None = None, origin:
283277
)
284278
)
285279

286-
last_run_client_info = last_run_client.get()
287-
actor_id = last_run_client_info['actId'] # type: ignore
288-
actor_run_id = last_run_client_info['id'] # type: ignore
289-
290-
return RunClient(
291-
**self._sub_resource_init_options(
292-
base_url='https://api.apify.com/v2',
293-
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
294-
params=self._params(
295-
status=maybe_extract_enum_member_value(status),
296-
origin=maybe_extract_enum_member_value(origin),
297-
),
298-
)
299-
)
300-
301280
def webhooks(self: TaskClient) -> WebhookCollectionClient:
302281
"""Retrieve a client for webhooks associated with this task."""
303282
return WebhookCollectionClient(**self._sub_resource_init_options())
@@ -512,7 +491,7 @@ def runs(self: TaskClientAsync) -> RunCollectionClientAsync:
512491
"""Retrieve a client for the runs of this task."""
513492
return RunCollectionClientAsync(**self._sub_resource_init_options(resource_path='runs'))
514493

515-
async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
494+
def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
516495
"""Retrieve the client for the last run of this task.
517496
518497
Last run is retrieved based on the start time of the runs.
@@ -524,13 +503,7 @@ async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = Non
524503
Returns:
525504
RunClientAsync: The resource client for the last run of this task.
526505
"""
527-
# Note:
528-
# The API does not provide a direct endpoint for aborting the last task run using a URL like:
529-
# https://api.apify.com/v2/actor-tasks/{task_id}/runs/last/abort
530-
# To achieve this, we need to implement a workaround using the following URL format:
531-
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort
532-
533-
last_run_client = RunClientAsync(
506+
return RunClientAsync(
534507
**self._sub_resource_init_options(
535508
resource_id='last',
536509
resource_path='runs',
@@ -541,21 +514,6 @@ async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = Non
541514
)
542515
)
543516

544-
last_run_client_info = await last_run_client.get()
545-
actor_id = last_run_client_info['actId'] # type: ignore
546-
actor_run_id = last_run_client_info['id'] # type: ignore
547-
548-
return RunClientAsync(
549-
**self._sub_resource_init_options(
550-
base_url='https://api.apify.com/v2',
551-
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
552-
params=self._params(
553-
status=maybe_extract_enum_member_value(status),
554-
origin=maybe_extract_enum_member_value(origin),
555-
),
556-
)
557-
)
558-
559517
def webhooks(self: TaskClientAsync) -> WebhookCollectionClientAsync:
560518
"""Retrieve a client for webhooks associated with this task."""
561519
return WebhookCollectionClientAsync(**self._sub_resource_init_options())

0 commit comments

Comments
 (0)