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:
Eric O'Connell
2017-07-24 23:56:34 -07:00
committed by wing328
parent 630b126938
commit 2540db8adf
3 changed files with 57 additions and 42 deletions

View File

@@ -50,21 +50,21 @@ import io.swagger.client.auth.OAuth;
public class ApiClient {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private String basePath = "http://petstore.swagger.io:80/v2";
private boolean debugging = false;
private int connectionTimeout = 0;
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected String basePath = "http://petstore.swagger.io:80/v2";
protected boolean debugging = false;
protected int connectionTimeout = 0;
private Client httpClient;
private JSON json;
private String tempFolderPath = null;
protected Client httpClient;
protected JSON json;
protected String tempFolderPath = null;
private Map<String, Authentication> authentications;
protected Map<String, Authentication> authentications;
private int statusCode;
private Map<String, List<String>> responseHeaders;
protected int statusCode;
protected Map<String, List<String>> responseHeaders;
private DateFormat dateFormat;
protected DateFormat dateFormat;
public ApiClient() {
json = new JSON();
@@ -729,7 +729,7 @@ public class ApiClient {
* @param debugging Debug setting
* @return Client
*/
private Client buildHttpClient(boolean debugging) {
protected Client buildHttpClient(boolean debugging) {
final ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MultiPartFeature.class);
clientConfig.register(json);
@@ -740,10 +740,15 @@ public class ApiClient {
// Set logger to ALL
java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).setLevel(java.util.logging.Level.ALL);
}
performAdditionalClientConfiguration(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>>();
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
List<Object> values = entry.getValue();
@@ -761,7 +766,7 @@ public class ApiClient {
*
* @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) {
Authentication auth = authentications.get(authName);
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);