Support custom requestbuilder interceptors #3476
Closed
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.
What
Support custom RequestBuilder interceptors, along with the possibility to add custom parameter annotations (see other PR). The RequestBuilderInterceptor is called just before Retrofit creates an okhttp3.Request and the
okhttp3.Request.Builder
andretrofit.Invocation
are provided.Why
This RequestBuilderInterceptor allows developers full freedom to mutate and alter the okhttp request using their own custom annotations (both method and parameter annotations) via the Invocation class.
My first intuition was to add support for custom ParameterHandlers, but with that, you still can't modify the request based on method arguments. Also, to implement correct support for custom ParameterHandlers, the retrofit.RequestBuilder would need to be public and a lot of methods needed to be added for it to be usable. It would basically become a copy of the okhttp3.Request.Builder. For my personal usecase I actually need access to both the method and parameter annotations when mutating the request.
How
Retrofit keeps track of a list of RequestBuilderInterceptors.
These interceptors are provided to the RequestFactory.
When the Request is created as a last step the RequestBuilderInterceptors are called.