Skip to content

Commit f810ae7

Browse files
feat: get server version
1 parent 168e731 commit f810ae7

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ public void checkServerTrusted(
142142
}
143143

144144
public String getServerVersion() throws InfluxDBApiException {
145-
HttpResponse<String> response;
146145
String influxdbVersion;
146+
HttpResponse<String> response = request("ping", HttpMethod.GET, null, null, null);
147147
try {
148-
response = request("ping", HttpMethod.GET, null, null, null);
149148
influxdbVersion = extractServerVersion(response);
149+
} catch (JsonProcessingException e) {
150+
influxdbVersion = null;
150151
} catch (Exception e) {
151152
throw new InfluxDBApiException(e);
152153
}
@@ -164,10 +165,10 @@ private String extractServerVersion(final HttpResponse<String> response) throws
164165
}
165166

166167
HttpResponse<String> request(@Nonnull final String path,
167-
@Nonnull final HttpMethod method,
168-
@Nullable final byte[] data,
169-
@Nullable final Map<String, String> queryParams,
170-
@Nullable final Map<String, String> headers) {
168+
@Nonnull final HttpMethod method,
169+
@Nullable final byte[] data,
170+
@Nullable final Map<String, String> queryParams,
171+
@Nullable final Map<String, String> headers) {
171172

172173
QueryStringEncoder uriEncoder = new QueryStringEncoder(String.format("%s%s", baseUrl, path));
173174
if (queryParams != null) {

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

Lines changed: 25 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;
@@ -502,4 +503,28 @@ public void getServerVersionV3Successful() throws Exception {
502503

503504
Assertions.assertThat(version).isEqualTo(influxDBVersion);
504505
}
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+
}
505530
}

0 commit comments

Comments
 (0)