Skip to content
Myr

Cache netrc authentication per-client (#400)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Can Sarıgöl · committed by Seth Michael Larson · parent 5e4f54d · Browse files · 4 files+29 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+29 added−18 removed

  • tests:+9 added−9 removed
  • other:+20 added−9 removed

4 files+29 added−18 removed

modified:docs/advanced.md+5 added
modified:httpx/client.py+13 added-5 removed
1import functools2import inspect3import netrc4import typing5from types import TracebackType67import hstspreload
40 unmodified lines
48 Response,49 ResponseContent,50 URLTypes,51)51from .utils import ElapsedTimer, get_environment_proxies, get_netrc_login52from .utils import ElapsedTimer, get_environment_proxies, get_netrc535455class BaseClient:56 def __init__(
220 unmodified lines
277 username=request.url.username, password=request.url.password278 )279280 if trust_env:280 netrc_login = get_netrc_login(request.url.authority)281 if netrc_login:282 username, _, password = netrc_login283 return BasicAuthMiddleware(username=username, password=password)281 netrc_info = self._get_netrc()282 if netrc_info:283 netrc_login = netrc_info.authenticators(request.url.authority)284 if netrc_login:285 username, _, password = netrc_login286 assert password is not None287 return BasicAuthMiddleware(username=username, password=password)288289 return None290291 @functools.lru_cache(1)292 def _get_netrc(self) -> typing.Optional[netrc.netrc]:293 return get_netrc()294295 def _dispatcher_for_request(296 self, request: AsyncRequest, proxies: typing.Dict[str, AsyncDispatcher]297 ) -> AsyncDispatcher:298 """Gets the AsyncDispatcher instance that should be used for a given Request"""
778 unmodified lines
modified:httpx/utils.py+2 added-4 removed
96 unmodified lines
9798NETRC_STATIC_FILES = (Path("~/.netrc"), Path("~/_netrc"))99100101def get_netrc_login(host: str) -> typing.Optional[typing.Tuple[str, str, str]]:101def get_netrc() -> typing.Optional[netrc.netrc]:102 NETRC_FILES = (Path(os.getenv("NETRC", "")),) + NETRC_STATIC_FILES103 netrc_path = None104105 for file_path in NETRC_FILES:
3 unmodified lines
109 break110111 if netrc_path is None:112 return None113 return netrc.netrc(str(netrc_path))114114 netrc_info = netrc.netrc(str(netrc_path))115 return netrc_info.authenticators(host) # type: ignore115117116def get_ca_bundle_from_env() -> typing.Optional[str]:117 if "SSL_CERT_FILE" in os.environ:118 ssl_file = Path(os.environ["SSL_CERT_FILE"])119 if ssl_file.is_file():
232 unmodified lines
modified:tests/test_utils.py+9 added-9 removed
8 unmodified lines
9from httpx.utils import (10 ElapsedTimer,11 get_ca_bundle_from_env,12 get_environment_proxies,13 get_netrc_login,13 get_netrc,14 guess_json_utf,15 obfuscate_sensitive_headers,16 parse_header_links,17)
35 unmodified lines
53 assert guess_json_utf(data) == expected545556def test_bad_get_netrc_login():57 assert get_netrc_login("url") is None5857 os.environ["NETRC"] = "tests/.netrc"60 assert get_netrc_login("url") is None58 assert str(get_netrc()) is not None5962 os.environ["NETRC"] = "wrongpath"63 assert get_netrc_login("url") is None6460 from httpx import utils6162 utils.NETRC_STATIC_FILES = ()6364 os.environ["NETRC"] = "wrongpath"65 assert utils.get_netrc() is None6667 os.environ["NETRC"] = ""69 assert utils.get_netrc_login("url") is None68 assert utils.get_netrc() is None697071def test_get_netrc_login():72 os.environ["NETRC"] = "tests/.netrc"73 netrc = get_netrc()74 assert get_netrc_login("netrcexample.org") == (74 assert netrc.authenticators("netrcexample.org") == (75 "example-username",76 None,77 "example-password",78 )
125 unmodified lines