|
9 | 9 | from ._dataset_client import ApifyDatasetClient
|
10 | 10 | from ._key_value_store_client import ApifyKeyValueStoreClient
|
11 | 11 | from ._request_queue_client import ApifyRequestQueueClient
|
| 12 | +from apify._configuration import Configuration as ApifyConfiguration |
12 | 13 | from apify._utils import docs_group
|
13 | 14 |
|
14 | 15 | if TYPE_CHECKING:
|
15 |
| - from crawlee.configuration import Configuration |
| 16 | + from collections.abc import Hashable |
| 17 | + |
| 18 | + from crawlee.configuration import Configuration as CrawleeConfiguration |
16 | 19 |
|
17 | 20 |
|
18 | 21 | @docs_group('Storage clients')
|
19 | 22 | class ApifyStorageClient(StorageClient):
|
20 | 23 | """Apify storage client."""
|
21 | 24 |
|
| 25 | + # This class breaches Liskov Substitution Principle. It requires specialized Configuration compared to its parent. |
| 26 | + _lsp_violation_error_message_template = ( |
| 27 | + 'Expected "configuration" to be an instance of "apify.Configuration", but got {} instead.' |
| 28 | + ) |
| 29 | + |
| 30 | + @override |
| 31 | + def get_additional_cache_key(self, configuration: CrawleeConfiguration) -> Hashable: |
| 32 | + if isinstance(configuration, ApifyConfiguration): |
| 33 | + return f'{configuration.api_base_url},{configuration.token}' |
| 34 | + raise TypeError(self._lsp_violation_error_message_template.format(type(configuration).__name__)) |
| 35 | + |
22 | 36 | @override
|
23 | 37 | async def create_dataset_client(
|
24 | 38 | self,
|
25 | 39 | *,
|
26 | 40 | id: str | None = None,
|
27 | 41 | name: str | None = None,
|
28 |
| - configuration: Configuration | None = None, |
| 42 | + alias: str | None = None, |
| 43 | + configuration: CrawleeConfiguration | None = None, |
29 | 44 | ) -> ApifyDatasetClient:
|
30 |
| - # Import here to avoid circular imports. |
31 |
| - from apify import Configuration as ApifyConfiguration # noqa: PLC0415 |
32 |
| - |
33 | 45 | configuration = configuration or ApifyConfiguration.get_global_configuration()
|
34 | 46 | if isinstance(configuration, ApifyConfiguration):
|
35 | 47 | return await ApifyDatasetClient.open(id=id, name=name, configuration=configuration)
|
36 | 48 |
|
37 |
| - raise TypeError( |
38 |
| - f'Expected "configuration" to be an instance of "apify.Configuration", ' |
39 |
| - f'but got {type(configuration).__name__} instead.' |
40 |
| - ) |
| 49 | + raise TypeError(self._lsp_violation_error_message_template.format(type(configuration).__name__)) |
41 | 50 |
|
42 | 51 | @override
|
43 | 52 | async def create_kvs_client(
|
44 | 53 | self,
|
45 | 54 | *,
|
46 | 55 | id: str | None = None,
|
47 | 56 | name: str | None = None,
|
48 |
| - configuration: Configuration | None = None, |
| 57 | + alias: str | None = None, |
| 58 | + configuration: CrawleeConfiguration | None = None, |
49 | 59 | ) -> ApifyKeyValueStoreClient:
|
50 |
| - # Import here to avoid circular imports. |
51 |
| - from apify import Configuration as ApifyConfiguration # noqa: PLC0415 |
52 |
| - |
53 | 60 | configuration = configuration or ApifyConfiguration.get_global_configuration()
|
54 | 61 | if isinstance(configuration, ApifyConfiguration):
|
55 | 62 | return await ApifyKeyValueStoreClient.open(id=id, name=name, configuration=configuration)
|
56 | 63 |
|
57 |
| - raise TypeError( |
58 |
| - f'Expected "configuration" to be an instance of "apify.Configuration", ' |
59 |
| - f'but got {type(configuration).__name__} instead.' |
60 |
| - ) |
| 64 | + raise TypeError(self._lsp_violation_error_message_template.format(type(configuration).__name__)) |
61 | 65 |
|
62 | 66 | @override
|
63 | 67 | async def create_rq_client(
|
64 | 68 | self,
|
65 | 69 | *,
|
66 | 70 | id: str | None = None,
|
67 | 71 | name: str | None = None,
|
68 |
| - configuration: Configuration | None = None, |
| 72 | + alias: str | None = None, |
| 73 | + configuration: CrawleeConfiguration | None = None, |
69 | 74 | ) -> ApifyRequestQueueClient:
|
70 |
| - # Import here to avoid circular imports. |
71 |
| - from apify import Configuration as ApifyConfiguration # noqa: PLC0415 |
72 |
| - |
73 | 75 | configuration = configuration or ApifyConfiguration.get_global_configuration()
|
74 | 76 | if isinstance(configuration, ApifyConfiguration):
|
75 | 77 | return await ApifyRequestQueueClient.open(id=id, name=name, configuration=configuration)
|
76 | 78 |
|
77 |
| - raise TypeError( |
78 |
| - f'Expected "configuration" to be an instance of "apify.Configuration", ' |
79 |
| - f'but got {type(configuration).__name__} instead.' |
80 |
| - ) |
| 79 | + raise TypeError(self._lsp_violation_error_message_template.format(type(configuration).__name__)) |
0 commit comments