Skip to content
Merged
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
51 changes: 51 additions & 0 deletions src/apify_client/clients/resource_clients/key_value_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import warnings
from contextlib import asynccontextmanager, contextmanager
from http import HTTPStatus
from typing import TYPE_CHECKING, Any

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

return None

def record_exists(self, key: str) -> bool:
"""Check if given record is present in the key-value store.

https://docs.apify.com/api/v2/key-value-store-record-head

Args:
key: Key of the record to check.

Returns:
True if the record exists, False otherwise.
"""
try:
response = self.http_client.call(
url=self._url(f'records/{key}'),
method='HEAD',
params=self._params(),
)
except ApifyApiError as exc:
if exc.status_code == HTTPStatus.NOT_FOUND:
return False

raise

return response.status_code == HTTPStatus.OK

def get_record_as_bytes(self, key: str) -> dict | None:
"""Retrieve the given record from the key-value store, without parsing it.

Expand Down Expand Up @@ -376,6 +402,31 @@ async def get_record(self, key: str) -> dict | None:

return None

async def record_exists(self, key: str) -> bool:
"""Check if given record is present in the key-value store.

https://docs.apify.com/api/v2/key-value-store-record-head

Args:
key: Key of the record to check.

Returns:
True if the record exists, False otherwise.
"""
try:
response = await self.http_client.call(
url=self._url(f'records/{key}'),
method='HEAD',
params=self._params(),
)
except ApifyApiError as exc:
if exc.status_code == HTTPStatus.NOT_FOUND:
return False

raise

return response.status_code == HTTPStatus.OK

async def get_record_as_bytes(self, key: str) -> dict | None:
"""Retrieve the given record from the key-value store, without parsing it.

Expand Down