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

configure raise_for_status per request feature #3073

Merged
merged 5 commits into from
Jun 14, 2018
Merged
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
1 change: 1 addition & 0 deletions CHANGES/3073.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``raise_for_status`` request parameter
5 changes: 4 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ async def _request(self, method, url, *,
compress=None,
chunked=None,
expect100=False,
raise_for_status=None,
read_until_eof=True,
proxy=None,
proxy_auth=None,
Expand Down Expand Up @@ -465,7 +466,9 @@ async def _request(self, method, url, *,
break

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

# register connection
Expand Down
29 changes: 27 additions & 2 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ The client session supports the context manager protocol for self closing.
Automatically call :meth:`ClientResponse.raise_for_status()` for
each response, ``False`` by default.

This parameter can be overridden when you making a request, e.g.::

client_session = aiohttp.ClientSession(raise_for_status=True)
resp = await client_session.get(url, raise_for_status=False)
async with resp:
assert resp.status == 200

Set the parameter to ``True`` if you need ``raise_for_status``
for most of cases but override ``raise_for_status``
for those requests where
you need to handle responses with status 400 or higher.

:param timeout: a :class:`ClientTimeout` settings structure, 5min
total timeout by default.

Expand Down Expand Up @@ -205,7 +217,7 @@ The client session supports the context manager protocol for self closing.
headers=None, skip_auto_headers=None, \
auth=None, allow_redirects=True,\
max_redirects=10,\
compress=None, chunked=None, expect100=False,\
compress=None, chunked=None, expect100=False, raise_for_status=None,\
read_until_eof=True, proxy=None, proxy_auth=None,\
timeout=sentinel, ssl=None, \
verify_ssl=None, fingerprint=None, \
Expand Down Expand Up @@ -279,6 +291,13 @@ The client session supports the context manager protocol for self closing.
:param bool expect100: Expect 100-continue response from server.
``False`` by default (optional).

:param bool raise_for_status: Automatically call :meth:`ClientResponse.raise_for_status()` for
response if set to ``True``.
If set to ``None`` value from ``ClientSession`` will be used.
``None`` by default (optional).

.. versionadded:: 3.4

:param bool read_until_eof: Read response until EOF if response
does not have Content-Length header.
``True`` by default (optional).
Expand Down Expand Up @@ -639,7 +658,7 @@ certification chaining.
allow_redirects=True, max_redirects=10, \
encoding='utf-8', \
version=HttpVersion(major=1, minor=1), \
compress=None, chunked=None, expect100=False, \
compress=None, chunked=None, expect100=False, raise_for_status=None, \
connector=None, loop=None,\
read_until_eof=True)

Expand Down Expand Up @@ -684,6 +703,12 @@ certification chaining.
:param bool expect100: Expect 100-continue response from server.
``False`` by default (optional).

:param bool raise_for_status: Automatically call :meth:`ClientResponse.raise_for_status()` for
response if set to ``True``.
If set to ``None`` value from ``ClientSession`` will be used.
``None`` by default (optional).
.. versionadded:: 3.4

:param aiohttp.connector.BaseConnector connector: BaseConnector sub-class
instance to support connection pooling.

Expand Down
27 changes: 27 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,33 @@ async def handler_redirect(request):
await client.get('/')


async def test_raise_for_status_per_request(aiohttp_client):

async def handler_redirect(request):
raise web.HTTPBadRequest()

app = web.Application()
app.router.add_route('GET', '/', handler_redirect)
client = await aiohttp_client(app)

with pytest.raises(aiohttp.ClientResponseError):
await client.get('/', raise_for_status=True)


async def test_raise_for_status_disable_per_request(aiohttp_client):

async def handler_redirect(request):
raise web.HTTPBadRequest()

app = web.Application()
app.router.add_route('GET', '/', handler_redirect)
client = await aiohttp_client(app, raise_for_status=True)

resp = await client.get('/', raise_for_status=False)
assert 400 == resp.status
resp.close()


async def test_invalid_idna():
session = aiohttp.ClientSession()
try:
Expand Down