diff --git a/CHANGELOG.md b/CHANGELOG.md index da1cdc53e..766272daf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features - Support for Basic Authentication [PR #492](https://github.com/influxdata/influxdb-java/pull/492) +- Added possibility to reuse client as a core part of [influxdb-java-reactive](https://github.com/bonitoo-io/influxdb-java-reactive) client [PR #493](https://github.com/influxdata/influxdb-java/pull/493) ## 2.12 [2018-07-31] diff --git a/src/main/java/org/influxdb/impl/InfluxDBImpl.java b/src/main/java/org/influxdb/impl/InfluxDBImpl.java index a2f1019b6..800badb1e 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBImpl.java +++ b/src/main/java/org/influxdb/impl/InfluxDBImpl.java @@ -107,14 +107,37 @@ public class InfluxDBImpl implements InfluxDB { * The InfluxDB user name * @param password * The InfluxDB user password - * @param client + * @param okHttpBuilder * The OkHttp Client Builder * @param responseFormat * The {@code ResponseFormat} to use for response from InfluxDB * server */ - public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client, - final ResponseFormat responseFormat) { + public InfluxDBImpl(final String url, final String username, final String password, + final OkHttpClient.Builder okHttpBuilder, final ResponseFormat responseFormat) { + this(url, username, password, okHttpBuilder, new Retrofit.Builder(), responseFormat); + } + + /** + * Constructs a new {@code InfluxDBImpl}. + * + * @param url + * The InfluxDB server API URL + * @param username + * The InfluxDB user name + * @param password + * The InfluxDB user password + * @param okHttpBuilder + * The OkHttp Client Builder + * @param retrofitBuilder + * The Retrofit Builder + * @param responseFormat + * The {@code ResponseFormat} to use for response from InfluxDB + * server + */ + public InfluxDBImpl(final String url, final String username, final String password, + final OkHttpClient.Builder okHttpBuilder, final Retrofit.Builder retrofitBuilder, + final ResponseFormat responseFormat) { this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat); this.hostAddress = parseHostAddress(url); @@ -122,13 +145,13 @@ public InfluxDBImpl(final String url, final String username, final String passwo setLogLevel(LOG_LEVEL); this.gzipRequestInterceptor = new GzipRequestInterceptor(); - OkHttpClient.Builder clonedBuilder = client.build().newBuilder(); - clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor). + OkHttpClient.Builder clonedOkHttpBuilder = okHttpBuilder.build().newBuilder(); + clonedOkHttpBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor). addInterceptor(new BasicAuthInterceptor(username, password)); Factory converterFactory = null; switch (responseFormat) { case MSGPACK: - clonedBuilder.addInterceptor(chain -> { + clonedOkHttpBuilder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("Accept", APPLICATION_MSGPACK).build(); return chain.proceed(request); }); @@ -146,8 +169,9 @@ public InfluxDBImpl(final String url, final String username, final String passwo break; } - this.retrofit = new Retrofit.Builder().baseUrl(url).client( - clonedBuilder.build()).addConverterFactory(converterFactory).build(); + Retrofit.Builder clonedRetrofitBuilder = retrofitBuilder.baseUrl(url).build().newBuilder(); + this.retrofit = clonedRetrofitBuilder.client(clonedOkHttpBuilder.build()) + .addConverterFactory(converterFactory).build(); this.influxDBService = this.retrofit.create(InfluxDBService.class); } diff --git a/src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java b/src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java index 189f4d6d1..9e30586e8 100644 --- a/src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java +++ b/src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java @@ -107,9 +107,11 @@ public void testAllNonRecoverableExceptions() { InfluxDBException nonRecoverable5 = InfluxDBException.buildExceptionForErrorState(createErrorBody("field type conflict 'abc'")); InfluxDBException nonRecoverable6 = new InfluxDBException.RetryBufferOverrunException(createErrorBody("Retry BufferOverrun Exception")); InfluxDBException nonRecoverable7 = InfluxDBException.buildExceptionForErrorState(createErrorBody("user is not authorized to write to database")); - + InfluxDBException nonRecoverable8 = InfluxDBException.buildExceptionForErrorState(createErrorBody("authorization failed")); + InfluxDBException nonRecoverable9 = InfluxDBException.buildExceptionForErrorState(createErrorBody("username required")); + List exceptions = Arrays.asList(nonRecoverable1, nonRecoverable2, nonRecoverable3, - nonRecoverable4, nonRecoverable5, nonRecoverable6, nonRecoverable7); + nonRecoverable4, nonRecoverable5, nonRecoverable6, nonRecoverable7, nonRecoverable8, nonRecoverable9); int size = exceptions.size(); doAnswer(new TestAnswer() { int i = 0; @@ -224,8 +226,15 @@ protected void check(InvocationOnMock invocation) { Assertions.assertEquals(bp1, captor4Write.getAllValues().get(1)); //bp2 written Assertions.assertEquals(bp2, captor4Write.getAllValues().get(2)); - } + + @Test + void defaultExceptionIsRecoverable() { + InfluxDBException unknownError = InfluxDBException.buildExceptionForErrorState(createErrorBody("unknown error")); + + Assertions.assertTrue(unknownError.isRetryWorth()); + } + private static String createErrorBody(String errorMessage) { return MessageFormat.format("'{' \"error\": \"{0}\" '}'", errorMessage); }