Skip to content

Commit 490fb25

Browse files
authored
Merge pull request #493 from bonitoo-io/reactive-pull-request
Added possibility to reuse client as a core part of Reactive client
2 parents 681e0db + e6c9ad2 commit 490fb25

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Features
1010

1111
- Support for Basic Authentication [PR #492](https://github.com/influxdata/influxdb-java/pull/492)
12+
- 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)
1213

1314
## 2.12 [2018-07-31]
1415

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+32-8
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,51 @@ public class InfluxDBImpl implements InfluxDB {
110110
* The InfluxDB user name
111111
* @param password
112112
* The InfluxDB user password
113-
* @param client
113+
* @param okHttpBuilder
114114
* The OkHttp Client Builder
115115
* @param responseFormat
116116
* The {@code ResponseFormat} to use for response from InfluxDB
117117
* server
118118
*/
119-
public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
120-
final ResponseFormat responseFormat) {
119+
public InfluxDBImpl(final String url, final String username, final String password,
120+
final OkHttpClient.Builder okHttpBuilder, final ResponseFormat responseFormat) {
121+
this(url, username, password, okHttpBuilder, new Retrofit.Builder(), responseFormat);
122+
}
123+
124+
/**
125+
* Constructs a new {@code InfluxDBImpl}.
126+
*
127+
* @param url
128+
* The InfluxDB server API URL
129+
* @param username
130+
* The InfluxDB user name
131+
* @param password
132+
* The InfluxDB user password
133+
* @param okHttpBuilder
134+
* The OkHttp Client Builder
135+
* @param retrofitBuilder
136+
* The Retrofit Builder
137+
* @param responseFormat
138+
* The {@code ResponseFormat} to use for response from InfluxDB
139+
* server
140+
*/
141+
public InfluxDBImpl(final String url, final String username, final String password,
142+
final OkHttpClient.Builder okHttpBuilder, final Retrofit.Builder retrofitBuilder,
143+
final ResponseFormat responseFormat) {
121144
this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
122145
this.hostName = parseHost(url);
123146

124147
this.loggingInterceptor = new HttpLoggingInterceptor();
125148
setLogLevel(LOG_LEVEL);
126149

127150
this.gzipRequestInterceptor = new GzipRequestInterceptor();
128-
OkHttpClient.Builder clonedBuilder = client.build().newBuilder();
129-
clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).
151+
OkHttpClient.Builder clonedOkHttpBuilder = okHttpBuilder.build().newBuilder();
152+
clonedOkHttpBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).
130153
addInterceptor(new BasicAuthInterceptor(username, password));
131154
Factory converterFactory = null;
132155
switch (responseFormat) {
133156
case MSGPACK:
134-
clonedBuilder.addInterceptor(chain -> {
157+
clonedOkHttpBuilder.addInterceptor(chain -> {
135158
Request request = chain.request().newBuilder().addHeader("Accept", APPLICATION_MSGPACK).build();
136159
return chain.proceed(request);
137160
});
@@ -149,8 +172,9 @@ public InfluxDBImpl(final String url, final String username, final String passwo
149172
break;
150173
}
151174

152-
this.retrofit = new Retrofit.Builder().baseUrl(url).client(
153-
clonedBuilder.build()).addConverterFactory(converterFactory).build();
175+
Retrofit.Builder clonedRetrofitBuilder = retrofitBuilder.baseUrl(url).build().newBuilder();
176+
this.retrofit = clonedRetrofitBuilder.client(clonedOkHttpBuilder.build())
177+
.addConverterFactory(converterFactory).build();
154178
this.influxDBService = this.retrofit.create(InfluxDBService.class);
155179

156180
}

src/test/java/org/influxdb/impl/RetryCapableBatchWriterTest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ public void testAllNonRecoverableExceptions() {
107107
InfluxDBException nonRecoverable5 = InfluxDBException.buildExceptionForErrorState(createErrorBody("field type conflict 'abc'"));
108108
InfluxDBException nonRecoverable6 = new InfluxDBException.RetryBufferOverrunException(createErrorBody("Retry BufferOverrun Exception"));
109109
InfluxDBException nonRecoverable7 = InfluxDBException.buildExceptionForErrorState(createErrorBody("user is not authorized to write to database"));
110-
110+
InfluxDBException nonRecoverable8 = InfluxDBException.buildExceptionForErrorState(createErrorBody("authorization failed"));
111+
InfluxDBException nonRecoverable9 = InfluxDBException.buildExceptionForErrorState(createErrorBody("username required"));
112+
111113
List<InfluxDBException> exceptions = Arrays.asList(nonRecoverable1, nonRecoverable2, nonRecoverable3,
112-
nonRecoverable4, nonRecoverable5, nonRecoverable6, nonRecoverable7);
114+
nonRecoverable4, nonRecoverable5, nonRecoverable6, nonRecoverable7, nonRecoverable8, nonRecoverable9);
113115
int size = exceptions.size();
114116
doAnswer(new TestAnswer() {
115117
int i = 0;
@@ -224,8 +226,15 @@ protected void check(InvocationOnMock invocation) {
224226
Assertions.assertEquals(bp1, captor4Write.getAllValues().get(1));
225227
//bp2 written
226228
Assertions.assertEquals(bp2, captor4Write.getAllValues().get(2));
227-
228229
}
230+
231+
@Test
232+
void defaultExceptionIsRecoverable() {
233+
InfluxDBException unknownError = InfluxDBException.buildExceptionForErrorState(createErrorBody("unknown error"));
234+
235+
Assertions.assertTrue(unknownError.isRetryWorth());
236+
}
237+
229238
private static String createErrorBody(String errorMessage) {
230239
return MessageFormat.format("'{' \"error\": \"{0}\" '}'", errorMessage);
231240
}

0 commit comments

Comments
 (0)