python-trio / hip

A new Python HTTP client for everybody
Other
79 stars 22 forks source link

Figure out what interface the Response object should expose for streaming data #123

Open njsmith opened 5 years ago

njsmith commented 5 years ago

In urllib3, HTTPResponse inherits from io.IOBase and implements the standard Python file interface. This is an interesting issue that discusses a bug in their implementation, and also why people value this feature: https://github.com/urllib3/urllib3/issues/1305

We need to decide what interface our version of HTTPResponse should expose. Constraints:

You'll notice that these constraints are contradictory :-). Especially since the ReceiveStream and IOBase interfaces actually conflict with each other: they both support iteration, but for ReceiveStream it gives arbitrary chunks of bytes, while for IOBase it gives lines.

Some options:

sethmlarson commented 4 years ago

Had more discussions about this, here's the results of that discussion:

Also discussed the usage of .text()/.data()/.json() as methods that load all data onto the Response. There were questions about whether you should be able to call .text() and then maybe .text() again.

If we are one-shot only then you'd only be able to call a single "body" function on a response. However @pquentin brought up that this would make things annoying for debugging as you'd have to reissue requests to get another body.

Another thought was that if a response body is completely loaded into memory we could hold onto that body internally on the request to allow calling .text(), .json(), and .data() again if needed.