forked from loafle/openapi-generator-original
Make ApiClient in retrofit2 be able to use own OkHttpClient (#6699)
* set adapterBuilder.client() only if okBuilder was used in retrofit2 * updated the samples * added field okHttpClient and updated samples * bug fixed, added exception if okBuilder is null * added semicolon * added space, changed Exception to RuntimeException and changed its message * updated the samples
This commit is contained in:
@@ -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) {
|
||||
@@ -141,7 +149,6 @@ public class ApiClient {
|
||||
{{/hasOAuthMethods}}
|
||||
public void createDefaultAdapter() {
|
||||
json = new JSON();
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "{{{basePath}}}";
|
||||
if (!baseUrl.endsWith("/"))
|
||||
@@ -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) {
|
||||
@@ -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 RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
|
||||
}
|
||||
okBuilder.addInterceptor(authorization);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -114,7 +122,6 @@ public class ApiClient {
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
json = new JSON();
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io:80/v2";
|
||||
if (!baseUrl.endsWith("/"))
|
||||
@@ -128,10 +135,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) {
|
||||
@@ -303,7 +311,11 @@ public class ApiClient {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
if(okBuilder == null){
|
||||
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
|
||||
}
|
||||
okBuilder.addInterceptor(authorization);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -115,7 +123,6 @@ public class ApiClient {
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
json = new JSON();
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io:80/v2";
|
||||
if (!baseUrl.endsWith("/"))
|
||||
@@ -130,10 +137,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) {
|
||||
@@ -305,7 +313,11 @@ public class ApiClient {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
if(okBuilder == null){
|
||||
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
|
||||
}
|
||||
okBuilder.addInterceptor(authorization);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -115,7 +123,6 @@ public class ApiClient {
|
||||
|
||||
public void createDefaultAdapter() {
|
||||
json = new JSON();
|
||||
okBuilder = new OkHttpClient.Builder();
|
||||
|
||||
String baseUrl = "http://petstore.swagger.io:80/v2";
|
||||
if (!baseUrl.endsWith("/"))
|
||||
@@ -130,10 +137,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) {
|
||||
@@ -305,7 +313,11 @@ public class ApiClient {
|
||||
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
|
||||
}
|
||||
apiAuthorizations.put(authName, authorization);
|
||||
if(okBuilder == null){
|
||||
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
|
||||
}
|
||||
okBuilder.addInterceptor(authorization);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user