Repository: aio-libs/aiohttp

Issue:
Expose ClientSession defaults such as headers and auth for introspection
## Long story short

Too many attributes of ClientSession are private without an explicit need to. The following constructor arguments can never be accessed after creating a session:

- `headers`
- `skip_auto_headers`
- `auth`
- `json_serialize`
- ~`request_class`~
- ~`response_class`~
- ~`ws_response_class`~
- `connector_owner`
- `raise_for_status`
- `timeout`
- `auto_decompress`
- `trust_env`
- `trace_configs`

## Expected behaviour

I want to be able to create a new session based on an existing session, only tweaking some aspects. Without access to these attributes, you can't properly create a new session instance with only some aspects replaced.

## Actual behaviour

You have to access API private attribute to get at this information, making it much harder than necessary to create an updated wrapper as session needs change.

For example, I'm currently implementing an OAuth 2.0 authentication handler, which automatically fetches an authentication token if required for the current request, transparently. We create a client session in a different part of the app, shared between a group of objects, some of which need to dynamically switch to support OAuth 2.0. 

Rather than have to create a new session for each of object regardless of OAuth needs (preventing connection re-use between some of these objects where the same server is involved) or having to introspect every request to see if OAuth applies to this specific request, I want to create a new session only when OAuth is actually needed, reusing the same connection pool, if and when OAuth is needed. But this requires knowing the headers, timeouts and trace config on the session used so far.


Discussion:
PR for providing properties for them is welcome, except `request_class`, `response_class`, and `ws_response_class`. 

These three arguments will be removed soon.
What was the reason to hide them on first place? Perhaps exporting the API is not too cool. I have never needed that by making sessions persistent in one place and reusing whatever common bits I needed there.

Just asking, because not having them publicly available might have been done on purpose.
The reason for not exposing API was: nobody asked.
I had doubts what parameters should be exposed but now I see no objections.
#4193 adds a timeout property. 
Looking for a champion for other names
No need to expose all properties at once, multiple PRs are acceptable.

Resolving patch:
diff --git a/aiohttp/client.py b/aiohttp/client.py
--- a/aiohttp/client.py
+++ b/aiohttp/client.py
@@ -14,6 +14,7 @@
     Awaitable,
     Callable,
     Coroutine,
+    FrozenSet,
     Generator,
     Generic,
     Iterable,
@@ -918,6 +919,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.
 


Test patch:
diff --git a/tests/test_client_session.py b/tests/test_client_session.py
--- a/tests/test_client_session.py
+++ b/tests/test_client_session.py
@@ -70,7 +70,7 @@ 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")]))
 
 
@@ -78,7 +78,7 @@ 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")]))
@@ -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")]))
@@ -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")]))