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

Feature new response property in ClientWebSocketResponse #8572

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGES/8572.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added ``response`` attribute to the :py:class:`~aiohttp.ClientWebSocketResponse` class. It allows users to have access to response headers and cookies of a websocket request
-- by :user:`leszekhanusz`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Kyrylo Perevozchikov
Kyungmin Lee
Lars P. Søndergaard
Lee LieWhite
Leszek Hanusz
Liu Hua
Louis-Philippe Huberdeau
Loïc Lajeanne
Expand Down
4 changes: 4 additions & 0 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ def compress(self) -> int:
def client_notakeover(self) -> bool:
return self._client_notakeover

@property
def response(self) -> ClientResponse:
return self._response

def get_extra_info(self, name: str, default: Any = None) -> Any:
"""extra info from connection transport"""
conn = self._response.connection
Expand Down
7 changes: 7 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,13 @@ manually.
May be ``None`` if server and client protocols are
not overlapping.

.. attribute:: response

The :class:`ClientResponse <ClientResponse>` response object.
Can be used to get the response headers and cookies.

.. versionadded:: 4.0

.. method:: get_extra_info(name, default=None)

Reads optional extra information from the connection's transport.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ async def test_ws_connect(ws_key: Any, loop: Any, key_data: Any) -> None:
assert res.protocol == "chat"
assert hdrs.ORIGIN not in m_req.call_args[1]["headers"]

# Checking new response attribute
assert res.response.headers == resp.headers


async def test_ws_connect_read_timeout_is_reset_to_inf(
ws_key: Any, loop: Any, key_data: Any
Expand Down
Loading