feign icon indicating copy to clipboard operation
feign copied to clipboard

Add possibility to get body from feign.Response as a decoded object

Open jkubrynski opened this issue 10 years ago • 29 comments

Currently decoding of a Response.body() needs to be done manually. It'll be great to get a method body().asObject(MyDtoType.class)

jkubrynski avatar Jun 14 '16 07:06 jkubrynski

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?

codefromthecrypt avatar Jul 11 '16 00:07 codefromthecrypt

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

jkubrynski avatar Jul 11 '16 09:07 jkubrynski

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);
  }
}

codefromthecrypt avatar Jul 11 '16 10:07 codefromthecrypt

Don't you think it will be useful for end users to have such functionality out-of-the-box?

jkubrynski avatar Jul 11 '16 21:07 jkubrynski

we don't typically add features only requested once, especially features that are for edge cases that can be worked around.

codefromthecrypt avatar Jul 12 '16 01:07 codefromthecrypt

closing this issue as won't fix. if it comes up often, we can reconsider

codefromthecrypt avatar Jul 12 '16 01:07 codefromthecrypt

ps some time back I added a HACKING file which explains some of these sentiments.

https://github.com/Netflix/feign/blob/master/HACKING.md

codefromthecrypt avatar Jul 12 '16 01:07 codefromthecrypt

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 :(

matheus208 avatar May 30 '17 17:05 matheus208

hmm.. if you are only using feign to decode bodies, maybe yeah there's no point.

codefromthecrypt avatar Jun 06 '17 03:06 codefromthecrypt

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.

codefromthecrypt avatar Jun 06 '17 03:06 codefromthecrypt

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.

matheus208 avatar Jun 06 '17 12:06 matheus208

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

codefromthecrypt avatar Jun 06 '17 15:06 codefromthecrypt

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

mihir83in avatar Jul 08 '17 09:07 mihir83in

+1

bahrmichael avatar Mar 08 '18 15:03 bahrmichael

+1

pcals90 avatar Mar 09 '18 00:03 pcals90

I'm using this one now:

// feign.Response response
org.apache.commons.io.IOUtils.toString(response.body().asReader())

bahrmichael avatar Mar 09 '18 12:03 bahrmichael

+1

doughert avatar Mar 20 '18 14:03 doughert

+1

kent2171 avatar Apr 17 '18 09:04 kent2171

++

Yanislavcore avatar Apr 27 '18 16:04 Yanislavcore

+1

LeapSpace avatar Jul 07 '18 03:07 LeapSpace