Skip to content

Commit

Permalink
added raise_for_status #1724
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 17, 2017
1 parent c184e3c commit f7b8ef8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGES

- Fixed None timeout support #1720

- Added session's `raise_for_status` parameter, automatically calls raise_for_status() on any request. #1724

- Cleanup timer and loop handle on any client exception.


Expand Down
7 changes: 6 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
response_class=ClientResponse,
ws_response_class=ClientWebSocketResponse,
version=http.HttpVersion11,
cookie_jar=None, connector_owner=True,
cookie_jar=None, connector_owner=True, raise_for_status=False,
read_timeout=None, conn_timeout=None):

implicit_loop = False
Expand Down Expand Up @@ -95,6 +95,7 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
self._version = version
self._read_timeout = read_timeout
self._conn_timeout = conn_timeout
self._raise_for_status = raise_for_status

# Convert to list of tuples
if headers:
Expand Down Expand Up @@ -284,6 +285,10 @@ def _request(self, method, url, *,

break

# check response status
if self._raise_for_status:
resp.raise_for_status()

# register connection
if handle is not None:
if resp.connection is not None:
Expand Down
8 changes: 7 additions & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ The client session supports the context manager protocol for self closing.
.. class:: ClientSession(*, connector=None, loop=None, cookies=None, \
headers=None, skip_auto_headers=None, \
auth=None, version=aiohttp.HttpVersion11, \
cookie_jar=None, read_timeout=None, conn_timeout=None)
cookie_jar=None, read_timeout=None, conn_timeout=None, \
raise_for_status=False)

The class for creating client sessions and making requests.

Expand Down Expand Up @@ -99,6 +100,11 @@ The client session supports the context manager protocol for self closing.

.. versionadded:: 0.22

:param bool raise_for_status: Automatically call `raise_for_status()` for each response.
(default is False)

.. versionadded:: 2.0

:param float read_timeout: Request operations timeout. read_timeout is
cumulative for all request operations (request, redirects, responses,
data consuming)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,3 +1978,17 @@ def handler_redirect(request):

with pytest.warns(DeprecationWarning):
yield from client.get('/', chunked=1024)


@asyncio.coroutine
def test_raise_for_status(loop, test_client):
@asyncio.coroutine
def handler_redirect(request):
return web.HTTPBadRequest()

app = web.Application(loop=loop)
app.router.add_route('GET', '/', handler_redirect)
client = yield from test_client(app, raise_for_status=True)

with pytest.raises(aiohttp.ClientResponseError):
yield from client.get('/')

0 comments on commit f7b8ef8

Please sign in to comment.