cached _content

This commit is contained in:
2018-10-20 07:38:53 -04:00
parent 5bb9f96701
commit 5c9a3912a9
2 changed files with 8 additions and 3 deletions
+6 -3
View File
@@ -90,13 +90,14 @@ class QueryDict(dict):
# TODO: add slots
class Request:
__slots__ = ["_starlette", "formats", "_headers", "_encoding", "api"]
__slots__ = ["_starlette", "formats", "_headers", "_encoding", "api", "_content"]
def __init__(self, scope, receive, api=None):
self._starlette = StarletteRequest(scope, receive)
self.formats = None
self._encoding = None
self.api = api
self._content = None
headers = CaseInsensitiveDict()
for header, value in self._starlette.headers.items():
@@ -179,12 +180,14 @@ class Request:
@property
async def content(self):
"""The Request body, as bytes. Must be awaited."""
return await self._starlette.body()
if not self._content:
self._content = await self._starlette.body()
return self._content
@property
async def text(self):
"""The Request body, as unicode. Must be awaited."""
return (await self._starlette.body()).decode(await self.encoding)
return (await self.content).decode(await self.encoding)
@property
async def declared_encoding(self):
+2
View File
@@ -405,3 +405,5 @@ def test_template_rendering(api, session):
r = session.get(api.url_for(view))
assert r.text == "hello"