Drop per-request `cert`, `verify`, and `trust_env` (#617)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Drop per-request cert/verify/trust_env * Remove cert/verify from the dispatcher API * Apply lint * Reintroduce cert/verify/trust_env on client methods, with errors
authored by Florimond Manca · committed by Tom Christie · parent f3b7999 · Browse files · 7 files·+36 added−126 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+36 added−126 removed
- tests:+2 added−14 removed
- other:+34 added−112 removed
Provenance
- Commit
e9ebd1d- Tree
f65d5f0- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
f3b7999
7 files changed+36 added−126 removed
modified:httpx/dispatch/asgi.py+2 added-9 removed
1 import typing2 3-from ..config import CertTypes, TimeoutTypes, VerifyTypes3+from ..config import TimeoutTypes4 from ..content_streams import ByteStream5 from ..models import Request, Response6 from .base import Dispatcher7 45 unmodified lines53 self.raise_app_exceptions = raise_app_exceptions54 self.root_path = root_path55 self.client = client56 57- async def send(58- self,59- request: Request,60- verify: VerifyTypes = None,61- cert: CertTypes = None,62- timeout: TimeoutTypes = None,63- ) -> Response:64-57+ async def send(self, request: Request, timeout: TimeoutTypes = None) -> Response:58 scope = {59 "type": "http",60 "asgi": {"version": "3.0"},61 "http_version": "1.1",65 unmodified linesmodified:httpx/dispatch/base.py+3 added-11 removed
1 import typing2 from types import TracebackType3 4-from ..config import CertTypes, Timeout, VerifyTypes4+from ..config import Timeout5 from ..models import (6 HeaderTypes,7 QueryParamTypes,8 Request,19 unmodified lines28 *,29 data: RequestData = b"",30 params: QueryParamTypes = None,31 headers: HeaderTypes = None,32- verify: VerifyTypes = None,33- cert: CertTypes = None,32 timeout: Timeout = None,33 ) -> Response:34 request = Request(method, url, data=data, params=params, headers=headers)37- return await self.send(request, verify=verify, cert=cert, timeout=timeout)35+ return await self.send(request, timeout=timeout)36 39- async def send(40- self,41- request: Request,42- verify: VerifyTypes = None,43- cert: CertTypes = None,44- timeout: Timeout = None,45- ) -> Response:37+ async def send(self, request: Request, timeout: Timeout = None) -> Response:38 raise NotImplementedError() # pragma: nocover39 40 async def close(self) -> None:41 pass # pragma: nocover34 unmodified linesmodified:httpx/dispatch/connection_pool.py+2 added-10 removed
138 unmodified lines139 self.keepalive_connections.remove(connection)140 self.max_connections.release()141 await connection.close()142 143- async def send(144- self,145- request: Request,146- verify: VerifyTypes = None,147- cert: CertTypes = None,148- timeout: Timeout = None,149- ) -> Response:143+ async def send(self, request: Request, timeout: Timeout = None) -> Response:144 await self.check_keepalive_expiry()145 connection = await self.acquire_connection(146 origin=request.url.origin, timeout=timeout147 )148 try:155- response = await connection.send(156- request, verify=verify, cert=cert, timeout=timeout157- )149+ response = await connection.send(request, timeout=timeout)150 except BaseException as exc:151 self.active_connections.remove(connection)152 self.max_connections.release()153 raise exc66 unmodified linesmodified:httpx/dispatch/connection.py+6 added-22 removed
38 unmodified lines39 self.uds = uds40 self.open_connection: typing.Optional[OpenConnection] = None41 self.expires_at: typing.Optional[float] = None42 43- async def send(44- self,45- request: Request,46- verify: VerifyTypes = None,47- cert: CertTypes = None,48- timeout: Timeout = None,49- ) -> Response:43+ async def send(self, request: Request, timeout: Timeout = None) -> Response:44 timeout = Timeout() if timeout is None else timeout45 46 if self.open_connection is None:53- await self.connect(verify=verify, cert=cert, timeout=timeout)47+ await self.connect(timeout=timeout)48 49 assert self.open_connection is not None50 response = await self.open_connection.send(request, timeout=timeout)51 52 return response53 60- async def connect(61- self, timeout: Timeout, verify: VerifyTypes = None, cert: CertTypes = None,62- ) -> None:63- ssl = self.ssl.with_overrides(verify=verify, cert=cert)64-54+ async def connect(self, timeout: Timeout) -> None:55 host = self.origin.host56 port = self.origin.port67- ssl_context = await self.get_ssl_context(ssl)57+ ssl_context = await self.get_ssl_context(self.ssl)58 59 if self.release_func is None:60 on_release = None61 else:19 unmodified lines81 82 self.set_open_connection(http_version, socket=stream, on_release=on_release)83 84 async def tunnel_start_tls(95- self,96- origin: Origin,97- proxy_url: URL,98- timeout: Timeout = None,99- cert: CertTypes = None,100- verify: VerifyTypes = True,85+ self, origin: Origin, proxy_url: URL, timeout: Timeout = None,86 ) -> None:87 """88 Upgrade this connection to use TLS, assuming it represents a TCP tunnel.89 """16 unmodified lines106 107 if origin.is_ssl:108 # Pull the socket stream off the internal HTTP connection object,109 # and run start_tls().125- ssl_config = SSLConfig(cert=cert, verify=verify)126- ssl_context = await self.get_ssl_context(ssl_config)110+ ssl_context = await self.get_ssl_context(self.ssl)111 assert ssl_context is not None112 113 logger.trace(f"tunnel_start_tls proxy_url={proxy_url!r} origin={origin!r}")114 socket = await socket.start_tls(59 unmodified linesmodified:httpx/dispatch/proxy_http.py+3 added-16 removed
115 unmodified lines116 connection.origin = origin117 self.active_connections.add(connection)118 119 await connection.tunnel_start_tls(120- origin=origin,121- proxy_url=self.proxy_url,122- timeout=timeout,123- cert=self.cert,124- verify=self.verify,120+ origin=origin, proxy_url=self.proxy_url, timeout=timeout,121 )122 else:123 self.active_connections.add(connection)124 53 unmodified lines178 return (179 self.proxy_mode == DEFAULT_MODE and not origin.is_ssl180 ) or self.proxy_mode == FORWARD_ONLY181 186- async def send(187- self,188- request: Request,189- verify: VerifyTypes = None,190- cert: CertTypes = None,191- timeout: Timeout = None,192- ) -> Response:193-182+ async def send(self, request: Request, timeout: Timeout = None) -> Response:183 if self.should_forward_origin(request.url.origin):184 # Change the request to have the target URL185 # as its full_path and switch the proxy URL186 # for where the request will be sent.2 unmodified lines189 request.url.full_path = target_url190 for name, value in self.proxy_headers.items():191 request.headers.setdefault(name, value)192 204- return await super().send(205- request=request, verify=verify, cert=cert, timeout=timeout206- )193+ return await super().send(request=request, timeout=timeout)194 195 def __repr__(self) -> str:196 return (197 f"HTTPProxy(proxy_url={self.proxy_url!r} "3 unmodified linesmodified:httpx/client.py+18 added-44 removed
221 unmodified lines222 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,223 trust_env: bool = None,224 ) -> Response:225 if cert is not None:226- warnings.warn(226+ raise RuntimeError(227 "Passing a 'cert' argument when making a request on a client "228- "is due to be deprecated. Instantiate a new client instead, "228+ "is not supported anymore. Instantiate a new client instead, "229 "passing any 'cert' arguments to the client itself."230 )231+232 if verify is not None:232- warnings.warn(233+ raise RuntimeError(234 "Passing a 'verify' argument when making a request on a client "234- "is due to be deprecated. Instantiate a new client instead, "235+ "is not supported anymore. Instantiate a new client instead, "236 "passing any 'verify' arguments to the client itself."237 )238+239 if trust_env is not None:238- warnings.warn(240+ raise RuntimeError(241 "Passing a 'trust_env' argument when making a request on a client "240- "is due to be deprecated. Instantiate a new client instead, "242+ "is not supported anymore. Instantiate a new client instead, "243 "passing any 'trust_env' argument to the client itself."244 )245+246 if stream:247 warnings.warn(248 "The 'stream=True' argument is due to be deprecated. "249 "Use 'async with client.stream(method, url, ...) as response' instead."13 unmodified lines263 request,264 stream=stream,265 auth=auth,266 allow_redirects=allow_redirects,264- verify=verify,265- cert=cert,267 timeout=timeout,267- trust_env=trust_env,268 )269 return response270 271 def stream(115 unmodified lines387 *,388 stream: bool = False,389 auth: AuthTypes = None,390 allow_redirects: bool = True,391- verify: VerifyTypes = None,392- cert: CertTypes = None,391 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,394- trust_env: bool = None,392 ) -> Response:393 if request.url.scheme not in ("http", "https"):394 raise InvalidURL('URL scheme must be "http" or "https".')395 396 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)397 401- auth = self.setup_auth(request, trust_env, auth)398+ auth = self.setup_auth(request, auth)399 400 response = await self.send_handling_redirects(404- request,405- auth=auth,406- verify=verify,407- cert=cert,408- timeout=timeout,409- allow_redirects=allow_redirects,401+ request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,402 )403 404 if not stream:405 try:2 unmodified lines408 await response.close()409 410 return response411 420- def setup_auth(421- self, request: Request, trust_env: bool = None, auth: AuthTypes = None422- ) -> Auth:412+ def setup_auth(self, request: Request, auth: AuthTypes = None) -> Auth:413 auth = self.auth if auth is None else auth424- trust_env = self.trust_env if trust_env is None else trust_env414 415 if auth is not None:416 if isinstance(auth, tuple):417 return BasicAuth(username=auth[0], password=auth[1])6 unmodified lines424 username, password = request.url.username, request.url.password425 if username or password:426 return BasicAuth(username=username, password=password)427 439- if trust_env and "Authorization" not in request.headers:428+ if self.trust_env and "Authorization" not in request.headers:429 credentials = self.netrc.get_credentials(request.url.authority)430 if credentials is not None:431 return BasicAuth(username=credentials[0], password=credentials[1])432 3 unmodified lines436 self,437 request: Request,438 auth: Auth,439 timeout: Timeout,451- verify: VerifyTypes = None,452- cert: CertTypes = None,440 allow_redirects: bool = True,441 history: typing.List[Response] = None,442 ) -> Response:443 if history is None:5 unmodified lines449 if request.url in (response.url for response in history):450 raise RedirectLoop()451 452 response = await self.send_handling_auth(466- request, auth=auth, timeout=timeout, verify=verify, cert=cert453+ request, auth=auth, timeout=timeout,454 )455 response.history = list(history)456 457 if not response.is_redirect:7 unmodified lines465 response.call_next = functools.partial(466 self.send_handling_redirects,467 request=request,468 auth=auth,482- verify=verify,483- cert=cert,469 timeout=timeout,470 allow_redirects=False,471 history=history,472 )91 unmodified lines564 raise RedirectBodyUnavailable()565 return request.stream566 567 async def send_handling_auth(583- self,584- request: Request,585- auth: Auth,586- timeout: Timeout,587- verify: VerifyTypes = None,588- cert: CertTypes = None,568+ self, request: Request, auth: Auth, timeout: Timeout,569 ) -> Response:570 auth_flow = auth(request)571 request = next(auth_flow)572 while True:593- response = await self.send_single_request(request, timeout, verify, cert)573+ response = await self.send_single_request(request, timeout)574 try:575 next_request = auth_flow.send(response)576 except StopIteration:577 return response4 unmodified lines582 request = next_request583 await response.close()584 585 async def send_single_request(606- self,607- request: Request,608- timeout: Timeout,609- verify: VerifyTypes = None,610- cert: CertTypes = None,586+ self, request: Request, timeout: Timeout,587 ) -> Response:588 """589 Sends a single request, without handling any redirections.590 """1 unmodified line592 dispatcher = self.dispatcher_for_url(request.url)593 594 try:595 with ElapsedTimer() as timer:620- response = await dispatcher.send(621- request, verify=verify, cert=cert, timeout=timeout622- )596+ response = await dispatcher.send(request, timeout=timeout)597 response.elapsed = timer.elapsed598 response.request = request599 except HTTPError as exc:600 # Add the original request to any HTTPError unless359 unmodified linesmodified:tests/dispatch/test_connections.py+2 added-14 removed
29 unmodified lines30 await response.read()31 32 33 @pytest.mark.usefixtures("async_environment")34-async def test_https_get_with_ssl_defaults(https_server, ca_cert_pem_file):34+async def test_https_get_with_ssl(https_server, ca_cert_pem_file):35 """36- An HTTPS request, with default SSL configuration set on the client.36+ An HTTPS request, with SSL configuration set on the client.37 """38 async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:39 response = await conn.request("GET", https_server.url)40 await response.read()41 assert response.status_code == 20042 assert response.content == b"Hello, world!"43-44-45-@pytest.mark.usefixtures("async_environment")46-async def test_https_get_with_sll_overrides(https_server, ca_cert_pem_file):47- """48- An HTTPS request, with SSL configuration set on the request.49- """50- async with HTTPConnection(origin=https_server.url) as conn:51- response = await conn.request("GET", https_server.url, verify=ca_cert_pem_file)52- await response.read()53- assert response.status_code == 20054- assert response.content == b"Hello, world!"