ureq
ureq copied to clipboard
Add conversions to and from `http` crate types
Hi there, I was wondering if there's any interest in adding conversions to and from http::Request<T> and http::Response<T> to the corresponding types in this crate. In the application I'm working on, we like to use http to define all of our requests in a "sans io" fashion and plug in clients later, and I'm interested in trying to use ureq as one client option. It seems to me that there shouldn't be any technical reason this wouldn't be possible, would you be open to considering this addition, perhaps under an http (or otherwise named) feature flag?
Hi! Welcome to ureq!
I like the http crate. In a future major version bump we could even consider using it instead of having our own type.
I would welcome and http crate interop under a feature flag. If we implement the From trait, such as impl From<http::Request<Body>> for ureq::Request – the question is what we do with the Body parameter. Do we define our own body type? Do we make a bunch of trait bounds like Body: io::Read, Body: Into<String> etc?
Thoughts?
For reference, here's what reqwest does: reqwest::Request has
impl<T> TryFrom<http::request::Request<T>> for Request
where
T: Into<reqwest::Body>,
https://docs.rs/reqwest/latest/reqwest/struct.Request.html#impl-TryFrom%3CRequest%3CT%3E%3E
And reqwest::Body has these impls:
[From<&'static [u8]>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3C%26%27static%20%5Bu8%5D%3E)
[From<&'static str>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3C%26%27static%20str%3E)
[From<Body>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CBody%3E)
[From<Bytes>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CBytes%3E)
[From<File>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CFile%3E)
[From<Response>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CResponse%3E)
[From<String>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CString%3E)
[From<Vec<u8, Global>>](https://docs.rs/reqwest/latest/reqwest/struct.Body.html#impl-From%3CVec%3Cu8%2C%20Global%3E%3E)
Applying a similar approach to ureq would mean implementing Froms for each of the input types that ureq::Request knows about: impl Serialize (from_json()); &[u8] (send_bytes()), &str (send_string()), &[(&str, &str)] (send_form()), and impl Read (send()).
We would need a new type, RequestWithBody, since our Request type represents a request with everything but the body. We might choose to make that available outside of the feature flag, since it could be a generally useful thing. Its implied counterpart, ResponseWithBody, could be useful in solving #505.
Or we could skip transforming to a ureq::Request and just have ureq::Agent::request_http_crate(http::Method, http::Request<UreqBody> – where UreqBody would be a new type representing the different body types we can use.
I have some time today that I think I'm going to try this out. I'll report back with progress later, and maybe open a draft PR so we can talk about what API looks best for this.
Maybe a simpler step would be impl {Try}From<http::request::Builder> for ureq::Request, that seems to be the directly equivalent type containing all the data other than the body (and is what I'm going to have to manually implement to use crates_index::SparseIndex::make_cache_request with ureq).
Oh wait, I see there is an http-interop feature already. It would be good to activate this on docs.rs so that these impls can be seen (and I think that resolves this issue?).
Looks like this was implemented to some extent in #591 (the http-interop feature mentioned above) and it is now visible in the docs. Perhaps this can be closed as completed?
It can indeed. Thanks!