Skip to content

Commit a12fa64

Browse files
feat: get influxdb version (#243)
1 parent ac2dba9 commit a12fa64

File tree

6 files changed

+109
-8
lines changed

6 files changed

+109
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
`"second"` (
1515
in addition to the existing `"ns"`, `"us"`, `"ms"`, `"s"`).
1616
3. [#241](https://github.com/InfluxCommunity/influxdb3-java/pull/241): Some default options will be used from a getter.
17+
4. [#243](https://github.com/InfluxCommunity/influxdb3-java/pull/243): Add function to get InfluxDB version.
1718

1819
### Bug Fixes
1920

src/main/java/com/influxdb/v3/client/InfluxDBClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
449449
@Nonnull final Map<String, Object> parameters,
450450
@Nonnull final QueryOptions options);
451451

452+
/**
453+
* Get InfluxDB server version.
454+
*
455+
* @return a string representing the server version.
456+
* Returns <code>null</code> if the server version can't be determined.
457+
*/
458+
@Nullable
459+
String getServerVersion();
460+
452461
/**
453462
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
454463
* common operations such as writing, querying.

src/main/java/com/influxdb/v3/client/internal/InfluxDBClientImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ public Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
290290
return queryData(query, parameters, options);
291291
}
292292

293+
@Override
294+
public String getServerVersion() {
295+
return this.restClient.getServerVersion();
296+
}
297+
293298
@Override
294299
public void close() throws Exception {
295300
restClient.close();

src/main/java/com/influxdb/v3/client/internal/RestClient.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import java.security.SecureRandom;
3434
import java.security.cert.Certificate;
3535
import java.security.cert.CertificateFactory;
36+
import java.security.cert.X509Certificate;
3637
import java.util.ArrayList;
3738
import java.util.List;
3839
import java.util.Map;
40+
import java.util.Optional;
3941
import java.util.stream.Stream;
4042
import javax.annotation.Nonnull;
4143
import javax.annotation.Nullable;
@@ -63,16 +65,16 @@ final class RestClient implements AutoCloseable {
6365

6466
private static final TrustManager[] TRUST_ALL_CERTS = new TrustManager[]{
6567
new X509TrustManager() {
66-
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
68+
public X509Certificate[] getAcceptedIssuers() {
6769
return null;
6870
}
6971

7072
public void checkClientTrusted(
71-
final java.security.cert.X509Certificate[] certs, final String authType) {
73+
final X509Certificate[] certs, final String authType) {
7274
}
7375

7476
public void checkServerTrusted(
75-
final java.security.cert.X509Certificate[] certs, final String authType) {
77+
final X509Certificate[] certs, final String authType) {
7678
}
7779
}
7880
};
@@ -140,11 +142,27 @@ public void checkServerTrusted(
140142
this.client = builder.build();
141143
}
142144

143-
void request(@Nonnull final String path,
144-
@Nonnull final HttpMethod method,
145-
@Nullable final byte[] data,
146-
@Nullable final Map<String, String> queryParams,
147-
@Nullable final Map<String, String> headers) {
145+
public String getServerVersion() {
146+
String influxdbVersion;
147+
HttpResponse<String> response = request("ping", HttpMethod.GET, null, null, null);
148+
try {
149+
influxdbVersion = response.headers().firstValue("X-Influxdb-Version").orElse(null);
150+
if (influxdbVersion == null) {
151+
JsonNode jsonNode = objectMapper.readTree(response.body());
152+
influxdbVersion = Optional.ofNullable(jsonNode.get("version")).map(JsonNode::asText).orElse(null);
153+
}
154+
} catch (JsonProcessingException e) {
155+
return null;
156+
}
157+
158+
return influxdbVersion;
159+
}
160+
161+
HttpResponse<String> request(@Nonnull final String path,
162+
@Nonnull final HttpMethod method,
163+
@Nullable final byte[] data,
164+
@Nullable final Map<String, String> queryParams,
165+
@Nullable final Map<String, String> headers) {
148166

149167
QueryStringEncoder uriEncoder = new QueryStringEncoder(String.format("%s%s", baseUrl, path));
150168
if (queryParams != null) {
@@ -235,6 +253,8 @@ void request(@Nonnull final String path,
235253
String message = String.format("HTTP status code: %d; Message: %s", statusCode, reason);
236254
throw new InfluxDBApiHttpException(message, response.headers(), response.statusCode());
237255
}
256+
257+
return response;
238258
}
239259

240260
private X509TrustManager getX509TrustManagerFromFile(@Nonnull final String filePath) {

src/test/java/com/influxdb/v3/client/integration/E2ETest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,21 @@ public void testQueryRowsExceptionCases() throws Exception {
384384
}
385385
}
386386

387+
@EnabledIfEnvironmentVariable(named = "TESTING_INFLUXDB_URL", matches = ".*")
388+
@EnabledIfEnvironmentVariable(named = "TESTING_INFLUXDB_TOKEN", matches = ".*")
389+
@EnabledIfEnvironmentVariable(named = "TESTING_INFLUXDB_DATABASE", matches = ".*")
390+
@Test
391+
public void testGetServerVersion() throws Exception {
392+
try (InfluxDBClient client = InfluxDBClient.getInstance(
393+
System.getenv("TESTING_INFLUXDB_URL"),
394+
System.getenv("TESTING_INFLUXDB_TOKEN").toCharArray(),
395+
System.getenv("TESTING_INFLUXDB_DATABASE"),
396+
null)) {
397+
398+
Assertions.assertThat(client.getServerVersion()).isNotEmpty();
399+
}
400+
}
401+
387402
private void assertGetDataSuccess(@Nonnull final InfluxDBClient influxDBClient) {
388403
influxDBClient.writePoint(
389404
Point.measurement("test1")

src/test/java/com/influxdb/v3/client/internal/RestClientTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Optional;
3737

3838
import io.netty.handler.codec.http.HttpMethod;
39+
import okhttp3.mockwebserver.MockResponse;
3940
import okhttp3.mockwebserver.RecordedRequest;
4041
import org.assertj.core.api.Assertions;
4142
import org.junit.jupiter.api.AfterEach;
@@ -476,4 +477,54 @@ public void errorHttpExceptionThrown() {
476477
Assertions.assertThat(he.getMessage())
477478
.isEqualTo("HTTP status code: 503; Message: temporarily offline");
478479
}
480+
481+
@Test
482+
public void getServerVersionV2Successful() throws Exception {
483+
String influxDBVersion = "v2.1.0";
484+
mockServer.enqueue(createResponse(200).setHeader("x-influxdb-version", influxDBVersion));
485+
486+
restClient = new RestClient(new ClientConfig.Builder()
487+
.host(baseURL)
488+
.build());
489+
String version = restClient.getServerVersion();
490+
491+
Assertions.assertThat(version).isEqualTo(influxDBVersion);
492+
}
493+
494+
@Test
495+
public void getServerVersionV3Successful() throws Exception {
496+
String influxDBVersion = "3.0.0";
497+
mockServer.enqueue(createResponse(200).setBody("{\"version\":\"" + influxDBVersion + "\"}"));
498+
499+
restClient = new RestClient(new ClientConfig.Builder()
500+
.host(baseURL)
501+
.build());
502+
String version = restClient.getServerVersion();
503+
504+
Assertions.assertThat(version).isEqualTo(influxDBVersion);
505+
}
506+
507+
@Test
508+
public void getServerVersionError() {
509+
MockResponse mockResponse = new MockResponse();
510+
mockResponse.setBody("not json")
511+
.setHeader("something", "something");
512+
mockServer.enqueue(mockResponse);
513+
514+
restClient = new RestClient(new ClientConfig.Builder()
515+
.host(baseURL)
516+
.build());
517+
String version = restClient.getServerVersion();
518+
Assertions.assertThat(version).isEqualTo(null);
519+
}
520+
521+
@Test
522+
public void getServerVersionErrorNoBody() {
523+
mockServer.enqueue(new MockResponse().setResponseCode(200));
524+
restClient = new RestClient(new ClientConfig.Builder()
525+
.host(baseURL)
526+
.build());
527+
String version = restClient.getServerVersion();
528+
Assertions.assertThat(version).isEqualTo(null);
529+
}
479530
}

0 commit comments

Comments
 (0)