|
2 | 2 |
|
3 | 3 | import warnings
|
4 | 4 | from contextlib import asynccontextmanager, contextmanager
|
| 5 | +from http import HTTPStatus |
5 | 6 | from typing import TYPE_CHECKING, Any
|
6 | 7 |
|
7 | 8 | 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)
|
154 | 155 |
|
155 | 156 | return None
|
156 | 157 |
|
| 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 | + |
157 | 183 | def get_record_as_bytes(self, key: str) -> dict | None:
|
158 | 184 | """Retrieve the given record from the key-value store, without parsing it.
|
159 | 185 |
|
@@ -376,6 +402,31 @@ async def get_record(self, key: str) -> dict | None:
|
376 | 402 |
|
377 | 403 | return None
|
378 | 404 |
|
| 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 | + |
379 | 430 | async def get_record_as_bytes(self, key: str) -> dict | None:
|
380 | 431 | """Retrieve the given record from the key-value store, without parsing it.
|
381 | 432 |
|
|
0 commit comments