Skip to content

Commit

Permalink
fix: getting storages of last run (#241)
Browse files Browse the repository at this point in the history
### 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))
```
  • Loading branch information
vdusek authored Jul 11, 2024
1 parent a6df6d3 commit 1aaa196
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 91 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

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

...
### Fixed

- fix breaking change (sync -> async) in 1.7.0
- fix getting storages of last run

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

Expand Down
48 changes: 3 additions & 45 deletions src/apify_client/clients/resource_clients/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,7 @@ def last_run(
Returns:
RunClient: The resource client for the last run of this actor.
"""
# Note:
# The API does not provide a direct endpoint for aborting the last Actor run using a URL like:
# https://api.apify.com/v2/acts/{actor_id}/runs/last/abort
# To achieve this, we need to implement a workaround using the following URL format:
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort

last_run_client = RunClient(
return RunClient(
**self._sub_resource_init_options(
resource_id='last',
resource_path='runs',
Expand All @@ -361,21 +355,6 @@ def last_run(
)
)

last_run_client_info = last_run_client.get()
actor_id = last_run_client_info['actId'] # type: ignore
actor_run_id = last_run_client_info['id'] # type: ignore

return RunClient(
**self._sub_resource_init_options(
base_url='https://api.apify.com/v2',
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
params=self._params(
status=maybe_extract_enum_member_value(status),
origin=maybe_extract_enum_member_value(origin),
),
)
)

def versions(self: ActorClient) -> ActorVersionCollectionClient:
"""Retrieve a client for the versions of this actor."""
return ActorVersionCollectionClient(**self._sub_resource_init_options())
Expand Down Expand Up @@ -655,7 +634,7 @@ def runs(self: ActorClientAsync) -> RunCollectionClientAsync:
"""Retrieve a client for the runs of this actor."""
return RunCollectionClientAsync(**self._sub_resource_init_options(resource_path='runs'))

async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
"""Retrieve the client for the last run of this actor.
Last run is retrieved based on the start time of the runs.
Expand All @@ -667,13 +646,7 @@ async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = No
Returns:
RunClientAsync: The resource client for the last run of this actor.
"""
# Note:
# The API does not provide a direct endpoint for aborting the last Actor run using a URL like:
# https://api.apify.com/v2/acts/{actor_id}/runs/last/abort
# To achieve this, we need to implement a workaround using the following URL format:
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort

last_run_client = RunClientAsync(
return RunClientAsync(
**self._sub_resource_init_options(
resource_id='last',
resource_path='runs',
Expand All @@ -684,21 +657,6 @@ async def last_run(self: ActorClientAsync, *, status: ActorJobStatus | None = No
)
)

last_run_client_info = await last_run_client.get()
actor_id = last_run_client_info['actId'] # type: ignore
actor_run_id = last_run_client_info['id'] # type: ignore

return RunClientAsync(
**self._sub_resource_init_options(
base_url='https://api.apify.com/v2',
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
params=self._params(
status=maybe_extract_enum_member_value(status),
origin=maybe_extract_enum_member_value(origin),
),
)
)

def versions(self: ActorClientAsync) -> ActorVersionCollectionClientAsync:
"""Retrieve a client for the versions of this actor."""
return ActorVersionCollectionClientAsync(**self._sub_resource_init_options())
Expand Down
48 changes: 3 additions & 45 deletions src/apify_client/clients/resource_clients/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,7 @@ def last_run(self: TaskClient, *, status: ActorJobStatus | None = None, origin:
Returns:
RunClient: The resource client for the last run of this task.
"""
# Note:
# The API does not provide a direct endpoint for aborting the last task run using a URL like:
# https://api.apify.com/v2/actor-tasks/{task_id}/runs/last/abort
# To achieve this, we need to implement a workaround using the following URL format:
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort

last_run_client = RunClient(
return RunClient(
**self._sub_resource_init_options(
resource_id='last',
resource_path='runs',
Expand All @@ -283,21 +277,6 @@ def last_run(self: TaskClient, *, status: ActorJobStatus | None = None, origin:
)
)

last_run_client_info = last_run_client.get()
actor_id = last_run_client_info['actId'] # type: ignore
actor_run_id = last_run_client_info['id'] # type: ignore

return RunClient(
**self._sub_resource_init_options(
base_url='https://api.apify.com/v2',
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
params=self._params(
status=maybe_extract_enum_member_value(status),
origin=maybe_extract_enum_member_value(origin),
),
)
)

def webhooks(self: TaskClient) -> WebhookCollectionClient:
"""Retrieve a client for webhooks associated with this task."""
return WebhookCollectionClient(**self._sub_resource_init_options())
Expand Down Expand Up @@ -512,7 +491,7 @@ def runs(self: TaskClientAsync) -> RunCollectionClientAsync:
"""Retrieve a client for the runs of this task."""
return RunCollectionClientAsync(**self._sub_resource_init_options(resource_path='runs'))

async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = None, origin: MetaOrigin | None = None) -> RunClientAsync:
"""Retrieve the client for the last run of this task.
Last run is retrieved based on the start time of the runs.
Expand All @@ -524,13 +503,7 @@ async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = Non
Returns:
RunClientAsync: The resource client for the last run of this task.
"""
# Note:
# The API does not provide a direct endpoint for aborting the last task run using a URL like:
# https://api.apify.com/v2/actor-tasks/{task_id}/runs/last/abort
# To achieve this, we need to implement a workaround using the following URL format:
# https://api.apify.com/v2/acts/{actorId}/runs/{runId}/abort

last_run_client = RunClientAsync(
return RunClientAsync(
**self._sub_resource_init_options(
resource_id='last',
resource_path='runs',
Expand All @@ -541,21 +514,6 @@ async def last_run(self: TaskClientAsync, *, status: ActorJobStatus | None = Non
)
)

last_run_client_info = await last_run_client.get()
actor_id = last_run_client_info['actId'] # type: ignore
actor_run_id = last_run_client_info['id'] # type: ignore

return RunClientAsync(
**self._sub_resource_init_options(
base_url='https://api.apify.com/v2',
resource_path=f'acts/{actor_id}/runs/{actor_run_id}',
params=self._params(
status=maybe_extract_enum_member_value(status),
origin=maybe_extract_enum_member_value(origin),
),
)
)

def webhooks(self: TaskClientAsync) -> WebhookCollectionClientAsync:
"""Retrieve a client for webhooks associated with this task."""
return WebhookCollectionClientAsync(**self._sub_resource_init_options())

0 comments on commit 1aaa196

Please sign in to comment.