Skip to content

Commit

Permalink
Deprecated BaseRequest.has_body, replaced with 2 new attributes (#2005)…
Browse files Browse the repository at this point in the history
… (#2169)

* Deprecated BaseRequest.has_body, replaced with 2 new attributes

* Test obsolete BaseRequest.has_body attr
  • Loading branch information
David Poirier authored and asvetlov committed Aug 7, 2017
1 parent bdd862b commit dda632b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
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()

@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
4 changes: 4 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ def test_empty_content_for_query_without_body(loop, test_client):

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

Expand All @@ -710,6 +712,8 @@ def test_empty_content_for_query_with_body(loop, test_client):

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

0 comments on commit dda632b

Please sign in to comment.