Let's assume, that I have some client library with HttpUtils class inside it. And it has field API_VERSION
public class HttpUtils {
public static final int API_VERSION = 1;
private static final String JSON_CONTENT_TYPE = "application/json";
private final CookieStore cookieStore = new BasicCookieStore();
// Public HTTP request methods
public <Request extends HttpGetRequest, Response> Response
get(ConnectionConfig config, String endpoint,
Request request, Map<String, String> requestHeaders,
ResponseHandler<Request, Response> handler, int expectedStatusCode) {
return get(config, endpoint, request, Defaults.DEFAULT_HTTP_TIMEOUT, requestHeaders, handler, expectedStatusCode);
}
private static String getEndpointURI(String endpoint) {
return "/" + UrlParameters.API_SERVICE +
"/" + UrlParameters.VERSION_PREFIX + API_VERSION +
"/" + endpoint;
}
And also I have third-party API that I want to call using this client, but the API is updated and now they using API_VERSION = 2 for some new endpoints. How correctly extend from HTTPUtils and have ability to use both versions for my third-party API calls with v1 and v2 versions?
Just create a method that let's you override the version and don't make it final.
public static int API_VERSION = 1;
private static String getEndpointURI(String endpoint, String apiVersion) {
if (apiVersion != null) {
API_VERSION = apiVersion;
}
return "/" + UrlParameters.API_SERVICE +
"/" + UrlParameters.VERSION_PREFIX + API_VERSION +
"/" + endpoint;
}
Something like that anyway.