-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Make ApiClient in retrofit2 be able to use own OkHttpClient #6699
Conversation
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @bkabrda (2020/01) |
Please update the samples by running |
I don't really like the |
Yes we can include in the v5.0.0 release (release date not yet finalized) |
I actually kind of expected that this is not a beautiful solution but I thought why not give a try. Could we also do your proposed solution for a CallFactory like |
@cbornet I'm not sure what exactly your thought was, but I would suggest something like this
so the change for the current user would be that they would have to make a call to setOkBuilderClient but I think that would be a reasonable change(?) |
I did a similar change for the regular okhttp client #6401. Would be good if we can make this the same for both? |
That would be breaking and very unexpected... Looking a little more, I think your solution can be accepted but I would change the logic a bit. public ApiClient setClient(OkHttpClient client) {
this.okHttpClient = client;
return this; then in if (okHttpClient != null) {
adapterBuilder.client(okHttpClient);
else {
adapterBuilder.client(okBuilder.build());
} and make clear in setClient's javadoc that the client provided will be used and the APIClient's method's that would have configured the client (such as the auth methods which configure an interceptor) will not have any effect anymore. |
@cbornet what about taking the okhttp client as a constructor param? this removes any uncertainty about which order things need to be called but more importantly doesn't instantiate an |
@jonnii Taking the okhttp client as a constructor param instead of the setClient method? Or what do you mean? |
@tgerth if you look at
instead of |
No it creates a builder not the actual client. A client is created only when calling createService. |
Sorry, i meant a builder - that's also not free: it instantiates a dispatcher, a connection pool etc... |
Are those allocations really that heavy compared to the rest ? |
I think it comes down to your usage pattern. If you can create a single |
so maybe something like this:
|
Yes. And null checks on okBuilder where needed. |
CircleCI reports some failure in building the Java client, e.g.
Please refer to https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/3722/workflows/3fe3d6fa-0df5-4e7e-bf30-7c5e135c23a7/jobs/17090/steps for the details. |
@@ -357,7 +365,9 @@ public class ApiClient { | |||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations"); | |||
} | |||
apiAuthorizations.put(authName, authorization); | |||
okBuilder.addInterceptor(authorization); | |||
if(okBuilder != null){ |
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.
May be better to throw an Exception with an explicit message if okBuilder is null ?
sry I forgot a semicolon😂😅 |
@@ -137,7 +137,7 @@ public void createDefaultAdapter() { | |||
public <S> S createService(Class<S> serviceClass) { | |||
if (okHttpClient != null) { | |||
return adapterBuilder.client(okHttpClient).build().create(serviceClass); | |||
else { | |||
}else { |
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.
space missing
if(okBuilder != null){ | ||
okBuilder.addInterceptor(authorization); | ||
if(okBuilder == null){ | ||
throw new Exception("okBuilder is null") |
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.
Use RuntimeException ? (or even better : create an ApiClientException)
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.
The message could be more explicit. Something like "the ApiClient was created with a built okClient so it's not possible to add an authorization interceptor to it".
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.
What is the advantage of creating an own ApiClientException comparing to the RuntimeException here in this case?
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.
See https://rules.sonarsource.com/java/RSPEC-112 . But we already have a bunch of RuntimeException so...
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.
Ah ok I see but as you said we already have many RuntimeExceptions so I will use a RuntimeException here if that's ok and then maybe in future you/we can change them to own Exceptions.
* master: (142 commits) update python samples clarify direction of py client side validation flag (#6850) fix erronous cmd arg example for docker in readme (#6846) [BUG] [JAVA] Fix multiple files upload (#4803) (#6808) [kotlin][client] fix retrofit dependencies (#6836) [PowerShell] add more fields to be customized (#6835) [Java][WebClient]remove the dead code from java ApiClient.mustache (#6556) [PHP] Better handling of invalid data (array) (#6760) Make ApiClient in retrofit2 be able to use own OkHttpClient (#6699) mark python2 support in flask as deprecated (#6653) update samples [Java][jersey2] Add a getter for the User-Agent header value (#6831) Provides a default nil value for optional init parameters (#6827) [Java] Deprecate feignVersion option (#6824) [R] Enum R6Class Support, closes #3367 (#5728) [Rust][Client] Unify sync/async client structure (#6753) [php-ze-ph] Set required PHP version to ^7.2 (#6763) [Java][client][native][Gradle] Add missing jackson-databind-nullable (#6802) Improve sttpOpenApiClient generator (#6684) Update docker-tag-latest-release.yml ...
Description
If I want to set a customized okHttpClient to the adapterBuilder like this:
the client gets overwritten by this:
and unfortunately this method:
is of no use to me because the okBuilder would just get the clients fields but I need/want to have the client itself respectively its instance. So I added a boolean field "okBuilderUsed" which checks if the okBuilder is used and if it's used then createService will return its client and if not then createService will just return the adapterBuilder:
PR checklist
./bin/generate-samples.sh
to update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/config/java*
. For Windows users, please run the script in Git BASH.master