-
-
Notifications
You must be signed in to change notification settings - Fork 839
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
Support for chunk_size
#1277
Support for chunk_size
#1277
Conversation
@@ -912,19 +913,28 @@ def read(self) -> bytes: | |||
self._content = b"".join(self.iter_bytes()) | |||
return self._content | |||
|
|||
def iter_bytes(self) -> typing.Iterator[bytes]: | |||
def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about chunk_size with default None
?
I do agree that it looks more suitable, but requests
provides us with defaults chunk_size=1 or 512
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, when chunk_size=None
we just return the input content
unchanged, as one single big chunk.
Yes, this would deviate from what Requests seems to do, but:
- Setting a non-
None
default would break backward compatibility on our side. - Defaulting to "transparently pass the chunk sent by the server" is probably the most reasonable approach anyway.
That said, we'd need to add this deviation from Requests to the compatibility guide. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Superb!
There may be room for factoring in some of the introduced logic within the _decoders.py
module (bunch of repetition there), but happy to treat it as a follow-up.
Edit: so as per #1277 (comment), should we add a small note in our Requests compatibility guide on the default chunking behavior?
@@ -912,19 +913,28 @@ def read(self) -> bytes: | |||
self._content = b"".join(self.iter_bytes()) | |||
return self._content | |||
|
|||
def iter_bytes(self) -> typing.Iterator[bytes]: | |||
def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, when chunk_size=None
we just return the input content
unchanged, as one single big chunk.
Yes, this would deviate from what Requests seems to do, but:
- Setting a non-
None
default would break backward compatibility on our side. - Defaulting to "transparently pass the chunk sent by the server" is probably the most reasonable approach anyway.
That said, we'd need to add this deviation from Requests to the compatibility guide. 👍
I really like this - it's a great answer to my question in #1392 about safely retrieving a truncated version of a resource when dealing with a URL provided by an untrusted user, as an extra protection against denial-of-service on top of the HTTPX timeouts. |
And if I'm correct, this would close #394? |
Just clarify when it says " |
@piersoh Correct yes. For HTTP/2 that'll mean chunk sizes that correspond to the data frames on the wire. |
Closes #394
iter_raw(chunk_size: int=None)
iter_bytes(chunk_size: int=None)
iter_text(chunk_size: int=None)
First bit of work towards
chunk_size
on theresponse.iter_[raw|bytes|text]
methods,using a decoder class in line with the other decoding.
Nice thing about this is that it's really easy to unit test, eg...