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

#34: Added configuration via property file #36

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 1.0-alpha.1 [unreleased]
## 1.0.0.M2 [unreleased]

### Features
1. [#34](https://github.com/bonitoo-io/influxdb-client-java/issues/34): Auto-configure client from configuration file

## 1.0.0.M1

### Features
1. [client-java](https://github.com/bonitoo-io/influxdb-client-java/tree/master/client#influxdb-client-java): The reference Java client that allows query, write and InfluxDB 2.0 management
Expand Down
59 changes: 51 additions & 8 deletions client-kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,66 @@ fun main(args: Array<String>) = runBlocking {

## Advanced Usage

### Client configuration file

A client can be configured via configuration file. The configuration file has to be named as `influx2.properties` and has to be in root of classpath.

The following options are supported:

| Property name | default | description |
| --------------------------|-----------|-------------|
| influx2.url | - | the url to connect to InfluxDB |
| influx2.org | - | default destination organization for writes and queries |
| influx2.bucket | - | default destination bucket for writes |
| influx2.token | - | the token to use for the authorization |
| influx2.logLevel | NONE | rest client verbosity level |
| influx2.readTimeout | 10000 ms | read timeout |
| influx2.writeTimeout | 10000 ms | write timeout |
| influx2.connectTimeout | 10000 ms | socket timeout |

The `influx2.readTimeout`, `influx2.writeTimeout` and `influx2.connectTimeout` supports `ms`, `s` and `m` as unit. Default is milliseconds.


##### Configuration example

```properties
influx2.url=http://localhost:9999
influx2.org=my-org
influx2.bucket=my-bucket
influx2.token=my-token
influx2.logLevel=BODY
influx2.readTimeout=5s
influx2.writeTimeout=10s
influx2.connectTimeout=5s
```

and then:

```kotlin
val influxDBClient = InfluxDBClientKotlinFactory.create();
```

### Client connection string

A client can be constructed using a connection string that can contain the InfluxDBClientOptions parameters encoded into the URL.

```java
```kotlin
val influxDBClient = InfluxDBClientKotlinFactory
.create("http://localhost:8086?readTimeout=5000&connectTimeout=5000&logLevel=BASIC", token)
```
The following options are supported:

| Property name | default | description |
| --------------|-------------|-------------|
| readTimeout | 10000 ms| read timeout |
| writeTimeout | 10000 ms| write timeout |
| connectTimeout | 10000 ms| socket timeout |
| logLevel | NONE | rest client verbosity level |

| Property name | default | description |
| ------------------|-----------|-------------|
| org | - | default destination organization for writes and queries |
| bucket | - | default destination bucket for writes |
| token | - | the token to use for the authorization |
| logLevel | NONE | rest client verbosity level |
| readTimeout | 10000 ms | read timeout |
| writeTimeout | 10000 ms | write timeout |
| connectTimeout | 10000 ms | socket timeout |

The `readTimeout`, `writeTimeout` and `connectTimeout` supports `ms`, `s` and `m` as unit. Default is milliseconds.

### Gzip support
`InfluxDBClientKotlin` does not enable gzip compress for http request body by default. If you want to enable gzip to reduce transfer data's size, you can call:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.influxdata.client.InfluxDBClientOptions
import org.influxdata.client.kotlin.internal.InfluxDBClientKotlinImpl

/**
* The Factory that creates a instance of a Flux client.
* The Factory that creates an instance of a Flux client.
*
* @author Jakub Bednar (bednar@github) (30/10/2018 08:19)
*/
Expand All @@ -35,23 +35,39 @@ class InfluxDBClientKotlinFactory {
companion object {

/**
* Create a instance of the InfluxDB 2.0 reactive client.
* Create an instance of the InfluxDB 2.0 client that is configured via {@code influx2.properties}.
* The {@code influx2.properties} has to be located on classpath.
*
* @param url the url to connect to the InfluxDB
* @return client
* @see InfluxDBClientOptions.Builder.url
*/
fun create(url: String): InfluxDBClientKotlin {
fun create(): InfluxDBClientKotlin {

val options = InfluxDBClientOptions.builder()
.url(url)
.loadProperties()
.build()

return create(options)
}

/**
* Create an instance of the InfluxDB 2.0 client. The url could be a connection string with various configurations.
*
* e.g.: "http://localhost:8086?readTimeout=5000&amp;connectTimeout=5000&amp;logLevel=BASIC
*
* @param connectionString connection string with various configurations.
* @return client
*/
fun create(connectionString: String): InfluxDBClientKotlin {

val options = InfluxDBClientOptions.builder()
.connectionString(connectionString)
.build()

return create(options)
}

/**
* Create a instance of the InfluxDB 2.0 reactive client.
* Create an instance of the InfluxDB 2.0 reactive client.
*
* @param url the url to connect to the InfluxDB
* @param username the username to use in the basic auth
Expand All @@ -72,7 +88,7 @@ class InfluxDBClientKotlinFactory {
}

/**
* Create a instance of the InfluxDB 2.0 reactive client.
* Create an instance of the InfluxDB 2.0 reactive client.
*
* @param url the url to connect to the InfluxDB
* @param token the token to use for the authorization
Expand All @@ -90,7 +106,7 @@ class InfluxDBClientKotlinFactory {
}

/**
* Create a instance of the InfluxDB 2.0 reactive client.
* Create an instance of the InfluxDB 2.0 reactive client.
*
* @param options the connection configuration
* @return client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,52 @@
*/
package org.influxdata.client.kotlin

import okhttp3.OkHttpClient
import org.assertj.core.api.Assertions
import org.assertj.core.api.AssertionsForClassTypes
import org.influxdata.LogLevel
import org.influxdata.client.InfluxDBClientOptions
import org.influxdata.client.internal.AbstractInfluxDBClient
import org.influxdata.test.AbstractTest
import org.junit.jupiter.api.Test
import org.junit.platform.runner.JUnitPlatform
import org.junit.runner.RunWith
import retrofit2.Retrofit

/**
* @author Jakub Bednar (bednar@github) (30/10/2018 08:32)
*/
@RunWith(JUnitPlatform::class)
class InfluxDBClientKotlinFactoryTest {
class InfluxDBClientKotlinFactoryTest: AbstractTest() {

@Test
fun connect()
{
val client = InfluxDBClientKotlinFactory.create("http://localhost:8093")
val client = InfluxDBClientKotlinFactory.create("http://localhost:9999")

AssertionsForClassTypes.assertThat<InfluxDBClientKotlin>(client).isNotNull
}

@Test
@Throws(NoSuchFieldException::class, IllegalAccessException::class)
fun loadFromProperties() {

val influxDBClient = InfluxDBClientKotlinFactory.create()

val options = getDeclaredField<InfluxDBClientOptions>(influxDBClient, "options", AbstractInfluxDBClient::class.java)

Assertions.assertThat(options.url).isEqualTo("http://localhost:9999")
Assertions.assertThat(options.org).isEqualTo("my-org")
Assertions.assertThat(options.bucket).isEqualTo("my-bucket")
Assertions.assertThat(options.token).isEqualTo("my-token".toCharArray())
AssertionsForClassTypes.assertThat<LogLevel>(options.logLevel).isEqualTo(LogLevel.BASIC)
AssertionsForClassTypes.assertThat<LogLevel>(influxDBClient.getLogLevel()).isEqualTo(LogLevel.BASIC)

val retrofit = getDeclaredField<Retrofit>(influxDBClient, "retrofit", AbstractInfluxDBClient::class.java)
val okHttpClient = retrofit.callFactory() as OkHttpClient

Assertions.assertThat(okHttpClient.readTimeoutMillis()).isEqualTo(5000)
Assertions.assertThat(okHttpClient.writeTimeoutMillis()).isEqualTo(60000)
Assertions.assertThat(okHttpClient.connectTimeoutMillis()).isEqualTo(5000)
}
}
30 changes: 30 additions & 0 deletions client-kotlin/src/test/resources/influx2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

influx2.url=http://localhost:9999
influx2.org=my-org
influx2.bucket=my-bucket
influx2.token=my-token
influx2.logLevel=BASIC
influx2.readTimeout=5s
influx2.writeTimeout=1m
influx2.connectTimeout=5s
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.influxdata.client.flux.internal.FluxApiImpl;

/**
* The Factory that creates a instance of a Flux client.
* The Factory that creates an instance of a Flux client.
*
* @author Jakub Bednar (bednar@github) (31/07/2018 13:11)
*/
Expand All @@ -37,7 +37,7 @@ private FluxClientFactory() {
}

/**
* Create a instance of the Flux client.
* Create an instance of the Flux client.
*
* @param connectionString the connectionString to connect to InfluxDB.
* @return client
Expand All @@ -54,7 +54,7 @@ public static FluxClient create(@Nonnull final String connectionString) {
}

/**
* Create a instance of the Flux client.
* Create an instance of the Flux client.
*
* @param options the connection configuration
* @return client
Expand Down
57 changes: 50 additions & 7 deletions client-reactive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,45 @@ public class WriteEvery10Seconds {
```
## Advanced Usage

### Client configuration file

A client can be configured via configuration file. The configuration file has to be named as `influx2.properties` and has to be in root of classpath.

The following options are supported:

| Property name | default | description |
| --------------------------|-----------|-------------|
| influx2.url | - | the url to connect to InfluxDB |
| influx2.org | - | default destination organization for writes and queries |
| influx2.bucket | - | default destination bucket for writes |
| influx2.token | - | the token to use for the authorization |
| influx2.logLevel | NONE | rest client verbosity level |
| influx2.readTimeout | 10000 ms | read timeout |
| influx2.writeTimeout | 10000 ms | write timeout |
| influx2.connectTimeout | 10000 ms | socket timeout |

The `influx2.readTimeout`, `influx2.writeTimeout` and `influx2.connectTimeout` supports `ms`, `s` and `m` as unit. Default is milliseconds.


##### Configuration example

```properties
influx2.url=http://localhost:9999
influx2.org=my-org
influx2.bucket=my-bucket
influx2.token=my-token
influx2.logLevel=BODY
influx2.readTimeout=5s
influx2.writeTimeout=10s
influx2.connectTimeout=5s
```

and then:

```java
InfluxDBClientReactive influxDBClient = InfluxDBClientReactiveFactory.create();
```

### Client connection string

A client can be constructed using a connection string that can contain the InfluxDBClientOptions parameters encoded into the URL.
Expand All @@ -258,13 +297,17 @@ InfluxDBClientReactive influxDBClient = InfluxDBClientReactiveFactory
```
The following options are supported:

| Property name | default | description |
| --------------|-------------|-------------|
| readTimeout | 10000 ms| read timeout |
| writeTimeout | 10000 ms| write timeout |
| connectTimeout | 10000 ms| socket timeout |
| logLevel | NONE | rest client verbosity level |

| Property name | default | description |
| ------------------|-----------|-------------|
| org | - | default destination organization for writes and queries |
| bucket | - | default destination bucket for writes |
| token | - | the token to use for the authorization |
| logLevel | NONE | rest client verbosity level |
| readTimeout | 10000 ms | read timeout |
| writeTimeout | 10000 ms | write timeout |
| connectTimeout | 10000 ms | socket timeout |

The `readTimeout`, `writeTimeout` and `connectTimeout` supports `ms`, `s` and `m` as unit. Default is milliseconds.

### Gzip support
`InfluxDBClientReactive` does not enable gzip compress for http request body by default. If you want to enable gzip to reduce transfer data's size, you can call:
Expand Down
Loading