Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proxy and proxy_auth varriables to ClientSession.__init__ #9207

Merged
merged 20 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/9207.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add default proxy variables to ClientSession -- by :user:`meshya`.
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Matthieu Hauglustaine
Matthieu Rigal
Matvey Tingaev
Meet Mangukiya
Meshya
Michael Ihnatenko
Michał Górny
Mikhail Burshteyn
Expand Down
12 changes: 12 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ class ClientSession:
"_max_line_size",
"_max_field_size",
"_resolve_charset",
"_default_proxy",
"_default_proxy_auth",
)

def __init__(
Expand All @@ -261,6 +263,8 @@ def __init__(
connector: Optional[BaseConnector] = None,
cookies: Optional[LooseCookies] = None,
headers: Optional[LooseHeaders] = None,
proxy: Optional[StrOrURL] = None,
proxy_auth: Optional[BasicAuth] = None,
skip_auto_headers: Optional[Iterable[str]] = None,
auth: Optional[BasicAuth] = None,
json_serialize: JSONEncoder = json.dumps,
Expand Down Expand Up @@ -361,6 +365,9 @@ def __init__(

self._resolve_charset = fallback_charset_resolver

self._default_proxy = proxy
self._default_proxy_auth = proxy_auth

def __init_subclass__(cls: Type["ClientSession"]) -> None:
raise TypeError(
"Inheritance class {} from ClientSession "
Expand Down Expand Up @@ -480,6 +487,11 @@ async def _request(
for i in skip_auto_headers:
skip_headers.add(istr(i))

if proxy is None:
proxy = self._default_proxy
if proxy_auth is None:
proxy_auth = self._default_proxy_auth

if proxy is not None:
try:
proxy = URL(proxy)
Expand Down
7 changes: 7 additions & 0 deletions docs/client_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,13 @@ Authentication credentials can be passed in proxy URL::
session.get("http://python.org",
proxy="http://user:pass@some.proxy.com")

And you may set default proxy::

proxy_auth = aiohttp.BasicAuth('user', 'pass')
async with aiohttp.ClientSession(proxy="http://proxy.com", proxy_auth=proxy_auth) as session:
async with session.get("http://python.org") as resp:
print(resp.status)

Contrary to the ``requests`` library, it won't read environment
variables by default. But you can do so by passing
``trust_env=True`` into :class:`aiohttp.ClientSession`
Expand Down
53 changes: 53 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,59 @@
]


async def test_default_proxy(loop: asyncio.AbstractEventLoop) -> None:
proxy_url = URL("http://proxy.example.com")
proxy_auth = mock.Mock()
proxy_url2 = URL("http://proxy.example2.com")
proxy_auth2 = mock.Mock()

class OnCall(Exception):
pass

request_class_mock = mock.Mock(side_effect=OnCall())
session = ClientSession(
proxy=proxy_url, proxy_auth=proxy_auth, request_class=request_class_mock
)

assert session._default_proxy == proxy_url, "`ClientSession._default_proxy` not set"
assert (
session._default_proxy_auth == proxy_auth
), "`ClientSession._default_proxy_auth` not set"

try:
await session.get(
"http://example.com",
)
except OnCall:
Fixed Show fixed Hide fixed
pass

assert request_class_mock.called, "request class not called"
assert (
request_class_mock.call_args[1].get("proxy") == proxy_url
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
assert (
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"

request_class_mock.reset_mock()
try:
await session.get(
"http://example.com", proxy=proxy_url2, proxy_auth=proxy_auth2
)
except OnCall:
Fixed Show fixed Hide fixed
pass

assert request_class_mock.called, "request class not called"
assert (
request_class_mock.call_args[1].get("proxy") == proxy_url2
), "`ClientSession._request` uses default proxy not one used in ClientSession.get"
assert (
request_class_mock.call_args[1].get("proxy_auth") == proxy_auth2
), "`ClientSession._request` uses default proxy_auth not one used in ClientSession.get"

await session.close()


async def test_request_tracing(
loop: asyncio.AbstractEventLoop, aiohttp_client: AiohttpClient
) -> None:
Expand Down
Loading