Skip to content

Commit 3bacab7

Browse files
committed
fixing generate public url for KVS records
1 parent afcb8c7 commit 3bacab7

File tree

4 files changed

+51
-77
lines changed

4 files changed

+51
-77
lines changed

src/apify/storage_clients/_apify/_dataset_client.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
if TYPE_CHECKING:
1616
from collections.abc import AsyncIterator
17-
from datetime import datetime
1817

1918
from apify_client.clients import DatasetClientAsync
2019
from crawlee._types import JsonSerializable
@@ -38,31 +37,23 @@ class ApifyDatasetClient(DatasetClient):
3837
def __init__(
3938
self,
4039
*,
41-
id: str,
42-
name: str | None,
43-
created_at: datetime,
44-
accessed_at: datetime,
45-
modified_at: datetime,
46-
item_count: int,
40+
metadata: DatasetMetadata,
4741
api_client: DatasetClientAsync,
42+
api_public_base_url: str,
4843
lock: asyncio.Lock,
4944
) -> None:
5045
"""Initialize a new instance.
5146
5247
Preferably use the `ApifyDatasetClient.open` class method to create a new instance.
5348
"""
54-
self._metadata = DatasetMetadata(
55-
id=id,
56-
name=name,
57-
created_at=created_at,
58-
accessed_at=accessed_at,
59-
modified_at=modified_at,
60-
item_count=item_count,
61-
)
49+
self._metadata = metadata
6250

6351
self._api_client = api_client
6452
"""The Apify dataset client for API operations."""
6553

54+
self._api_public_base_url = api_public_base_url
55+
"""The public base URL for accessing the key-value store records."""
56+
6657
self._lock = lock
6758
"""A lock to ensure that only one operation is performed at a time."""
6859

@@ -109,6 +100,13 @@ async def open(
109100
if not api_url:
110101
raise ValueError(f'Apify storage client requires a valid API URL in Configuration (api_url={api_url}).')
111102

103+
api_public_base_url = getattr(configuration, 'api_public_base_url', None)
104+
if not api_public_base_url:
105+
raise ValueError(
106+
'Apify storage client requires a valid API public base URL in Configuration '
107+
f'(api_public_base_url={api_public_base_url}).'
108+
)
109+
112110
if id and name:
113111
raise ValueError('Only one of "id" or "name" can be specified, not both.')
114112

@@ -144,13 +142,9 @@ async def open(
144142
metadata = DatasetMetadata.model_validate(await apify_dataset_client.get())
145143

146144
return cls(
147-
id=metadata.id,
148-
name=metadata.name,
149-
created_at=metadata.created_at,
150-
accessed_at=metadata.accessed_at,
151-
modified_at=metadata.modified_at,
152-
item_count=metadata.item_count,
145+
metadata=metadata,
153146
api_client=apify_dataset_client,
147+
api_public_base_url=api_public_base_url,
154148
lock=asyncio.Lock(),
155149
)
156150

src/apify/storage_clients/_apify/_key_value_store_client.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
if TYPE_CHECKING:
1818
from collections.abc import AsyncIterator
19-
from datetime import datetime
2019

2120
from apify_client.clients import KeyValueStoreClientAsync
2221
from crawlee.configuration import Configuration
@@ -30,28 +29,22 @@ class ApifyKeyValueStoreClient(KeyValueStoreClient):
3029
def __init__(
3130
self,
3231
*,
33-
id: str,
34-
name: str | None,
35-
created_at: datetime,
36-
accessed_at: datetime,
37-
modified_at: datetime,
32+
metadata: KeyValueStoreMetadata,
3833
api_client: KeyValueStoreClientAsync,
34+
api_public_base_url: str,
3935
lock: asyncio.Lock,
4036
) -> None:
4137
"""Initialize a new instance.
4238
4339
Preferably use the `ApifyKeyValueStoreClient.open` class method to create a new instance.
4440
"""
45-
self._metadata = KeyValueStoreMetadata(
46-
id=id,
47-
name=name,
48-
created_at=created_at,
49-
accessed_at=accessed_at,
50-
modified_at=modified_at,
51-
)
41+
self._metadata = metadata
5242

5343
self._api_client = api_client
54-
"""The Apify key-value store client for API operations."""
44+
"""The Apify KVS client for API operations."""
45+
46+
self._api_public_base_url = api_public_base_url
47+
"""The public base URL for accessing the key-value store records."""
5548

5649
self._lock = lock
5750
"""A lock to ensure that only one operation is performed at a time."""
@@ -99,6 +92,13 @@ async def open(
9992
if not api_url:
10093
raise ValueError(f'Apify storage client requires a valid API URL in Configuration (api_url={api_url}).')
10194

95+
api_public_base_url = getattr(configuration, 'api_public_base_url', None)
96+
if not api_public_base_url:
97+
raise ValueError(
98+
'Apify storage client requires a valid API public base URL in Configuration '
99+
f'(api_public_base_url={api_public_base_url}).'
100+
)
101+
102102
if id and name:
103103
raise ValueError('Only one of "id" or "name" can be specified, not both.')
104104

@@ -134,12 +134,9 @@ async def open(
134134
metadata = KeyValueStoreMetadata.model_validate(await apify_kvs_client.get())
135135

136136
return cls(
137-
id=metadata.id,
138-
name=metadata.name,
139-
created_at=metadata.created_at,
140-
accessed_at=metadata.accessed_at,
141-
modified_at=metadata.modified_at,
137+
metadata=metadata,
142138
api_client=apify_kvs_client,
139+
api_public_base_url=api_public_base_url,
143140
lock=asyncio.Lock(),
144141
)
145142

@@ -230,7 +227,7 @@ async def get_public_url(self, key: str) -> str:
230227
raise ValueError('resource_id cannot be None when generating a public URL')
231228

232229
public_url = (
233-
URL(self._api_client.base_url) / 'v2' / 'key-value-stores' / self._api_client.resource_id / 'records' / key
230+
URL(self._api_public_base_url) / 'v2' / 'key-value-stores' / self._api_client.resource_id / 'records' / key
234231
)
235232

236233
if self.metadata.model_extra is not None:

src/apify/storage_clients/_apify/_request_queue_client.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,23 @@ class ApifyRequestQueueClient(RequestQueueClient):
3838
def __init__(
3939
self,
4040
*,
41-
id: str,
42-
name: str | None,
43-
created_at: datetime,
44-
accessed_at: datetime,
45-
modified_at: datetime,
46-
had_multiple_clients: bool,
47-
handled_request_count: int,
48-
pending_request_count: int,
49-
stats: dict,
50-
total_request_count: int,
41+
metadata: RequestQueueMetadata,
5142
api_client: RequestQueueClientAsync,
43+
api_public_base_url: str,
5244
lock: asyncio.Lock,
5345
) -> None:
5446
"""Initialize a new instance.
5547
5648
Preferably use the `ApifyRequestQueueClient.open` class method to create a new instance.
5749
"""
58-
self._metadata = RequestQueueMetadata(
59-
id=id,
60-
name=name,
61-
created_at=created_at,
62-
accessed_at=accessed_at,
63-
modified_at=modified_at,
64-
had_multiple_clients=had_multiple_clients,
65-
handled_request_count=handled_request_count,
66-
pending_request_count=pending_request_count,
67-
stats=stats,
68-
total_request_count=total_request_count,
69-
)
50+
self._metadata = metadata
7051

7152
self._api_client = api_client
7253
"""The Apify request queue client for API operations."""
7354

55+
self._api_public_base_url = api_public_base_url
56+
"""The public base URL for accessing the key-value store records."""
57+
7458
self._lock = lock
7559
"""A lock to ensure that only one operation is performed at a time."""
7660

@@ -130,6 +114,13 @@ async def open(
130114
if not api_url:
131115
raise ValueError(f'Apify storage client requires a valid API URL in Configuration (api_url={api_url}).')
132116

117+
api_public_base_url = getattr(configuration, 'api_public_base_url', None)
118+
if not api_public_base_url:
119+
raise ValueError(
120+
'Apify storage client requires a valid API public base URL in Configuration '
121+
f'(api_public_base_url={api_public_base_url}).'
122+
)
123+
133124
if id and name:
134125
raise ValueError('Only one of "id" or "name" can be specified, not both.')
135126

@@ -165,17 +156,9 @@ async def open(
165156
metadata = RequestQueueMetadata.model_validate(await apify_rq_client.get())
166157

167158
return cls(
168-
id=metadata.id,
169-
name=metadata.name,
170-
created_at=metadata.created_at,
171-
accessed_at=metadata.accessed_at,
172-
modified_at=metadata.modified_at,
173-
had_multiple_clients=metadata.had_multiple_clients,
174-
handled_request_count=metadata.handled_request_count,
175-
pending_request_count=metadata.pending_request_count,
176-
stats=metadata.stats,
177-
total_request_count=metadata.total_request_count,
159+
metadata=metadata,
178160
api_client=apify_rq_client,
161+
api_public_base_url=api_public_base_url,
179162
lock=asyncio.Lock(),
180163
)
181164

tests/integration/test_actor_key_value_store.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ async def main() -> None:
217217
await kvs.set_value(record_key, {'exposedData': 'test'}, 'application/json')
218218

219219
record_url = await kvs.get_public_url(record_key)
220-
221220
signature = create_hmac_signature(url_signing_secret_key, record_key)
222-
assert (
223-
record_url
224-
== f'{public_api_url}/v2/key-value-stores/{default_kvs_id}/records/{record_key}?signature={signature}'
221+
expected_record_url = (
222+
f'{public_api_url}/v2/key-value-stores/{default_kvs_id}/records/{record_key}?signature={signature}'
225223
)
226224

225+
assert record_url == expected_record_url
226+
227227
actor = await make_actor(label='kvs-get-public-url', main_func=main)
228228
run_result = await run_actor(actor)
229229

0 commit comments

Comments
 (0)