Skip to content

Commit 519529b

Browse files
authored
feat: Add KeyValueStoreClient.record_exists (#427)
related to apify/apify-docs#1649
1 parent 5f56bf4 commit 519529b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/apify_client/clients/resource_clients/key_value_store.py

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

33
import warnings
44
from contextlib import asynccontextmanager, contextmanager
5+
from http import HTTPStatus
56
from typing import TYPE_CHECKING, Any
67

78
from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, parse_date_fields
@@ -154,6 +155,31 @@ def get_record(self, key: str, *, as_bytes: bool = False, as_file: bool = False)
154155

155156
return None
156157

158+
def record_exists(self, key: str) -> bool:
159+
"""Check if given record is present in the key-value store.
160+
161+
https://docs.apify.com/api/v2/key-value-store-record-head
162+
163+
Args:
164+
key: Key of the record to check.
165+
166+
Returns:
167+
True if the record exists, False otherwise.
168+
"""
169+
try:
170+
response = self.http_client.call(
171+
url=self._url(f'records/{key}'),
172+
method='HEAD',
173+
params=self._params(),
174+
)
175+
except ApifyApiError as exc:
176+
if exc.status_code == HTTPStatus.NOT_FOUND:
177+
return False
178+
179+
raise
180+
181+
return response.status_code == HTTPStatus.OK
182+
157183
def get_record_as_bytes(self, key: str) -> dict | None:
158184
"""Retrieve the given record from the key-value store, without parsing it.
159185
@@ -376,6 +402,31 @@ async def get_record(self, key: str) -> dict | None:
376402

377403
return None
378404

405+
async def record_exists(self, key: str) -> bool:
406+
"""Check if given record is present in the key-value store.
407+
408+
https://docs.apify.com/api/v2/key-value-store-record-head
409+
410+
Args:
411+
key: Key of the record to check.
412+
413+
Returns:
414+
True if the record exists, False otherwise.
415+
"""
416+
try:
417+
response = await self.http_client.call(
418+
url=self._url(f'records/{key}'),
419+
method='HEAD',
420+
params=self._params(),
421+
)
422+
except ApifyApiError as exc:
423+
if exc.status_code == HTTPStatus.NOT_FOUND:
424+
return False
425+
426+
raise
427+
428+
return response.status_code == HTTPStatus.OK
429+
379430
async def get_record_as_bytes(self, key: str) -> dict | None:
380431
"""Retrieve the given record from the key-value store, without parsing it.
381432

0 commit comments

Comments
 (0)