Skip to content

Commit 1fc2ad8

Browse files
committed
Move unique_key_to_request_id definition to the top
1 parent bedb6dd commit 1fc2ad8

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/apify/storage_clients/_apify/_request_queue_client.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@
2929
logger = getLogger(__name__)
3030

3131

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+
3255
class ApifyRequestQueueClient(RequestQueueClient):
3356
"""An Apify platform implementation of the request queue client."""
3457

@@ -762,26 +785,3 @@ def _cache_request(
762785
hydrated=hydrated_request,
763786
lock_expires_at=None,
764787
)
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

Comments
 (0)