1515import requests
1616
1717from . import utils
18+ from .tl_cache import CustomTelegramClient
19+ from .version import __version__
1820
1921logger = logging .getLogger (__name__ )
2022
@@ -45,7 +47,12 @@ def _get_path(self, repo: str, module_name: str) -> str:
4547 )
4648
4749 def save (self , repo : str , module_name : str , module_code : str ):
48- """Saves module to disk."""
50+ """
51+ Saves module to disk.
52+ :param repo: Repository name.
53+ :param module_name: Module name.
54+ :param module_code: Module source code.
55+ """
4956 size = len (module_code )
5057 if size > MAX_FILESIZE :
5158 logger .warning (
@@ -70,7 +77,12 @@ def save(self, repo: str, module_name: str, module_code: str):
7077 logger .debug ("Saved module %s from %s to local cache." , module_name , repo )
7178
7279 def fetch (self , repo : str , module_name : str ) -> typing .Optional [str ]:
73- """Fetches module from disk."""
80+ """
81+ Fetches module from disk.
82+ :param repo: Repository name.
83+ :param module_name: Module name.
84+ :return: Module source code or None.
85+ """
7486 path = self ._get_path (repo , module_name )
7587 if os .path .isfile (path ):
7688 with open (path , "r" ) as f :
@@ -80,8 +92,9 @@ def fetch(self, repo: str, module_name: str) -> typing.Optional[str]:
8092
8193
8294class RemoteStorage :
83- def __init__ (self ):
95+ def __init__ (self , client : CustomTelegramClient ):
8496 self ._local_storage = LocalStorage ()
97+ self ._client = client
8598
8699 async def preload (self , urls : typing .List [str ]):
87100 """Preloads modules from remote storage."""
@@ -122,7 +135,11 @@ async def preload_main_repo(self):
122135
123136 @staticmethod
124137 def _parse_url (url : str ) -> typing .Tuple [str , str , str ]:
125- """Parses a URL into a repository and module name."""
138+ """
139+ Parses a URL into a repository and module name.
140+ :param url: URL to parse.
141+ :return: Tuple of (url, repo, module_name).
142+ """
126143 domain_name = url .split ("/" )[2 ]
127144
128145 if domain_name == "raw.githubusercontent.com" :
@@ -142,14 +159,22 @@ def _parse_url(url: str) -> typing.Tuple[str, str, str]:
142159 async def fetch (self , url : str , auth : typing .Optional [str ] = None ) -> str :
143160 """
144161 Fetches the module from the remote storage.
145- :param ref: The module reference. Can be url, or a reference to official repo module.
162+ :param url: URL to the module.
163+ :param auth: Optional authentication string in the format "username:password".
164+ :return: Module source code.
146165 """
147166 url , repo , module_name = self ._parse_url (url )
148167 try :
149168 r = await utils .run_sync (
150169 requests .get ,
151170 url ,
152171 auth = (tuple (auth .split (":" , 1 )) if auth else None ),
172+ headers = {
173+ "User-Agent" : "Hikka Userbot" ,
174+ "X-Hikka-Version" : "." .join (map (str , __version__ )),
175+ "X-Hikka-Commit-SHA" : utils .get_git_hash (),
176+ "X-Hikka-User" : str (self ._client .tg_id ),
177+ },
153178 )
154179 r .raise_for_status ()
155180 except Exception :
0 commit comments