Skip to content

Commit

Permalink
Apply default timeout more broadly (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored May 26, 2024
1 parent d38f8c7 commit 9982d03
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/hatch/index/core.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/template/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions src/hatch/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions tests/index/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9982d03

Please sign in to comment.