Add possibility to get body from feign.Response as a decoded object
Currently decoding of a Response.body() needs to be done manually. It'll be great to get a method body().asObject(MyDtoType.class)
not sure we want to add that responsibility to Response. I recall retrofit reversing that feature, too. In the mean time, you can remember the codec for these edge cases, right?
The problem is that I don't need to manually care about deserializing responses until I return target object. When I want to wrap it into Response for example to get additionally response code I have to manually read body, which brings imho too much overhead compared to standard use case
yeah I don't think we are optimizing for routine use of Response and decoder together.
It may not seem obvious, but you could make your own Response type and a "decoder" for it. That type could have the method you want, and keep the reference to a real decoder which makes it work.
ex.
class MyDecoder implements Decoder {
final Decoder delegate;
public Object decode(Response response, Type type) throws IOException {
if (type == MyResponse.class) return new MyResponse(response, delegate);
return delegate.decode(response, type);
}
}
class MyResponse {
--snip--
public int status() {
return feignResponse.status;
}
<T> T asObject(Class<T> clazz) {
return (T) feignDecoder.decode(feignResponse, clazz);
}
}
Don't you think it will be useful for end users to have such functionality out-of-the-box?
we don't typically add features only requested once, especially features that are for edge cases that can be worked around.
closing this issue as won't fix. if it comes up often, we can reconsider
ps some time back I added a HACKING file which explains some of these sentiments.
https://github.com/Netflix/feign/blob/master/HACKING.md
I was expecting Response to behave like that as well.
I have to implement some requests that set headers in the response (for pagination), and I have to resort to using feign.Response to read the headers, but the downside is that I have to handle the ResponseBody myself, which defeats the purpose of using Feign in the first place :(
hmm.. if you are only using feign to decode bodies, maybe yeah there's no point.
what I mean is that Feign's is mostly abstraction of control apis: decoding is one of many convenience functions, and most functionality centers on code for highest value to most.
if you are doing everything else manually and only using feign to decode bodies, maybe use gson or something directly.
I'm using feign to abstract requests, handle errors, decode responses and circuit breaking (with HystrixFeign) with minimal code. And it excels at that!
The only lacking feature is to retrieve information other than the RequestBody from a response (mainly headers), which makes me resort to feign.Response, which doesn't include some of these convenience functions out of the box.
The only lacking feature is to retrieve information other than the RequestBody from a response (mainly headers), which makes me resort to feign.Response, which doesn't include some of these convenience functions out of the box.
Decoder can do this.. it intentionally allows you to access the entire response for this reason. To make a pagination thing you'd have to compose a decoder that understands whatever headers you need with a json or otherwise decoder.
there's a couple examples of pagination in denominator (old code, but still relevant) here https://github.com/Netflix/denominator/blob/master/route53/src/main/java/denominator/route53/Route53ZoneApi.java#L134
Hi Adrian,
Could you post / host / point to a complete example of your mentioned solution.
final Decoder delegate;
public Object decode(Response response, Type type) throws IOException {
if (type == MyResponse.class) return new MyResponse(response, delegate);
return delegate.decode(response, type);
}
}
class MyResponse {
--snip--
public int status() {
return feignResponse.status;
}
<T> T asObject(Class<T> clazz) {
return (T) feignDecoder.decode(feignResponse, clazz);
}
}```
This will be very helpful to those who stumble upon same problem
+1
+1
I'm using this one now:
// feign.Response response
org.apache.commons.io.IOUtils.toString(response.body().asReader())
+1
+1
++
+1