diff --git a/substrate/_client.py b/substrate/_client.py index e4a33cc..3b885b7 100644 --- a/substrate/_client.py +++ b/substrate/_client.py @@ -2,6 +2,7 @@ import platform from typing import Any, Dict, Union, Optional from typing_extensions import Literal +from .core.id_generator import IDGenerator import httpx import distro @@ -169,6 +170,7 @@ def default_headers(self) -> Dict[str, str]: "Content-Type": "application/json", "User-Agent": self.user_agent, "X-Substrate-Backend": self._backend, + "X-Substrate-Request-Id": IDGenerator.random_string(32), **self.platform_headers, **self.auth_headers, **self._additional_headers, diff --git a/substrate/core/id_generator.py b/substrate/core/id_generator.py index f3ea033..0e4ff33 100644 --- a/substrate/core/id_generator.py +++ b/substrate/core/id_generator.py @@ -10,18 +10,24 @@ class IDGenerator: _generators: Dict[str, "IDGenerator"] = {} _lock = Lock() - def __init__(self, prefix: str): + def __init__(self, prefix: str, length: int = 8): self.prefix = prefix self.n = 1 + self.length = length def get_next_id(self): with IDGenerator._lock: - alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" - random_string = "".join(random.choices(alphabet, k=8)) + random_string = IDGenerator.random_string(self.length) next_id = f"{self.prefix}{self.n}_{random_string}" self.n += 1 return next_id + @staticmethod + def random_string(length: int) -> str: + alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" + random_string = "".join(random.choices(alphabet, k=length)) + return random_string + @staticmethod def get_instance(class_name: str) -> "IDGenerator": with IDGenerator._lock: