Skip to content

Commit

Permalink
[3.6] Added properties of default ClientSession params to ClientSessi…
Browse files Browse the repository at this point in the history
…on class (#4240) (#4242)

* [3.6] Added properties of default ClientSession params to ClientSession class (#4240)
(cherry picked from commit 5f291b0)

Co-authored-by: Pavel Filatov <triksrimer@gmail.com>
  • Loading branch information
asvetlov and paulefoe authored Oct 20, 2019
1 parent b2ceae8 commit 0dbf937
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/3882.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added properties of default ClientSession params to ClientSession class so it is available for introspection
57 changes: 57 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from types import SimpleNamespace, TracebackType
from typing import ( # noqa
Any,
Awaitable,
Callable,
Coroutine,
FrozenSet,
Generator,
Generic,
Iterable,
Expand Down Expand Up @@ -957,6 +960,60 @@ def timeout(self) -> Union[object, ClientTimeout]:
"""Timeout for the session."""
return self._timeout

@property
def headers(self) -> 'CIMultiDict[str]':
"""The default headers of the client session."""
return self._default_headers

@property
def skip_auto_headers(self) -> FrozenSet[istr]:
"""Headers for which autogeneration should be skipped"""
return self._skip_auto_headers

@property
def auth(self) -> Optional[BasicAuth]:
"""An object that represents HTTP Basic Authorization"""
return self._default_auth

@property
def json_serialize(self) -> JSONEncoder:
"""Json serializer callable"""
return self._json_serialize

@property
def connector_owner(self) -> bool:
"""Should connector be closed on session closing"""
return self._connector_owner

@property
def raise_for_status(
self
) -> Union[bool, Callable[[ClientResponse], Awaitable[None]]]:
"""
Should `ClientResponse.raise_for_status()`
be called for each response
"""
return self._raise_for_status

@property
def auto_decompress(self) -> bool:
"""Should the body response be automatically decompressed"""
return self._auto_decompress

@property
def trust_env(self) -> bool:
"""
Should get proxies information
from HTTP_PROXY / HTTPS_PROXY environment variables
or ~/.netrc file if present
"""
return self._trust_env

@property
def trace_configs(self) -> List[TraceConfig]:
"""A list of TraceConfig instances used for client tracing"""
return self._trace_configs

def detach(self) -> None:
"""Detach connector from session without closing the former.
Expand Down
76 changes: 76 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,82 @@ The client session supports the context manager protocol for self closing.

.. versionadded:: 3.7

.. attribute:: headers

HTTP Headers that sent with every request

May be either *iterable of key-value pairs* or
:class:`~collections.abc.Mapping`
(e.g. :class:`dict`,
:class:`~multidict.CIMultiDict`).

.. versionadded:: 3.7

.. attribute:: skip_auto_headers

Set of headers for which autogeneration skipped.

:class:`frozenset` of :class:`str` or :class:`~aiohttp.istr` (optional)

.. versionadded:: 3.7

.. attribute:: auth

An object that represents HTTP Basic Authorization.

:class:`~aiohttp.BasicAuth` (optional)

.. versionadded:: 3.7

.. attribute:: json_serialize

Json serializer callable.

By default :func:`json.dumps` function.

.. versionadded:: 3.7

.. attribute:: connector_owner

Should connector be closed on session closing

:class:`bool` (optional)

.. versionadded:: 3.7

.. attribute:: raise_for_status

Should :meth:`ClientResponse.raise_for_status()` be called for each response

Either :class:`bool` or :class:`callable`

.. versionadded:: 3.7

.. attribute:: auto_decompress

Should the body response be automatically decompressed

:class:`bool` default is ``True``

.. versionadded:: 3.7

.. attribute:: trust_env

Should get proxies information from HTTP_PROXY / HTTPS_PROXY environment
variables or ~/.netrc file if present

:class:`bool` default is ``False``

.. versionadded:: 3.7

.. attribute:: trace_config

A list of :class:`TraceConfig` instances used for client
tracing. ``None`` (default) is used for request tracing
disabling. See :ref:`aiohttp-client-tracing-reference` for more information.

.. versionadded:: 3.7

.. comethod:: request(method, url, *, params=None, data=None, json=None,\
cookies=None, headers=None, skip_auto_headers=None, \
auth=None, allow_redirects=True,\
Expand Down
8 changes: 4 additions & 4 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ async def test_close_coro(create_session) -> None:
async def test_init_headers_simple_dict(create_session) -> None:
session = await create_session(headers={"h1": "header1",
"h2": "header2"})
assert (sorted(session._default_headers.items()) ==
assert (sorted(session.headers.items()) ==
([("h1", "header1"), ("h2", "header2")]))


async def test_init_headers_list_of_tuples(create_session) -> None:
session = await create_session(headers=[("h1", "header1"),
("h2", "header2"),
("h3", "header3")])
assert (session._default_headers ==
assert (session.headers ==
CIMultiDict([("h1", "header1"),
("h2", "header2"),
("h3", "header3")]))
Expand All @@ -88,7 +88,7 @@ async def test_init_headers_MultiDict(create_session) -> None:
session = await create_session(headers=MultiDict([("h1", "header1"),
("h2", "header2"),
("h3", "header3")]))
assert (session._default_headers ==
assert (session.headers ==
CIMultiDict([("H1", "header1"),
("H2", "header2"),
("H3", "header3")]))
Expand All @@ -99,7 +99,7 @@ async def test_init_headers_list_of_tuples_with_duplicates(
session = await create_session(headers=[("h1", "header11"),
("h2", "header21"),
("h1", "header12")])
assert (session._default_headers ==
assert (session.headers ==
CIMultiDict([("H1", "header11"),
("H2", "header21"),
("H1", "header12")]))
Expand Down

0 comments on commit 0dbf937

Please sign in to comment.