Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -141,7 +149,6 @@ public class ApiClient {
{{/hasOAuthMethods}}
public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "{{{basePath}}}";
if (!baseUrl.endsWith("/"))
Expand All @@ -164,10 +171,11 @@ public class ApiClient {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
}else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -357,7 +365,11 @@ public class ApiClient {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new Exception("okBuilder is null")
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -114,7 +122,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam

public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "http://petstore.swagger.io:80/v2";
if (!baseUrl.endsWith("/"))
Expand All @@ -128,10 +135,11 @@ public void createDefaultAdapter() {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
}else {
Copy link
Member

Choose a reason for hiding this comment

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

space missing

return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -303,7 +311,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new Exception("okBuilder is null")
Copy link
Member

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)

Copy link
Member

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".

Copy link
Contributor Author

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?

Copy link
Member

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...

Copy link
Contributor Author

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.

}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -115,7 +123,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam

public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "http://petstore.swagger.io:80/v2";
if (!baseUrl.endsWith("/"))
Expand All @@ -130,10 +137,11 @@ public void createDefaultAdapter() {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
}else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -305,7 +313,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new Exception("okBuilder is null")
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -115,7 +123,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam

public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "http://petstore.swagger.io:80/v2";
if (!baseUrl.endsWith("/"))
Expand All @@ -130,10 +137,11 @@ public void createDefaultAdapter() {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
}else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -305,7 +313,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new Exception("okBuilder is null")
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down