Skip to content

Commit 396218a

Browse files
feat: get server version
1 parent 56b12a8 commit 396218a

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
450450
@Nonnull final QueryOptions options);
451451

452452
/**
453-
* Sends a ping and returns the InfluxDB version.
453+
* Get InfluxDB server version.
454454
*
455-
* @return a non-null string representing the response to the ping request
455+
* @return a string representing the server version. Can be null.
456456
*/
457457
@Nonnull
458-
String ping();
458+
String getServerVersion();
459459

460460
/**
461461
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ public Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
292292

293293
@Nonnull
294294
@Override
295-
public String ping() {
296-
return this.restClient.ping();
295+
public String getServerVersion() {
296+
return this.restClient.getServerVersion();
297297
}
298298

299299
@Override

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void checkServerTrusted(
141141
}
142142

143143
@Nonnull
144-
public String ping() throws InfluxDBApiException {
144+
public String getServerVersion() throws InfluxDBApiException {
145145
HttpRequest request = HttpRequest.newBuilder()
146146
.uri(URI.create(this.baseUrl + "ping"))
147147
.GET()
@@ -151,7 +151,7 @@ public String ping() throws InfluxDBApiException {
151151
String influxdbVersion;
152152
try {
153153
response = client.send(request, HttpResponse.BodyHandlers.ofString());
154-
influxdbVersion = response.headers().firstValue("x-influxdb-version").orElseThrow();
154+
influxdbVersion = extractServerVersion(response);
155155
} catch (Exception e) {
156156
throw new InfluxDBApiException(e);
157157
}
@@ -160,6 +160,19 @@ public String ping() throws InfluxDBApiException {
160160
return influxdbVersion;
161161
}
162162

163+
private String extractServerVersion(HttpResponse<String> response) {
164+
return response.headers()
165+
.firstValue("x-influxdb-version")
166+
.orElseGet(() -> {
167+
try {
168+
JsonNode jsonNode = objectMapper.readTree(response.body());
169+
return jsonNode.get("version").asText();
170+
} catch (JsonProcessingException e) {
171+
throw new InfluxDBApiException(e);
172+
}
173+
});
174+
}
175+
163176
void request(@Nonnull final String path,
164177
@Nonnull final HttpMethod method,
165178
@Nullable final byte[] data,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,14 @@ public void testQueryRowsExceptionCases() throws Exception {
388388
@EnabledIfEnvironmentVariable(named = "TESTING_INFLUXDB_TOKEN", matches = ".*")
389389
@EnabledIfEnvironmentVariable(named = "TESTING_INFLUXDB_DATABASE", matches = ".*")
390390
@Test
391-
public void testPing() throws Exception {
391+
public void testGetServerVersion() throws Exception {
392392
try (InfluxDBClient client = InfluxDBClient.getInstance(
393393
System.getenv("TESTING_INFLUXDB_URL"),
394394
System.getenv("TESTING_INFLUXDB_TOKEN").toCharArray(),
395395
System.getenv("TESTING_INFLUXDB_DATABASE"),
396396
null)) {
397397

398-
Assertions.assertThat(client.ping()).isNotEmpty();
398+
Assertions.assertThat(client.getServerVersion()).isNotEmpty();
399399
}
400400
}
401401

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,28 +478,40 @@ public void errorHttpExceptionThrown() {
478478
}
479479

480480
@Test
481-
public void pingReturnsVersionWhenSuccessful() throws Exception {
481+
public void getServerVersionV2Successful() throws Exception {
482482
String influxDBVersion = "v2.1.0";
483483
mockServer.enqueue(createResponse(200).setHeader("x-influxdb-version", influxDBVersion));
484484

485485
restClient = new RestClient(new ClientConfig.Builder()
486486
.host(baseURL)
487487
.build());
488+
String version = restClient.getServerVersion();
488489

489-
String version = restClient.ping();
490+
Assertions.assertThat(version).isEqualTo(influxDBVersion);
491+
}
492+
493+
@Test
494+
public void getServerVersionV3Successful() throws Exception {
495+
String influxDBVersion = "3.0.0";
496+
mockServer.enqueue(createResponse(200).setBody("{\"version\":\"" + influxDBVersion + "\"}"));
497+
498+
restClient = new RestClient(new ClientConfig.Builder()
499+
.host(baseURL)
500+
.build());
501+
String version = restClient.getServerVersion();
490502

491503
Assertions.assertThat(version).isEqualTo(influxDBVersion);
492504
}
493505

494506
@Test
495-
public void pingThrowsExceptionOnFailure() {
507+
public void getServerVersionThrowsExceptionOnFailure() {
496508
mockServer.enqueue(createResponse(500).setBody("{\"message\":\"internal server error\"}"));
497509

498510
restClient = new RestClient(new ClientConfig.Builder()
499511
.host(baseURL)
500512
.build());
501513

502-
Assertions.assertThatThrownBy(() -> restClient.ping())
514+
Assertions.assertThatThrownBy(() -> restClient.getServerVersion())
503515
.isInstanceOf(InfluxDBApiException.class);
504516
}
505517
}

0 commit comments

Comments
 (0)