1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import logging
15- from typing import Any , Dict , List , Optional , Set , Tuple , cast
15+ from typing import Any , Dict , List , Optional , Set , Tuple
1616
1717import requests
1818import requests_toolbelt
1919import rich .progress
20- import urllib3
21- from requests import adapters
22- from requests_toolbelt .utils import user_agent
2320from rich import print
2421
25- import twine
2622from twine import package as package_file
23+ from twine .utils import make_requests_session
2724
2825KEYWORDS_TO_NOT_FLATTEN = {"gpg_signature" , "attestations" , "content" }
2926
@@ -47,7 +44,7 @@ def __init__(
4744 ) -> None :
4845 self .url = repository_url
4946
50- self .session = requests . session ()
47+ self .session = make_requests_session ()
5148 # requests.Session.auth should be Union[None, Tuple[str, str], ...]
5249 # But username or password could be None
5350 # See TODO for utils.RepositoryConfig
@@ -57,35 +54,10 @@ def __init__(
5754 logger .info (f"username: { username if username else '<empty>' } " )
5855 logger .info (f"password: <{ 'hidden' if password else 'empty' } >" )
5956
60- self .session .headers ["User-Agent" ] = self ._make_user_agent_string ()
61- for scheme in ("http://" , "https://" ):
62- self .session .mount (scheme , self ._make_adapter_with_retries ())
63-
6457 # Working around https://github.com/python/typing/issues/182
6558 self ._releases_json_data : Dict [str , Dict [str , Any ]] = {}
6659 self .disable_progress_bar = disable_progress_bar
6760
68- @staticmethod
69- def _make_adapter_with_retries () -> adapters .HTTPAdapter :
70- retry = urllib3 .Retry (
71- allowed_methods = ["GET" ],
72- connect = 5 ,
73- total = 10 ,
74- status_forcelist = [500 , 501 , 502 , 503 ],
75- )
76-
77- return adapters .HTTPAdapter (max_retries = retry )
78-
79- @staticmethod
80- def _make_user_agent_string () -> str :
81- user_agent_string = (
82- user_agent .UserAgentBuilder ("twine" , twine .__version__ )
83- .include_implementation ()
84- .build ()
85- )
86-
87- return cast (str , user_agent_string )
88-
8961 def close (self ) -> None :
9062 self .session .close ()
9163
0 commit comments