diff --git a/src/hatch/index/core.py b/src/hatch/index/core.py index 1fa70713a..c5a231bae 100644 --- a/src/hatch/index/core.py +++ b/src/hatch/index/core.py @@ -1,11 +1,13 @@ from __future__ import annotations +from functools import cached_property from typing import TYPE_CHECKING -import httpx import hyperlink if TYPE_CHECKING: + import httpx + from hatch.utils.fs import Path @@ -33,17 +35,26 @@ def __init__(self, repo: str, *, user='', auth='', ca_cert=None, client_cert=Non self.user = user self.auth = auth - cert = None + self.__cert = None if client_cert: - cert = client_cert + self.__cert = client_cert if client_key: - cert = (client_cert, client_key) + self.__cert = (client_cert, client_key) - verify = True + self.__verify = True if ca_cert: - verify = ca_cert + self.__verify = ca_cert + + @cached_property + def client(self) -> httpx.Client: + import httpx - self.client = httpx.Client(timeout=10, transport=httpx.HTTPTransport(retries=3, verify=verify, cert=cert)) + from hatch.utils.network import DEFAULT_TIMEOUT + + return httpx.Client( + transport=httpx.HTTPTransport(retries=3, verify=self.__verify, cert=self.__cert), + timeout=DEFAULT_TIMEOUT, + ) def upload_artifact(self, artifact: Path, data: dict): import hashlib diff --git a/src/hatch/template/default.py b/src/hatch/template/default.py index 7463a2d80..71acb3e97 100644 --- a/src/hatch/template/default.py +++ b/src/hatch/template/default.py @@ -43,7 +43,7 @@ def initialize_config(self, config): url = f'https://raw.githubusercontent.com/spdx/license-list-data/v{VERSION}/text/{license_file_name}' for _ in range(5): try: - download_file(cached_license_path, url, timeout=2) + download_file(cached_license_path, url) except Exception: # noqa: BLE001, S112 continue else: diff --git a/src/hatch/utils/network.py b/src/hatch/utils/network.py index 1bbb52ff4..b4131e13a 100644 --- a/src/hatch/utils/network.py +++ b/src/hatch/utils/network.py @@ -2,12 +2,11 @@ import time from contextlib import contextmanager -from secrets import choice from typing import TYPE_CHECKING, Any, Generator -import httpx - if TYPE_CHECKING: + import httpx + from hatch.utils.fs import Path MINIMUM_SLEEP = 2 @@ -20,6 +19,10 @@ @contextmanager def streaming_response(*args: Any, **kwargs: Any) -> Generator[httpx.Response, None, None]: + from secrets import choice + + import httpx + attempts = 0 while True: attempts += 1 diff --git a/tests/index/test_core.py b/tests/index/test_core.py index 20cda4280..9f6b32c1a 100644 --- a/tests/index/test_core.py +++ b/tests/index/test_core.py @@ -41,24 +41,28 @@ def test_project(self, repo_url, expected_url): class TestTLS: def test_default(self, mocker): mock = mocker.patch('httpx._transports.default.create_ssl_context') - _ = PackageIndex('https://foo.internal/a/b/') + index = PackageIndex('https://foo.internal/a/b/') + _ = index.client mock.assert_called_once_with(verify=True, cert=None, trust_env=True) def test_ca_cert(self, mocker): mock = mocker.patch('httpx._transports.default.create_ssl_context') - _ = PackageIndex('https://foo.internal/a/b/', ca_cert='foo') + index = PackageIndex('https://foo.internal/a/b/', ca_cert='foo') + _ = index.client mock.assert_called_once_with(verify='foo', cert=None, trust_env=True) def test_client_cert(self, mocker): mock = mocker.patch('httpx._transports.default.create_ssl_context') - _ = PackageIndex('https://foo.internal/a/b/', client_cert='foo') + index = PackageIndex('https://foo.internal/a/b/', client_cert='foo') + _ = index.client mock.assert_called_once_with(verify=True, cert='foo', trust_env=True) def test_client_cert_with_key(self, mocker): mock = mocker.patch('httpx._transports.default.create_ssl_context') - _ = PackageIndex('https://foo.internal/a/b/', client_cert='foo', client_key='bar') + index = PackageIndex('https://foo.internal/a/b/', client_cert='foo', client_key='bar') + _ = index.client mock.assert_called_once_with(verify=True, cert=('foo', 'bar'), trust_env=True)