|
30 | 30 | logger = getLogger(__name__)
|
31 | 31 |
|
32 | 32 |
|
| 33 | +def unique_key_to_request_id(unique_key: str, *, request_id_length: int = 15) -> str: |
| 34 | + """Generate a deterministic request ID based on a unique key. |
| 35 | +
|
| 36 | + Args: |
| 37 | + unique_key: The unique key to convert into a request ID. |
| 38 | + request_id_length: The length of the request ID. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + A URL-safe, truncated request ID based on the unique key. |
| 42 | + """ |
| 43 | + # Encode the unique key and compute its SHA-256 hash |
| 44 | + hashed_key = sha256(unique_key.encode('utf-8')).digest() |
| 45 | + |
| 46 | + # Encode the hash in base64 and decode it to get a string |
| 47 | + base64_encoded = b64encode(hashed_key).decode('utf-8') |
| 48 | + |
| 49 | + # Remove characters that are not URL-safe ('+', '/', or '=') |
| 50 | + url_safe_key = re.sub(r'(\+|\/|=)', '', base64_encoded) |
| 51 | + |
| 52 | + # Truncate the key to the desired length |
| 53 | + return url_safe_key[:request_id_length] |
| 54 | + |
| 55 | + |
33 | 56 | class ApifyRequestQueueClient(RequestQueueClient):
|
34 | 57 | """An Apify platform implementation of the request queue client."""
|
35 | 58 |
|
@@ -760,26 +783,3 @@ def _cache_request(
|
760 | 783 | hydrated=hydrated_request,
|
761 | 784 | lock_expires_at=None,
|
762 | 785 | )
|
763 |
| - |
764 |
| - |
765 |
| -def unique_key_to_request_id(unique_key: str, *, request_id_length: int = 15) -> str: |
766 |
| - """Generate a deterministic request ID based on a unique key. |
767 |
| -
|
768 |
| - Args: |
769 |
| - unique_key: The unique key to convert into a request ID. |
770 |
| - request_id_length: The length of the request ID. |
771 |
| -
|
772 |
| - Returns: |
773 |
| - A URL-safe, truncated request ID based on the unique key. |
774 |
| - """ |
775 |
| - # Encode the unique key and compute its SHA-256 hash |
776 |
| - hashed_key = sha256(unique_key.encode('utf-8')).digest() |
777 |
| - |
778 |
| - # Encode the hash in base64 and decode it to get a string |
779 |
| - base64_encoded = b64encode(hashed_key).decode('utf-8') |
780 |
| - |
781 |
| - # Remove characters that are not URL-safe ('+', '/', or '=') |
782 |
| - url_safe_key = re.sub(r'(\+|\/|=)', '', base64_encoded) |
783 |
| - |
784 |
| - # Truncate the key to the desired length |
785 |
| - return url_safe_key[:request_id_length] |
0 commit comments