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

Deprecated BaseRequest.has_body, replaced with 2 new attributes (#2005) #2169

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from . import hdrs, multipart
from .helpers import HeadersMixin, SimpleCookie, reify, sentinel
from .streams import EmptyStreamReader
from .web_exceptions import HTTPRequestEntityTooLarge


Expand Down Expand Up @@ -457,9 +458,22 @@ def content(self):

@property
def has_body(self):
"""Return True if request has HTTP BODY, False otherwise."""
"""Return True if request's HTTP BODY can be read, False otherwise."""
warnings.warn(
"Deprecated, use .can_read_body #2005",
DeprecationWarning, stacklevel=2)
return not self._payload.at_eof()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for the case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added old attr back into test cases - is this okay or should I break it out into another test case?


@property
def can_read_body(self):
"""Return True if request's HTTP BODY can be read, False otherwise."""
return not self._payload.at_eof()

@property
def body_exists(self):
"""Return True if request has HTTP BODY, False otherwise."""
return type(self._payload) is not EmptyStreamReader

@asyncio.coroutine
def release(self):
"""Release request.
Expand Down
3 changes: 3 additions & 0 deletions changes/2005.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Deprecated BaseRequest.has_body in favour of BaseRequest.can_read_body
- Added BaseRequest.body_exists attribute that stays static for the lifetime
of the request
24 changes: 21 additions & 3 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,33 @@ and :ref:`aiohttp-web-signals` handlers.

Read-only property.

.. attribute:: has_body
.. attribute:: body_exists

Return ``True`` if request has *HTTP BODY*, ``False`` otherwise.

Read-only :class:`bool` property.

.. versionadded:: 0.16
.. versionadded:: 2.3

.. attribute:: can_read_body

.. attribute:: content_type
Return ``True`` if request's *HTTP BODY* can be read, ``False`` otherwise.

Read-only :class:`bool` property.

.. versionadded:: 2.3

.. attribute:: has_body

Return ``True`` if request's *HTTP BODY* can be read, ``False`` otherwise.

Read-only :class:`bool` property.

.. deprecated:: 2.3

Use :meth:`can_read_body` instead.

.. attribute:: content_type

Read-only property with *content* part of *Content-Type* header.

Expand Down
6 changes: 4 additions & 2 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ def test_empty_content_for_query_without_body(loop, test_client):

@asyncio.coroutine
def handler(request):
assert not request.has_body
assert not request.body_exists
assert not request.can_read_body
return web.Response()

app = web.Application()
Expand All @@ -710,7 +711,8 @@ def test_empty_content_for_query_with_body(loop, test_client):

@asyncio.coroutine
def handler(request):
assert request.has_body
assert request.body_exists
assert request.can_read_body
body = yield from request.read()
return web.Response(body=body)

Expand Down