Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep sessions around between each ConanRequester #17455

Open
wants to merge 10 commits into
base: develop2
Choose a base branch
from
3 changes: 2 additions & 1 deletion conan/test/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ def chdir(self, newdir):
def mocked_servers(self, requester=None):
_req = requester or TestRequester(self.servers)
with mock.patch("conans.client.rest.conan_requester.requests", _req):
yield
with mock.patch("conans.client.rest.conan_requester.ConanRequester._http_requester_imp", _req):
yield

@contextmanager
def mocked_io(self):
Expand Down
34 changes: 20 additions & 14 deletions conans/client/rest/conan_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,14 @@ def add_auth(self, url, kwargs):


class ConanRequester:
_http_requester_imp = None
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
_adapter = None

def __init__(self, config, cache_folder=None):
# TODO: Make all this lazy, to avoid fully configuring Requester, for every api call
# even if it doesn't use it
# FIXME: Trick for testing when requests is mocked
if hasattr(requests, "Session"):
self._http_requester = requests.Session()
adapter = HTTPAdapter(max_retries=self._get_retries(config))
self._http_requester.mount("http://", adapter)
self._http_requester.mount("https://", adapter)
else:
self._http_requester = requests

self._url_creds = _SourceURLCredentials(cache_folder)
self._max_retries = config.get("core.net.http:max_retries", default=2, check_type=int)
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _max_retries is now legacy?

if ConanRequester._http_requester_imp is not None and hasattr(requests, "Session"):
ConanRequester._adapter.max_retries = self._get_retries()
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
self._timeout = config.get("core.net.http:timeout", default=DEFAULT_TIMEOUT)
self._no_proxy_match = config.get("core.net.http:no_proxy_match", check_type=list)
self._proxies = config.get("core.net.http:proxies")
Expand All @@ -123,9 +117,21 @@ def __init__(self, config, cache_folder=None):
platform.machine()])
self._user_agent = "Conan/%s (%s)" % (__version__, platform_info)

@staticmethod
def _get_retries(config):
retry = config.get("core.net.http:max_retries", default=2, check_type=int)
@property
def _http_requester(self):
if ConanRequester._http_requester_imp is None:
# FIXME: Trick for testing when requests is mocked
if hasattr(requests, "Session"):
ConanRequester._http_requester_imp = requests.Session()
ConanRequester._adapter = HTTPAdapter(max_retries=self._get_retries())
ConanRequester._http_requester_imp.mount("http://", ConanRequester._adapter)
ConanRequester._http_requester_imp.mount("https://", ConanRequester._adapter)
else:
ConanRequester._http_requester_imp = requests
return ConanRequester._http_requester_imp

def _get_retries(self):
retry = self._max_retries
if retry == 0:
return 0
retry_status_code_set = {
Expand Down
17 changes: 9 additions & 8 deletions test/integration/configuration/requester_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class ConanRequesterHeadersTests(unittest.TestCase):
def test_user_agent(self):
mock_http_requester = MagicMock()
with mock.patch("conans.client.rest.conan_requester.requests", mock_http_requester):
requester = ConanRequester(ConfDefinition())
requester.get(url="aaa")
headers = requester._http_requester.get.call_args[1]["headers"]
self.assertIn("Conan/%s" % __version__, headers["User-Agent"])

requester.get(url="aaa", headers={"User-Agent": "MyUserAgent"})
headers = requester._http_requester.get.call_args[1]["headers"]
self.assertEqual("MyUserAgent", headers["User-Agent"])
with mock.patch("conans.client.rest.conan_requester.ConanRequester._http_requester_imp", mock_http_requester):
requester = ConanRequester(ConfDefinition())
requester.get(url="aaa")
headers = requester._http_requester.get.call_args[1]["headers"]
self.assertIn("Conan/%s" % __version__, headers["User-Agent"])

requester.get(url="aaa", headers={"User-Agent": "MyUserAgent"})
headers = requester._http_requester.get.call_args[1]["headers"]
self.assertEqual("MyUserAgent", headers["User-Agent"])
4 changes: 3 additions & 1 deletion test/integration/remote/rest_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from conan.test.utils.mocks import LocalDBMock
from conan.test.utils.server_launcher import TestServerLauncher
from conan.test.utils.test_files import temp_folder
from conan.test.utils.tools import get_free_port
from conan.test.utils.tools import get_free_port, TestRequester
from conans.util.files import md5, save


Expand All @@ -43,6 +43,7 @@ def setUpClass(cls):
save(filename, "")
config = ConfDefinition()
requester = ConanRequester(config)
ConanRequester._http_requester_imp = TestRequester({"myremote", cls.server},)
localdb = LocalDBMock()

mocked_user_input = UserInput(non_interactive=False)
Expand All @@ -61,6 +62,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
ConanRequester._http_requester_imp = None
cls.server.stop()

def tearDown(self):
Expand Down
Loading