-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Allow HystrixFeign.Builder to be used incrementally. #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,18 +6,10 @@ | |
| import java.lang.reflect.Method; | ||
| import java.util.Map; | ||
|
|
||
| import feign.Client; | ||
| import feign.Contract; | ||
| import feign.Feign; | ||
| import feign.InvocationHandlerFactory; | ||
| import feign.Logger; | ||
| import feign.Request; | ||
| import feign.RequestInterceptor; | ||
| import feign.Retryer; | ||
| import feign.Target; | ||
| import feign.codec.Decoder; | ||
| import feign.codec.Encoder; | ||
| import feign.codec.ErrorDecoder; | ||
|
|
||
| /** | ||
| * Allows Feign interfaces to return HystrixCommand or rx.Observable or rx.Single objects. Also | ||
|
|
@@ -30,25 +22,30 @@ public static Builder builder() { | |
| return new Builder(); | ||
| } | ||
|
|
||
| // Doesn't extend Feign.Builder for two reasons: | ||
| // * Hide invocationHandlerFactory - as this isn't customizable | ||
| // * Provide a path to the new fallback method w/o using covariant return types | ||
| public static final class Builder { | ||
| private final Feign.Builder delegate = new Feign.Builder(); | ||
| private Contract contract = new Contract.Default(); | ||
| // Extends Feign.Builder because downstream projects use it incrementally. | ||
| public static final class Builder extends Feign.Builder { | ||
|
|
||
| /** | ||
| * @see #target(Class, String, Object) | ||
| */ | ||
| public <T> T target(Target<T> target, final T fallback) { | ||
| delegate.invocationHandlerFactory(new InvocationHandlerFactory() { | ||
| public Builder() { | ||
| // both needed if fallbacks, not used | ||
| contract(new HystrixDelegatingContract(new Contract.Default())); | ||
| super.invocationHandlerFactory(buildInvocationHandlerFactory(null)); | ||
| } | ||
|
|
||
| private InvocationHandlerFactory buildInvocationHandlerFactory(final Object fallback) { | ||
| return new InvocationHandlerFactory() { | ||
| @Override | ||
| public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) { | ||
| return new HystrixInvocationHandler(target, dispatch, fallback); | ||
| } | ||
| }); | ||
| delegate.contract(new HystrixDelegatingContract(contract)); | ||
| return delegate.build().newInstance(target); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @see #target(Class, String, Object) | ||
| */ | ||
| public <T> T target(Target<T> target, final T fallback) { | ||
| super.invocationHandlerFactory(buildInvocationHandlerFactory(fallback)); | ||
| return build().newInstance(target); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -79,137 +76,26 @@ public InvocationHandler create(Target target, Map<Method, MethodHandler> dispat | |
| * } | ||
| * }; | ||
| * | ||
| * GitHub github = HystrixFeign.builder() | ||
| * GitHub github = HystrixFeign.builder(fallback) | ||
| * ... | ||
| * .target(GitHub.class, "https://api.github.com", fallback); | ||
| * .target(GitHub.class, "https://api.github.com"); | ||
| * }</pre> | ||
| * | ||
| * @see #target(Target, Object) | ||
| */ | ||
| public <T> T target(Class<T> apiType, String url, T fallback) { | ||
| return target(new Target.HardCodedTarget<T>(apiType, url), fallback); | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#contract | ||
| */ | ||
| public Builder contract(Contract contract) { | ||
| this.contract = contract; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#build | ||
| */ | ||
| public Feign build() { | ||
| delegate.invocationHandlerFactory(new HystrixInvocationHandler.Factory()); | ||
| delegate.contract(new HystrixDelegatingContract(contract)); | ||
| return delegate.build(); | ||
| } | ||
|
|
||
| // re-declaring methods in Feign.Builder is same work as covariant overrides, | ||
| // but results in less complex bytecode. | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#target(Class, String) | ||
| */ | ||
| public <T> T target(Class<T> apiType, String url) { | ||
| return target(new Target.HardCodedTarget<T>(apiType, url)); | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#target(Target) | ||
| */ | ||
| public <T> T target(Target<T> target) { | ||
| return build().newInstance(target); | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#logLevel | ||
| */ | ||
| public Builder logLevel(Logger.Level logLevel) { | ||
| delegate.logLevel(logLevel); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#client | ||
| */ | ||
| public Builder client(Client client) { | ||
| delegate.client(client); | ||
| @Override | ||
| public Feign.Builder invocationHandlerFactory(InvocationHandlerFactory invocationHandlerFactory) { | ||
| //TODO: throw exception, log warning or do nothing? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the method can no longer be hidden, what do you think should be done here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsupportedoperationexception |
||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#retryer | ||
| */ | ||
| public Builder retryer(Retryer retryer) { | ||
| delegate.retryer(retryer); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#retryer | ||
| */ | ||
| public Builder logger(Logger logger) { | ||
| delegate.logger(logger); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#encoder | ||
| */ | ||
| public Builder encoder(Encoder encoder) { | ||
| delegate.encoder(encoder); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#decoder | ||
| */ | ||
| public Builder decoder(Decoder decoder) { | ||
| delegate.decoder(decoder); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#decode404 | ||
| */ | ||
| public Builder decode404() { | ||
| delegate.decode404(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#errorDecoder | ||
| */ | ||
| public Builder errorDecoder(ErrorDecoder errorDecoder) { | ||
| delegate.errorDecoder(errorDecoder); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#options | ||
| */ | ||
| public Builder options(Request.Options options) { | ||
| delegate.options(options); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#requestInterceptor | ||
| */ | ||
| public Builder requestInterceptor(RequestInterceptor requestInterceptor) { | ||
| delegate.requestInterceptor(requestInterceptor); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see feign.Feign.Builder#requestInterceptors | ||
| * @see feign.Feign.Builder#contract | ||
| */ | ||
| public Builder requestInterceptors(Iterable<RequestInterceptor> requestInterceptors) { | ||
| delegate.requestInterceptors(requestInterceptors); | ||
| return this; | ||
| public Feign.Builder contract(Contract contract) { | ||
| return super.contract(new HystrixDelegatingContract(contract)); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the change is in a terminal method you need it's original type 😞. One option is to move create a
fallback()method which could retain the fluent style. Another is to create anAbstractBuilder<B>that returnsBfrom each method allowing downstream builders to have custom terminal methods.