Skip to content
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

Convert response body to Stream #734

Closed
amesgen opened this issue Dec 11, 2019 · 6 comments · Fixed by #750
Closed

Convert response body to Stream #734

amesgen opened this issue Dec 11, 2019 · 6 comments · Fixed by #750

Comments

@amesgen
Copy link

amesgen commented Dec 11, 2019

Right now, there is no "direct" way to consume a response body as a Stream<Item = Result<Bytes>>. IMO this would be a good thing (either via some into_stream function, or a direct impl, like in 0.9). I went ahead and made the existing into_stream in Body public, but this also requires to make ImplStream public, which is probably suboptimal.

Related: How about enabling the stream feature by default, just as it is done in hyper 0.13?

@Arnavion
Copy link

You can make a stream in a slightly round-about way with:

async fn next_chunk(mut response: reqwest::Response) -> Option<(Result<bytes::Bytes, reqwest::Error>, reqwest::Response)> {
	let chunk = response.chunk().await;
	let chunk = chunk.transpose()?;
	Some((chunk, response))
}

let body = futures_util::stream::unfold(response, next_chunk);

But yes, it would be easier if it was just an into_stream on Response itself.

@amesgen
Copy link
Author

amesgen commented Dec 15, 2019

Yeah, that's why I wrote that there is no "direct" way. Your solution has a minor drawback: body is lifetime-constrained by response, so if you want to return body from a function (for e.g. some function async fn(url: &str) -> Result<impl Stream<Item = Result<Bytes>>>), it is not easily possible without sth like owning_ref. I am tired...

Another way is to use async-stream which does not have this problems, but instead you occasionally get weird compiler errors (see e.g. here).

@Arnavion
Copy link

Yeah, I switched from the unfold method to async-stream for my own stuff too.

@bartlomieju
Copy link

@seanmonstar is this issue is something you would consider before releasing reqwest? Ability to "take" body as a stream is the only missing feature for us at Deno in regard to updating reqwest.

@seanmonstar
Copy link
Owner

What if we added Response::into_stream(self) -> impl Stream<Item = crate::Result<Bytes>>?

@bartlomieju
Copy link

What if we added Response::into_stream(self) -> impl Stream<Item = crate::Result<Bytes>>?

That'd be perfect!

seanmonstar added a commit that referenced this issue Dec 23, 2019
This converts the `Response` into a `Stream` of `Bytes`.

Closes #734
seanmonstar added a commit that referenced this issue Dec 23, 2019
This converts the `Response` into a `Stream` of `Bytes`.

Closes #734
seanmonstar added a commit that referenced this issue Dec 23, 2019
This converts the `Response` into a `Stream` of `Bytes`.

Closes #734
seanmonstar added a commit that referenced this issue Dec 23, 2019
This converts the `Response` into a `Stream` of `Bytes`.

Closes #734
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants