44from interface_wrapper import IRepositoryAPI , RepositoryFactory
55
66
7- def login (source , token , base_url ):
8- client = RepositoryFactory .create_api (source , token , base_url )
9- return client
7+ def login (token , base_url ):
8+ try :
9+ client = RepositoryFactory .create_api (token , base_url )
10+ client .get_user_data ("test" )
11+ return client
12+ except Exception :
13+ return None
1014
1115
1216def get_tokens_from_file (tokens_path : str ) -> list [str ]:
@@ -24,61 +28,38 @@ def get_repos_from_file(repos_path: str) -> list[str]:
2428
2529
2630class Clients :
27- def __init__ (self , source : str , tokens : list [str ], base_url : str | None = None ):
28- # Возможно это можно переписать покрасивее
29- if source == 'github' :
30- self .clients = self ._init_clients (source , tokens , base_url )
31- elif base_url == 'forgejo' :
32- self .client = RepositoryFactory .create_api (source , tokens [0 ], base_url )
33- self .token = tokens [0 ]
34- else :
35- print (f"Unavailable source { source } , use [ 'github' | 'forgejo' ] instead" )
36-
37- self .source = source
31+ def __init__ (self , tokens : list [str ], base_url : str | None = None ):
32+ self .clients = []
33+ self .token_map = {}
3834
39- def _init_clients (
40- self , source : str , tokens : list [str ], base_url : str | None
41- ) -> list [dict ]:
42- clients = [
43- {
44- "client" : RepositoryFactory .create_api (source , token , base_url ),
45- "token" : token ,
46- }
47- for token in tokens
48- ]
35+ for token in tokens :
36+ client = login (token , base_url )
37+ if client :
38+ self .clients .append (client )
39+ self .token_map [client ] = token
4940
50- return clients
41+ if not self .clients :
42+ raise Exception ("No valid tokens for either GitHub or Forgejo" )
5143
52- def _get_next_git_client (self ) -> tuple [IRepositoryAPI , str ]:
44+ def _get_next_client (self ) -> tuple [IRepositoryAPI , str ]:
5345 client = None
5446 max_remaining_limit = - 1
5547
56- for client_tmp in self .clients :
57- remaining_limit , limit = client_tmp ["client" ].get_rate_limiting ()
58-
59- # можно добавить вывод износа токена
60- # можно дополнительно проверять на 403 и временно пропускать эти токены,
61- # либо завести константу "минимальный коэффициент износа" и не трогать "изношенные" токены
62-
63- if remaining_limit > max_remaining_limit :
64- client = client_tmp
65- max_remaining_limit = remaining_limit
66-
48+ for c in self .clients :
49+ remaining , _ = c .get_rate_limiting ()
50+ if remaining > max_remaining_limit :
51+ client = c
52+ max_remaining_limit = remaining
6753 sleep (TIMEDELTA )
6854
6955 if client is None :
7056 raise Exception ("No git clients available" )
7157
72- return client ['client' ], client ['token' ]
73-
74- def _get_next_forgejo_client (self ) -> tuple [IRepositoryAPI , str ]:
75- return self .client , self .token
58+ return client , self .token_map [client ]
7659
7760 def get_next_client (self ) -> tuple [IRepositoryAPI , str ]:
78- if self .source == 'github' :
79- return self ._get_next_git_client ()
80- elif self .source == 'forgejo' :
81- return self ._get_next_forgejo_client
61+ return self ._get_next_client ()
62+
8263
8364
8465def get_next_binded_repo (clients : Clients , repositories : list [str ]):
0 commit comments