Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/apify_client/_resource_clients/_resource_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def __init__(
if resource_path.endswith('/'):
raise ValueError('resource_path must not end with "/"')

if resource_id == '':
raise ValueError(f'resource_id of the `{resource_path}` client must not be an empty string')

self._base_url = base_url
self._api_base_url = api_base_url or base_url
self._public_base_url = public_base_url
Expand Down
4 changes: 2 additions & 2 deletions src/apify_client/_resource_clients/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
**kwargs: Any,
) -> None:
super().__init__(
resource_id=resource_id or 'me',
resource_id='me' if resource_id is None else resource_id,
resource_path=resource_path,
**kwargs,
)
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(
**kwargs: Any,
) -> None:
super().__init__(
resource_id=resource_id or 'me',
resource_id='me' if resource_id is None else resource_id,
resource_path=resource_path,
**kwargs,
)
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_empty_resource_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from apify_client import ApifyClient, ApifyClientAsync

if TYPE_CHECKING:
from collections.abc import Callable

_GETTERS = [
pytest.param(lambda client: client.actor(''), id='actor'),
pytest.param(lambda client: client.dataset(''), id='dataset'),
pytest.param(lambda client: client.log(''), id='log'),
pytest.param(lambda client: client.request_queue(''), id='request queue'),
pytest.param(lambda client: client.user(''), id='user'),
pytest.param(lambda client: client.actor('actor-id').version(''), id='actor version'),
pytest.param(lambda client: client.actor('actor-id').version('0.1').env_var(''), id='env var'),
]


@pytest.mark.parametrize('get_client', _GETTERS)
@pytest.mark.parametrize('client_class', [ApifyClient, ApifyClientAsync])
def test_empty_resource_id_is_rejected_by_the_getter(
client_class: type[ApifyClient | ApifyClientAsync],
get_client: Callable[[ApifyClient | ApifyClientAsync], object],
) -> None:
"""A getter refuses an empty ID as soon as it is called."""
client = client_class(token='test_token')

with pytest.raises(ValueError, match='resource_id'):
get_client(client)


@pytest.mark.parametrize('client_class', [ApifyClient, ApifyClientAsync])
@pytest.mark.parametrize('user_id', [pytest.param((), id='omitted'), pytest.param((None,), id='none')])
def test_user_without_an_id_is_the_current_user(
client_class: type[ApifyClient | ApifyClientAsync], user_id: tuple[None, ...]
) -> None:
"""Only an omitted or `None` user ID stands for the current user."""
client = client_class(token='test_token')

assert client.user(*user_id).resource_id == 'me'
4 changes: 2 additions & 2 deletions tests/unit/test_url_path_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,14 @@ def test_resource_id_is_encoded_into_a_single_path_segment(
assert captured_targets == [f'/v2/datasets/{encoded_resource_id}']


@pytest.mark.parametrize('resource_id', _DEGENERATE_SEGMENTS)
@pytest.mark.parametrize('resource_id', [pytest.param('.', id='current'), pytest.param('..', id='parent')])
def test_degenerate_resource_id_is_rejected_before_the_request(
*,
api_url: str,
captured_targets: list[str],
resource_id: str,
) -> None:
"""An empty or dot resource ID would address the whole collection, so it is refused instead."""
"""A dot resource ID would address another endpoint, so it is refused instead."""
client = ApifyClient(token='test_token', api_url=api_url)

with pytest.raises(ValueError, match='cannot be used as a URL path segment'):
Expand Down