Skip to content

Commit

Permalink
Make the Client.proxy and HTTPTransport.proxy types the same
Browse files Browse the repository at this point in the history
  • Loading branch information
karpetrosyan committed Oct 10, 2023
1 parent 189a4f2 commit 88eb99b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions httpx/_transports/default.py
Expand Up @@ -29,6 +29,8 @@

import httpcore

from httpx import URL

from .._config import DEFAULT_LIMITS, Limits, Proxy, create_ssl_context
from .._exceptions import (
ConnectError,
Expand All @@ -47,7 +49,7 @@
WriteTimeout,
)
from .._models import Request, Response
from .._types import AsyncByteStream, CertTypes, SyncByteStream, VerifyTypes
from .._types import AsyncByteStream, CertTypes, ProxyTypes, SyncByteStream, VerifyTypes
from .base import AsyncBaseTransport, BaseTransport

T = typing.TypeVar("T", bound="HTTPTransport")
Expand Down Expand Up @@ -124,13 +126,14 @@ def __init__(
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
trust_env: bool = True,
proxy: typing.Optional[Proxy] = None,
proxy: typing.Optional[ProxyTypes] = None,
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy

if proxy is None:
self._pool = httpcore.ConnectionPool(
Expand Down Expand Up @@ -263,13 +266,14 @@ def __init__(
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
trust_env: bool = True,
proxy: typing.Optional[Proxy] = None,
proxy: typing.Optional[ProxyTypes] = None,
uds: typing.Optional[str] = None,
local_address: typing.Optional[str] = None,
retries: int = 0,
socket_options: typing.Optional[typing.Iterable[SOCKET_OPTION]] = None,
) -> None:
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy

if proxy is None:
self._pool = httpcore.AsyncConnectionPool(
Expand Down
8 changes: 8 additions & 0 deletions tests/client/test_proxies.py
Expand Up @@ -328,3 +328,11 @@ def test_proxy_and_proxies_together():
httpx.AsyncClient(
proxies={"all://": "http://127.0.0.1"}, proxy="http://127.0.0.1"
)


def test_proxy_with_mounts():
proxy_transport = httpx.HTTPTransport(proxy="http://127.0.0.1")
client = httpx.Client(mounts={"http://": proxy_transport})

transport = client._transport_for_url(httpx.URL("http://example.com"))
assert transport == proxy_transport

0 comments on commit 88eb99b

Please sign in to comment.