Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to reuse client as a core part of Reactive client #493

Merged
merged 6 commits into from
Sep 6, 2018
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
40 changes: 32 additions & 8 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,51 @@ 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);

this.loggingInterceptor = new HttpLoggingInterceptor();
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);
});
Expand All @@ -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);

}
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<InfluxDBException> 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;
Expand Down Expand Up @@ -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);
}
Expand Down