Skip to content

Commit dfbdb85

Browse files
feat: get influxdb version
1 parent ac2dba9 commit dfbdb85

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

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

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

452+
/**
453+
* Sends a ping and returns the InfluxDB version.
454+
*
455+
* @return a non-null string representing the response to the ping request
456+
*/
457+
@Nonnull
458+
String ping();
459+
452460
/**
453461
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
454462
* common operations such as writing, querying.

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

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

293+
@Nonnull
294+
@Override
295+
public String ping() {
296+
return this.restClient.ping();
297+
}
298+
293299
@Override
294300
public void close() throws Exception {
295301
restClient.close();

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ public void checkServerTrusted(
140140
this.client = builder.build();
141141
}
142142

143+
@Nonnull
144+
public String ping() throws InfluxDBApiException {
145+
HttpRequest request = HttpRequest.newBuilder()
146+
.uri(URI.create(this.baseUrl + "ping"))
147+
.GET()
148+
.build();
149+
150+
HttpResponse<String> response;
151+
String influxdbVersion;
152+
try {
153+
response = client.send(request, HttpResponse.BodyHandlers.ofString());
154+
influxdbVersion = response.headers().firstValue("x-influxdb-version").orElseThrow();
155+
} catch (Exception e) {
156+
throw new InfluxDBApiException(e);
157+
}
158+
handleInfluxDBApiHttpException(response);
159+
160+
return influxdbVersion;
161+
}
162+
143163
void request(@Nonnull final String path,
144164
@Nonnull final HttpMethod method,
145165
@Nullable final byte[] data,
@@ -197,6 +217,10 @@ void request(@Nonnull final String path,
197217
throw new InfluxDBApiException(e);
198218
}
199219

220+
handleInfluxDBApiHttpException(response);
221+
}
222+
223+
private void handleInfluxDBApiHttpException(final HttpResponse<String> response) {
200224
int statusCode = response.statusCode();
201225
if (statusCode < 200 || statusCode >= 300) {
202226
String reason = "";

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,30 @@ public void errorHttpExceptionThrown() {
476476
Assertions.assertThat(he.getMessage())
477477
.isEqualTo("HTTP status code: 503; Message: temporarily offline");
478478
}
479+
480+
@Test
481+
public void pingReturnsVersionWhenSuccessful() throws Exception {
482+
String influxDBVersion = "v2.1.0";
483+
mockServer.enqueue(createResponse(200).setHeader("x-influxdb-version", influxDBVersion));
484+
485+
restClient = new RestClient(new ClientConfig.Builder()
486+
.host(baseURL)
487+
.build());
488+
489+
String version = restClient.ping();
490+
491+
Assertions.assertThat(version).isEqualTo(influxDBVersion);
492+
}
493+
494+
@Test
495+
public void pingThrowsExceptionOnFailure() {
496+
mockServer.enqueue(createResponse(500).setBody("{\"message\":\"internal server error\"}"));
497+
498+
restClient = new RestClient(new ClientConfig.Builder()
499+
.host(baseURL)
500+
.build());
501+
502+
Assertions.assertThatThrownBy(() -> restClient.ping())
503+
.isInstanceOf(InfluxDBApiException.class);
504+
}
479505
}

0 commit comments

Comments
 (0)