From 5c9a3912a95fa6dfdd4bb5b0af6a062bcb40663c Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sat, 20 Oct 2018 07:38:53 -0400 Subject: [PATCH] cached _content --- responder/models.py | 9 ++++++--- tests/test_responder.py | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/responder/models.py b/responder/models.py index 6340005..f4dbcc3 100644 --- a/responder/models.py +++ b/responder/models.py @@ -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): diff --git a/tests/test_responder.py b/tests/test_responder.py index a8c6091..51659e9 100644 --- a/tests/test_responder.py +++ b/tests/test_responder.py @@ -405,3 +405,5 @@ def test_template_rendering(api, session): r = session.get(api.url_for(view)) assert r.text == "hello" + +