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

Implement aiohttp.ClientResponse.ok property #4711

Merged
merged 9 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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/4711.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ClientResponse.ok property for checking status code under 400.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Eugene Ershov
Eugene Naydenov
Eugene Nikolaiev
Eugene Tolmachev
Evan Kepner
Evert Lammerts
Felix Yan
Fernanda Guimarães
Expand Down
13 changes: 13 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,19 @@ def release(self) -> Any:
self._cleanup_writer()
return noop()

@property
def ok(self) -> bool:
"""Returns ``True`` if ``status`` is less than ``400``, ``False`` if not.

This is **not** a check for ``200 OK`` but a check that the response
status is under 400.
"""
try:
self.raise_for_status()
except ClientResponseError:
return False
return True

def raise_for_status(self) -> None:
if 400 <= self.status:
# reason should always be not None for a started response
Expand Down
5 changes: 5 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,11 @@ Response object

HTTP status reason of response (:class:`str`), e.g. ``"OK"``.

.. attribute:: ok

Boolean representation of HTTP status code (:class:`bool`).
``True`` if ``status`` is less than ``400``; otherwise, ``False``.

.. attribute:: method

Request's method (:class:`str`).
Expand Down
24 changes: 24 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,30 @@ async def handler_redirect(request):
assert data == body


@pytest.mark.parametrize(
("status", "expected_ok"),
(
(200, True),
(201, True),
(301, True),
(400, False),
(403, False),
(500, False),
),
ids=lambda c, o: f"HTTP status code {c} is{' ' if o else ' not'} ok",
)
async def test_ok_from_status(aiohttp_client, status, expected_ok) -> None:

async def handler(request):
return web.Response(status=status, body=b'')

app = web.Application()
app.router.add_route('GET', '/endpoint', handler)
client = await aiohttp_client(app, raise_for_status=False)
resp = await client.get('/endpoint')

assert resp.ok is expected_ok

async def test_raise_for_status(aiohttp_client) -> None:

async def handler(request):
Expand Down