Skip to content

Commit

Permalink
Make ClientResponse.history readonly property, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 3, 2015
1 parent 8246853 commit eac7028
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _request(self, method, url, *,

break

resp.history = history
resp._history = tuple(history)
return resp

def ws_connect(self, url, *,
Expand Down
7 changes: 6 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ class ClientResponse:
cookies = None # Response cookies (Set-Cookie)
content = None # Payload stream
headers = None # Response headers, CIMultiDictProxy
history = None # List of responses, if redirects occured

_connection = None # current connection
flow_control_class = FlowControlStreamReader # reader flow control
Expand All @@ -535,6 +534,7 @@ def __init__(self, method, url, host='', *, writer=None, continue100=None):
self._continue = continue100
self._closed = False
self._should_close = True # override by message.should_close later
self._history = ()

def _post_init(self, loop):
self._loop = loop
Expand Down Expand Up @@ -565,6 +565,11 @@ def __repr__(self):
def connection(self):
return self._connection

@property
def history(self):
"""A sequence of of responses, if redirects occured."""
return self._history

def waiting_for_continue(self):
return self._continue is not None

Expand Down
6 changes: 3 additions & 3 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,16 @@ Response History
----------------

If a request was redirected, it is possible to view previous responses using
the history attribute::
the :attr:`~ClientResponse.history` attribute::

>>> r = await aiohttp.get('http://example.com/some/redirect/')
>>> r
<ClientResponse(http://example.com/some/other/url/) [200]>
>>> r.history
[<ClientResponse(http://example.com/some/redirect/) [301]>]
(<ClientResponse(http://example.com/some/redirect/) [301]>,)

If no redirects occured or ``allow_redirects`` is set to ``False``, history will
be an empty list.
be an empty sequence.


Timeouts
Expand Down
5 changes: 3 additions & 2 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,9 @@ Response object

.. attribute:: history

:class:`list` of :class:`ClientResponse` objects of preceding requests, if there
were redirects.
A :class:`~collections.abc.Sequence` of :class:`ClientResponse`
objects of preceding requests if there were redirects, an empty
sequence otherwise.

.. method:: close()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def handler_ok(request):

resp = yield from client.get('/ok')
try:
assert resp.history == []
assert len(resp.history) == 0
assert resp.status == 200
finally:
resp.release()
Expand Down

0 comments on commit eac7028

Please sign in to comment.