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

Create a http::Response<hyper::Body> from a reqwest::Response #1954

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/async_impl/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ impl From<Response> for Body {
}
}

/// A `Response` can be transformed into a classic http hyper body response.
impl From<Response> for http::Response<hyper::Body> {
fn from(r: Response) -> http::Response<hyper::Body> {
let (parts, body) = r.res.into_parts();
http::Response::from_parts(parts, hyper::Body::wrap_stream(body))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could give the body without wrapping it prematurely, and then anyone needing to unify it could wrap it themselves. If they didn't need to, we could save them a boxed trait object.

Is there any reason we shouldn't do that?

Copy link
Contributor Author

@Threated Threated Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good I just don't know how as the body returned by into_parts has the private Decoder type so we need to wrap it some way right? If this were a method I suppose we could return http::Response<impl Stream<..>>. What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In at least the blocking implementation, I'd like to use a similar conversion trait for a simple Vec<u8>. See how ureq provides a few of them:

https://github.com/algesten/ureq/blob/916ffbffbe098d28d9ac142054484fd71423d9a6/src/http_interop.rs#L55-L129

Seems there's also #1258, #1483, #1612 to track and implement (something) like this. The latter being a PR implementing the same trait in src/async_impl/response.rs but for anything with a From<Decoder>. @seanmonstar is one of these PRs "superseding" the other? Can I help gain some traction on any of these?

}
}

#[cfg(test)]
mod tests {
use super::Response;
Expand Down