reqwest-middleware icon indicating copy to clipboard operation
reqwest-middleware copied to clipboard

wrap reqwest::Response to match Errors

Open peanutbother opened this issue 1 year ago • 2 comments

Motivations

[taken from #57] There is currently no wrapper for reqwest::Response. As a result, resp.body().await? [...]

... returns a reqwest::Error instead of reqwest_middleware::Error.

Solution

It would be possible to create wrapper types but this is not really feasible. Instead there should be a wrapper to make the api more uniform.

Additional context

I'm implementing reqwest-middleware for https://github.com/peanutbother/api-client to extend its features I'm trying to change to reqwest-middleware which introduces non uniform apis:

https://github.com/peanutbother/api-client/blob/main/src/lib.rs#L453 where

self.request(::reqwest::Method::$method, format!($url).as_str(), $crate::Body::Json(request)).await?.json().await

fails due to the response not being wrapped.

peanutbother avatar Mar 26 '23 03:03 peanutbother

I might be missing something, if you change your return type to reqwest_middleware::Error, wouldn't it work with reqwest-middleware as-is? You'd need to change it anyways if we were to apply this suggestion here, no?

Also, cool crate 👀

tl-rodrigo-gryzinski avatar Mar 28 '23 15:03 tl-rodrigo-gryzinski

the call to response.json().await returns a reqwest::Result instead of a reqwest_middleware::Result

I solved it by mapping the error for now but it would be more convenient if the Result type would be consistent.

I updated the link in the first post as it was pointing to a previous version.

peanutbother avatar Mar 29 '23 11:03 peanutbother