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