diff --git a/cellbase-client/src/main/java/org/opencb/cellbase/client/rest/ParentRestClient.java b/cellbase-client/src/main/java/org/opencb/cellbase/client/rest/ParentRestClient.java index 8e915e8df..5e41373e9 100644 --- a/cellbase-client/src/main/java/org/opencb/cellbase/client/rest/ParentRestClient.java +++ b/cellbase-client/src/main/java/org/opencb/cellbase/client/rest/ParentRestClient.java @@ -88,7 +88,8 @@ public class ParentRestClient { protected static final String META = "meta"; protected static final String WEBSERVICES = "webservices"; protected static final String REST = "rest"; - + private String apiKeyParam; + private String serverVersion; @Deprecated ParentRestClient(ClientConfiguration configuration) { @@ -234,6 +235,8 @@ protected CellBaseDataResponse execute(List idList, String resour return new CellBaseDataResponse<>(); } + lazyInit(); + // If the list contain less than REST_CALL_BATCH_SIZE variants then we can make a normal REST call. if (idList.size() <= REST_CALL_BATCH_SIZE) { return fetchData(idList, resource, options, clazz, post); @@ -445,13 +448,6 @@ private CellBaseDataResponse restCall(List hosts, String version, // Add the last URL part, the 'action' or 'resource' callUrl = callUrl.path(resource); - // Attention: parameter 'token' was renamed to 'apiKey' in CelBase v5.7 - String apiKeyParam = null; - if (VersionUtils.isMinVersion("v5.7", configuration.getVersion())) { - apiKeyParam = API_KEY_PARAM; - } else if (VersionUtils.isMinVersion("v5.4", configuration.getVersion())) { - apiKeyParam = TOKEN_PARAM; - } if (queryOptions != null) { for (String s : queryOptions.keySet()) { @@ -463,7 +459,7 @@ private CellBaseDataResponse restCall(List hosts, String version, if (dataRelease != null && StringUtils.isEmpty(queryOptions.getString(DATA_RELEASE_PARAM))) { callUrl = callUrl.queryParam(DATA_RELEASE_PARAM, dataRelease); } - if (apiKeyParam != null && apiKey != null && StringUtils.isEmpty(queryOptions.getString(API_KEY_PARAM))) { + if (apiKeyParam != null && apiKey != null && StringUtils.isEmpty(queryOptions.getString(apiKeyParam))) { callUrl = callUrl.queryParam(apiKeyParam, apiKey); } } else { @@ -490,6 +486,39 @@ private CellBaseDataResponse restCall(List hosts, String version, return parseResult(jsonString, clazz); } + private void lazyInit() throws IOException { + // Lazy init other variables. + if (apiKey != null) { + String serverVersion = getServerVersion(); + + // Attention: parameter 'token' was renamed to 'apiKey' in CelBase v5.7 + if (VersionUtils.isMinVersion("5.7.0", serverVersion)) { + apiKeyParam = API_KEY_PARAM; + } else if (VersionUtils.isMinVersion("5.4.0", serverVersion)) { + apiKeyParam = TOKEN_PARAM; + } // else { throw exception, api key not supported } ??? + } + } + + /** + * Get CellBase server version. + * @return CellBase full version + * @throws IOException on IOException + */ + public String getServerVersion() throws IOException { + if (serverVersion == null) { + initServerVersion(); + } + return serverVersion; + } + + private synchronized void initServerVersion() throws IOException { + if (serverVersion == null) { + ObjectMap about = new MetaClient(species, assembly, dataRelease, null, configuration).about().firstResult(); + serverVersion = about.getString("Version"); + } + } + protected WebTarget getBaseUrl(List hosts, String version) { return client .target(URI.create(hosts.get(0)))