Skip to content
Myr

For non-streaming cases, populate `request.content` automatically. (#1583)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* For non-streaming cases, populate request.content * Linting

authored by Tom Christie · committed by GitHub · parent 8fd5b71 · Browse files · 3 files+24 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+24 added−22 removed

  • tests:+6 added−5 removed
  • other:+18 added−17 removed

3 files+24 added−22 removed

modified:httpx/_exceptions.py+4 added-8 removed
292 unmodified lines
293294295class ResponseNotRead(StreamError):296 """297 Attempted to access response content, without having called `read()`298 after a streaming response.297 Attempted to access streaming response content, without having called `read()`.298 """299300 def __init__(self) -> None:302 message = (303 "Attempted to access response content, without having called `read()` "304 "after a streaming response."305 )301 message = "Attempted to access streaming response content, without having called `read()`."302 super().__init__(message)303304305class RequestNotRead(StreamError):306 """311 Attempted to access request content, without having called `read()`.307 Attempted to access streaming request content, without having called `read()`.308 """309310 def __init__(self) -> None:315 message = "Attempted to access request content, without having called `read()`."311 message = "Attempted to access streaming request content, without having called `read()`."312 super().__init__(message)313314315class ResponseClosed(StreamError):
25 unmodified lines
modified:httpx/_models.py+14 added-9 removed
827 unmodified lines
828 else:829 headers, stream = encode_request(content, data, files, json)830 self._prepare(headers)831 self.stream = stream832 # Load the request body, except for streaming content.833 if isinstance(stream, ByteStream):834 self.read()835836 def _prepare(self, default_headers: typing.Dict[str, str]) -> None:837 for key, value in default_headers.items():838 # Ignore Transfer-Encoding if the Content-Length has been set explicitly.
32 unmodified lines
871 """872 if not hasattr(self, "_content"):873 assert isinstance(self.stream, typing.Iterable)874 self._content = b"".join(self.stream)875 if not isinstance(self.stream, ByteStream):872 # If a streaming request has been read entirely into memory, then873 # we can replace the stream with a raw bytes implementation,874 # to ensure that any non-replayable streams can still be used.875 self.stream = ByteStream(self._content)876 # If a streaming request has been read entirely into memory, then877 # we can replace the stream with a raw bytes implementation,878 # to ensure that any non-replayable streams can still be used.879 self.stream = ByteStream(self._content)880 return self._content881882 async def aread(self) -> bytes:883 """
1 unmodified line
885 """886 if not hasattr(self, "_content"):887 assert isinstance(self.stream, typing.AsyncIterable)888 self._content = b"".join([part async for part in self.stream])889 if not isinstance(self.stream, ByteStream):885 # If a streaming request has been read entirely into memory, then886 # we can replace the stream with a raw bytes implementation,887 # to ensure that any non-replayable streams can still be used.888 self.stream = ByteStream(self._content)890 # If a streaming request has been read entirely into memory, then891 # we can replace the stream with a raw bytes implementation,892 # to ensure that any non-replayable streams can still be used.893 self.stream = ByteStream(self._content)894 return self._content895896 def __repr__(self) -> str:897 class_name = self.__class__.__name__
47 unmodified lines
945 else:946 headers, stream = encode_response(content, text, html, json)947 self._prepare(headers)948 self.stream = stream944 if content is None or isinstance(content, (bytes, str)):949 if isinstance(stream, ByteStream):950 # Load the response body, except for streaming content.951 self.read()952953 self._num_bytes_downloaded = 0
591 unmodified lines
modified:tests/models/test_requests.py+6 added-5 removed
95 unmodified lines
96 content = b"".join([part async for part in request.stream])97 assert content == request.content9899100@pytest.mark.asyncio101async def test_cannot_access_content_without_read():102 # Ensure a request may still be streamed if it has been read.103 # Needed for cases such as authentication classes that read the request body.104 request = httpx.Request("POST", "http://example.org", json={"test": 123})100def test_cannot_access_streaming_content_without_read():101 # Ensure that streaming requests102 def streaming_body(): # pragma: nocover103 yield ""104105 request = httpx.Request("POST", "http://example.org", content=streaming_body())106 with pytest.raises(httpx.RequestNotRead):107 request.content108109
67 unmodified lines