Skip to content

Commit e63f546

Browse files
committed
Use single and shared literals and rename the RQ client classes
1 parent 7712410 commit e63f546

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

src/apify/storage_clients/_apify/_request_queue_client_full.py renamed to src/apify/storage_clients/_apify/_request_queue_shared_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
logger = getLogger(__name__)
2626

2727

28-
class ApifyRequestQueueClientFull(ApifyRequestQueueClient):
28+
class ApifyRequestQueueSharedClient(ApifyRequestQueueClient):
2929
"""An Apify platform implementation of the request queue client.
3030
3131
This implementation supports multiple producers and multiple consumers scenario.

src/apify/storage_clients/_apify/_request_queue_client_simple.py renamed to src/apify/storage_clients/_apify/_request_queue_single_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
logger = getLogger(__name__)
2424

2525

26-
class ApifyRequestQueueClientSimple(ApifyRequestQueueClient):
26+
class ApifyRequestQueueSingleClient(ApifyRequestQueueClient):
2727
"""An Apify platform implementation of the request queue client with limited capability.
2828
2929
This client is designed to use as little resources as possible, but has to be used in constrained context.

src/apify/storage_clients/_apify/_storage_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Literal
44

55
from typing_extensions import override
66

77
from crawlee.storage_clients._base import StorageClient
88

99
from ._dataset_client import ApifyDatasetClient
1010
from ._key_value_store_client import ApifyKeyValueStoreClient
11-
from ._request_queue_client_full import ApifyRequestQueueClientFull
12-
from ._request_queue_client_simple import ApifyRequestQueueClientSimple
11+
from ._request_queue_shared_client import ApifyRequestQueueSharedClient
12+
from ._request_queue_single_client import ApifyRequestQueueSingleClient
1313
from ._utils import hash_api_base_url_and_token
1414
from apify._configuration import Configuration as ApifyConfiguration
1515
from apify._utils import docs_group
@@ -26,16 +26,16 @@
2626
class ApifyStorageClient(StorageClient):
2727
"""Apify storage client."""
2828

29-
def __init__(self, *, simple_request_queue: bool = True) -> None:
29+
def __init__(self, *, access: Literal['single', 'shared'] = 'single') -> None:
3030
"""Initialize the Apify storage client.
3131
3232
Args:
33-
simple_request_queue: If True, the `create_rq_client` will always return `ApifyRequestQueueClientSimple`,
34-
if false it will return `ApifyRequestQueueClientFull`. Simple client is suitable for single consumer
35-
scenarios and makes less API calls. Full client is suitable for multiple consumers scenarios at the
36-
cost of higher API usage
33+
access: If 'single', the `create_rq_client` will return `ApifyRequestQueueSingleClient`, if 'shared' it
34+
will return `ApifyRequestQueueSharedClient`.
35+
- 'single' is suitable for single consumer scenarios. It makes less API calls, is cheaper and faster.
36+
- 'shared' is suitable for multiple consumers scenarios at the cost of higher API usage.
3737
"""
38-
self._simple_request_queue = simple_request_queue
38+
self._access = access
3939

4040
# This class breaches Liskov Substitution Principle. It requires specialized Configuration compared to its parent.
4141
_lsp_violation_error_message_template = (
@@ -96,7 +96,7 @@ async def create_rq_client(
9696
configuration = configuration or ApifyConfiguration.get_global_configuration()
9797
if isinstance(configuration, ApifyConfiguration):
9898
client: type[ApifyRequestQueueClient] = (
99-
ApifyRequestQueueClientSimple if self._simple_request_queue else ApifyRequestQueueClientFull
99+
ApifyRequestQueueSingleClient if self._access == 'single' else ApifyRequestQueueSharedClient
100100
)
101101
return await client.open(id=id, name=name, alias=alias, configuration=configuration)
102102

tests/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ def apify_client_async(apify_token: str) -> ApifyClientAsync:
107107
return ApifyClientAsync(apify_token, api_url=api_url)
108108

109109

110-
@pytest.fixture(params=[False, True])
110+
@pytest.fixture(params=[['single', 'shared']])
111111
async def request_queue_apify(
112112
apify_token: str, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
113113
) -> AsyncGenerator[RequestQueue]:
114114
"""Create an instance of the Apify request queue on the platform and drop it when the test is finished."""
115115
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_token)
116116

117117
async with Actor:
118-
rq = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=request.param))
118+
rq = await RequestQueue.open(storage_client=ApifyStorageClient(access=request.param))
119119
yield rq
120120
await rq.drop()
121121

tests/integration/test_actor_request_queue.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .conftest import MakeActorFunction, RunActorFunction
2424

2525

26-
@pytest.fixture(params=[False, True])
26+
@pytest.fixture(params=['single', 'shared'])
2727
async def apify_named_rq(
2828
apify_client_async: ApifyClientAsync, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
2929
) -> AsyncGenerator[RequestQueue]:
@@ -33,7 +33,7 @@ async def apify_named_rq(
3333

3434
async with Actor:
3535
request_queue = await RequestQueue.open(
36-
name=request_queue_name, storage_client=ApifyStorageClient(simple_request_queue=request.param)
36+
name=request_queue_name, storage_client=ApifyStorageClient(access=request.param)
3737
)
3838
yield request_queue
3939
await request_queue.drop()

tests/integration/test_request_queue.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44
from datetime import datetime, timezone
5-
from typing import TYPE_CHECKING, cast
5+
from typing import TYPE_CHECKING, Literal, cast
66

77
import pytest
88

@@ -1086,22 +1086,22 @@ async def test_request_queue_simple_and_full_at_the_same_time(
10861086
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_token)
10871087

10881088
async with Actor:
1089-
rq_simple = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=True))
1090-
rq_full = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=False))
1089+
rq_simple = await RequestQueue.open(storage_client=ApifyStorageClient(access='single'))
1090+
rq_full = await RequestQueue.open(storage_client=ApifyStorageClient(access='shared'))
10911091
# Opening same queue again with different ApifyStorageClient will resolve to the first client used.
10921092
assert rq_simple is rq_full
10931093
await rq_simple.drop()
10941094

10951095

10961096
@pytest.mark.parametrize(
1097-
('simple_request_queue', 'expected_write_count_per_request'),
1098-
[pytest.param(True, 2, id='Simple rq client'), pytest.param(False, 3, id='Full rq client')],
1097+
('access', 'expected_write_count_per_request'),
1098+
[pytest.param('single', 2, id='Simple rq client'), pytest.param('shared', 3, id='Full rq client')],
10991099
)
11001100
async def test_crawler_run_request_queue_variant_stats(
11011101
*,
11021102
apify_token: str,
11031103
monkeypatch: pytest.MonkeyPatch,
1104-
simple_request_queue: bool,
1104+
access: Literal['single', 'shared'],
11051105
expected_write_count_per_request: int,
11061106
) -> None:
11071107
"""Check the main difference in the simple vs full request queue client - writeCount per request.
@@ -1112,7 +1112,7 @@ async def test_crawler_run_request_queue_variant_stats(
11121112
monkeypatch.setenv(ApifyEnvVars.TOKEN, apify_token)
11131113
async with Actor:
11141114
requests = 5
1115-
rq = await RequestQueue.open(storage_client=ApifyStorageClient(simple_request_queue=simple_request_queue))
1115+
rq = await RequestQueue.open(storage_client=ApifyStorageClient(access=access))
11161116
crawler = BasicCrawler(request_manager=rq)
11171117

11181118
@crawler.router.default_handler

0 commit comments

Comments
 (0)