Skip to content

Interceptor decoder - #534

Merged
codefromthecrypt merged 7 commits into
OpenFeign:masterfrom
odin-delrio:feature/interceptor-decoder
May 6, 2017
Merged

codefromthecrypt merged 7 commits into
OpenFeign:masterfrom
odin-delrio:feature/interceptor-decoder

Conversation

@odin-delrio

@odin-delrio odin-delrio commented Mar 11, 2017 •

Copy link
Copy Markdown
Contributor

Hi all!

I have an use case in which I have to interact with some HTTP API that is currently responding ciphered JSONs (I can't change this), so I need to first decipher that JSON before giving it to the Decoder (JacksonDecoder in my case).

To do this in a generic way I implemented an InterceptorDecoder that allows you to intercept the feign response before the desired decoder is called.

This also could be used to rewrite the response headers.
For example, okhttp allow to do that in a similar way: https://github.com/square/okhttp/wiki/Interceptors#rewriting-responses

@odin-delrio odin-delrio changed the title InterceptorDecoder implemented. Interceptor decoder Mar 11, 2017
@spencergibb

Copy link
Copy Markdown
Contributor

Why don't we wait to see if more people want something like this in core.

@spencergibb spencergibb added the waiting for votes Enhancements or changes proposed that need more support before consideration label Mar 13, 2017
@odin-delrio

Copy link
Copy Markdown
Contributor Author

@spencergibb Cool! I just updated the description to reference headers rewriting ;)

@codefromthecrypt

Copy link
Copy Markdown

tons of votes on this one (relatively speaking)

import java.io.IOException;
import java.lang.reflect.Type;

public class InterceptorDecoder implements Decoder {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ResponseMappingDecoder? Add some javadoc since this is a public type, and tighten up with finals etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ResponseMappingDecoder seems a good name for me, I can change it!
Also you're right about the final modifier to the properties and some javadoc, I'm on it ;)

Comment thread README.md Outdated
```java
GitHub github = Feign.builder()
.decoder(
new InterceptorDecoder(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tempted to make this stuff private and only expose the builder..

Ex. mapAndDecode((response, type) -> myTransformingLogic(response, type), new GsonDecoder())

thoughts?

@odin-delrio odin-delrio Apr 15, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is ok for me! But for make this Decoder private (at least package private) I have to move it from the feign.codec package to the feignone... is that ok for you?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it could be a static package protected member of feign

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

fyi we'll be doing a release in a couple days

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for be patient!
I'm not sure that I understand what do you want to say with "static package protected member of feign"...
make the ResponseMappingDecoder an inner class insied the feign.Feign abstract class?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

yep that's what I meant. The users don't need to know about this type.

@codefromthecrypt

Copy link
Copy Markdown

can those voting mention what you want to use this for?

Comment thread core/src/main/java/feign/Feign.java Outdated
/**
* Map function to apply to the response before decoding it.
*/
public interface ResponseMapper {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

make this part a top-level type.

Also, please add an example in the javadoc.

ex.

<pre>{@code
// your compelling example here:
}</pre>

Comment thread README.md Outdated

```java
GitHub github = Feign.builder()
.mapAndDecode((response, type) -> myTransformingLogic(response, type), new GsonDecoder())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

myTransformingLogic is coolio, but there's probably a better hint. Ex what made you want this in the first place? is there a concrete, but easy to understand example? Notably, the map happens before the decoding, so something like unwrapping a nested json envelope doesn't seem to be realistic.

Let's make this easy to understand!

ps.. leave out the github thing as their api is already nice :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had 2 use cases for this, ciphered JSONS and JSONP responses...
I explained the second one because I think that could be more usual.

@codefromthecrypt codefromthecrypt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

made some comments to tighten up.. basically need to understand what the preprocessing will do for folks.

next, update CHANGES (which will be 9.5)

@codefromthecrypt
codefromthecrypt merged commit 56c105d into OpenFeign:master May 6, 2017
@odin-delrio
odin-delrio deleted the feature/interceptor-decoder branch May 6, 2017 22:57
phymbert pushed a commit to phymbert/feign that referenced this pull request Aug 22, 2017
Adds `Feign.Builder.mapAndDecode()` to allow response preprocessing before decoding it
velo pushed a commit that referenced this pull request Oct 7, 2024
Adds `Feign.Builder.mapAndDecode()` to allow response preprocessing before decoding it
velo pushed a commit that referenced this pull request Oct 8, 2024
Adds `Feign.Builder.mapAndDecode()` to allow response preprocessing before decoding it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for votes Enhancements or changes proposed that need more support before consideration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants