forked from loafle/openapi-generator-original
More flexible subclassing of ApiClient possible by s/private/protected/g (#6159)
* More flexible subclassing of ApiClient possible by s/private/protected/g I found myself in a situation where I needed to change the configured `Feature`s, but because of all the private variables I was not able to do so. Perhaps this is a bit too broad of a stroke, but I changed all fields and methods to `protected` instead of `private`. In this way, future extensibility should be improved. Also, to solve my particular problem, I added a new empty method called `performAdditionalClientConfiguration` which will allow subclasses to add specific features, or do anything else possible with a `ClientConfig`. * Updated samples
This commit is contained in:
@@ -56,21 +56,21 @@ import {{invokerPackage}}.auth.OAuth;
|
|||||||
|
|
||||||
{{>generatedAnnotation}}
|
{{>generatedAnnotation}}
|
||||||
public class ApiClient {
|
public class ApiClient {
|
||||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||||
private String basePath = "{{{basePath}}}";
|
protected String basePath = "{{{basePath}}}";
|
||||||
private boolean debugging = false;
|
protected boolean debugging = false;
|
||||||
private int connectionTimeout = 0;
|
protected int connectionTimeout = 0;
|
||||||
|
|
||||||
private Client httpClient;
|
protected Client httpClient;
|
||||||
private JSON json;
|
protected JSON json;
|
||||||
private String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
private Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
|
||||||
private int statusCode;
|
protected int statusCode;
|
||||||
private Map<String, List<String>> responseHeaders;
|
protected Map<String, List<String>> responseHeaders;
|
||||||
|
|
||||||
private DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
json = new JSON();
|
json = new JSON();
|
||||||
@@ -740,7 +740,7 @@ public class ApiClient {
|
|||||||
* @param debugging Debug setting
|
* @param debugging Debug setting
|
||||||
* @return Client
|
* @return Client
|
||||||
*/
|
*/
|
||||||
private Client buildHttpClient(boolean debugging) {
|
protected Client buildHttpClient(boolean debugging) {
|
||||||
final ClientConfig clientConfig = new ClientConfig();
|
final ClientConfig clientConfig = new ClientConfig();
|
||||||
clientConfig.register(MultiPartFeature.class);
|
clientConfig.register(MultiPartFeature.class);
|
||||||
clientConfig.register(json);
|
clientConfig.register(json);
|
||||||
@@ -751,10 +751,15 @@ public class ApiClient {
|
|||||||
// Set logger to ALL
|
// Set logger to ALL
|
||||||
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
||||||
}
|
}
|
||||||
|
performAdditionalClientConfiguration(clientConfig);
|
||||||
return ClientBuilder.newClient(clientConfig);
|
return ClientBuilder.newClient(clientConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, List<String>> buildResponseHeaders(Response response) {
|
protected void performAdditionalClientConfiguration(ClientConfig clientConfig) {
|
||||||
|
// No-op extension point
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, List<String>> buildResponseHeaders(Response response) {
|
||||||
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
||||||
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
||||||
List<Object> values = entry.getValue();
|
List<Object> values = entry.getValue();
|
||||||
@@ -772,7 +777,7 @@ public class ApiClient {
|
|||||||
*
|
*
|
||||||
* @param authNames The authentications to apply
|
* @param authNames The authentications to apply
|
||||||
*/
|
*/
|
||||||
private void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
protected void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
||||||
for (String authName : authNames) {
|
for (String authName : authNames) {
|
||||||
Authentication auth = authentications.get(authName);
|
Authentication auth = authentications.get(authName);
|
||||||
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
||||||
|
|||||||
@@ -50,21 +50,21 @@ import io.swagger.client.auth.OAuth;
|
|||||||
|
|
||||||
|
|
||||||
public class ApiClient {
|
public class ApiClient {
|
||||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||||
private String basePath = "http://petstore.swagger.io:80/v2";
|
protected String basePath = "http://petstore.swagger.io:80/v2";
|
||||||
private boolean debugging = false;
|
protected boolean debugging = false;
|
||||||
private int connectionTimeout = 0;
|
protected int connectionTimeout = 0;
|
||||||
|
|
||||||
private Client httpClient;
|
protected Client httpClient;
|
||||||
private JSON json;
|
protected JSON json;
|
||||||
private String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
private Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
|
||||||
private int statusCode;
|
protected int statusCode;
|
||||||
private Map<String, List<String>> responseHeaders;
|
protected Map<String, List<String>> responseHeaders;
|
||||||
|
|
||||||
private DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
json = new JSON();
|
json = new JSON();
|
||||||
@@ -729,7 +729,7 @@ public class ApiClient {
|
|||||||
* @param debugging Debug setting
|
* @param debugging Debug setting
|
||||||
* @return Client
|
* @return Client
|
||||||
*/
|
*/
|
||||||
private Client buildHttpClient(boolean debugging) {
|
protected Client buildHttpClient(boolean debugging) {
|
||||||
final ClientConfig clientConfig = new ClientConfig();
|
final ClientConfig clientConfig = new ClientConfig();
|
||||||
clientConfig.register(MultiPartFeature.class);
|
clientConfig.register(MultiPartFeature.class);
|
||||||
clientConfig.register(json);
|
clientConfig.register(json);
|
||||||
@@ -740,10 +740,15 @@ public class ApiClient {
|
|||||||
// Set logger to ALL
|
// Set logger to ALL
|
||||||
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
||||||
}
|
}
|
||||||
|
performAdditionalClientConfiguration(clientConfig);
|
||||||
return ClientBuilder.newClient(clientConfig);
|
return ClientBuilder.newClient(clientConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, List<String>> buildResponseHeaders(Response response) {
|
protected void performAdditionalClientConfiguration(ClientConfig clientConfig) {
|
||||||
|
// No-op extension point
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, List<String>> buildResponseHeaders(Response response) {
|
||||||
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
||||||
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
||||||
List<Object> values = entry.getValue();
|
List<Object> values = entry.getValue();
|
||||||
@@ -761,7 +766,7 @@ public class ApiClient {
|
|||||||
*
|
*
|
||||||
* @param authNames The authentications to apply
|
* @param authNames The authentications to apply
|
||||||
*/
|
*/
|
||||||
private void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
protected void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
||||||
for (String authName : authNames) {
|
for (String authName : authNames) {
|
||||||
Authentication auth = authentications.get(authName);
|
Authentication auth = authentications.get(authName);
|
||||||
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
||||||
|
|||||||
@@ -51,21 +51,21 @@ import io.swagger.client.auth.OAuth;
|
|||||||
|
|
||||||
|
|
||||||
public class ApiClient {
|
public class ApiClient {
|
||||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||||
private String basePath = "http://petstore.swagger.io:80/v2";
|
protected String basePath = "http://petstore.swagger.io:80/v2";
|
||||||
private boolean debugging = false;
|
protected boolean debugging = false;
|
||||||
private int connectionTimeout = 0;
|
protected int connectionTimeout = 0;
|
||||||
|
|
||||||
private Client httpClient;
|
protected Client httpClient;
|
||||||
private JSON json;
|
protected JSON json;
|
||||||
private String tempFolderPath = null;
|
protected String tempFolderPath = null;
|
||||||
|
|
||||||
private Map<String, Authentication> authentications;
|
protected Map<String, Authentication> authentications;
|
||||||
|
|
||||||
private int statusCode;
|
protected int statusCode;
|
||||||
private Map<String, List<String>> responseHeaders;
|
protected Map<String, List<String>> responseHeaders;
|
||||||
|
|
||||||
private DateFormat dateFormat;
|
protected DateFormat dateFormat;
|
||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
json = new JSON();
|
json = new JSON();
|
||||||
@@ -729,7 +729,7 @@ public class ApiClient {
|
|||||||
* @param debugging Debug setting
|
* @param debugging Debug setting
|
||||||
* @return Client
|
* @return Client
|
||||||
*/
|
*/
|
||||||
private Client buildHttpClient(boolean debugging) {
|
protected Client buildHttpClient(boolean debugging) {
|
||||||
final ClientConfig clientConfig = new ClientConfig();
|
final ClientConfig clientConfig = new ClientConfig();
|
||||||
clientConfig.register(MultiPartFeature.class);
|
clientConfig.register(MultiPartFeature.class);
|
||||||
clientConfig.register(json);
|
clientConfig.register(json);
|
||||||
@@ -740,10 +740,15 @@ public class ApiClient {
|
|||||||
// Set logger to ALL
|
// Set logger to ALL
|
||||||
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
|
||||||
}
|
}
|
||||||
|
performAdditionalClientConfiguration(clientConfig);
|
||||||
return ClientBuilder.newClient(clientConfig);
|
return ClientBuilder.newClient(clientConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, List<String>> buildResponseHeaders(Response response) {
|
protected void performAdditionalClientConfiguration(ClientConfig clientConfig) {
|
||||||
|
// No-op extension point
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, List<String>> buildResponseHeaders(Response response) {
|
||||||
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
||||||
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
|
||||||
List<Object> values = entry.getValue();
|
List<Object> values = entry.getValue();
|
||||||
@@ -761,7 +766,7 @@ public class ApiClient {
|
|||||||
*
|
*
|
||||||
* @param authNames The authentications to apply
|
* @param authNames The authentications to apply
|
||||||
*/
|
*/
|
||||||
private void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
protected void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String, String> headerParams) {
|
||||||
for (String authName : authNames) {
|
for (String authName : authNames) {
|
||||||
Authentication auth = authentications.get(authName);
|
Authentication auth = authentications.get(authName);
|
||||||
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
|
||||||
|
|||||||
Reference in New Issue
Block a user