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

feat: client could be configured via TOML file #203

Merged
merged 2 commits into from
Mar 11, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.16.0 [unreleased]

### Features
1. [#203](https://github.com/influxdata/influxdb-client-python/pull/203): Allow configuring client via TOML file.

### Documentation
1. [#202](https://github.com/influxdata/influxdb-client-python/pull/202): Added an example how to use RxPY and sync batching

Expand Down
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ _______
Via Configuration file
______________________

In a ini configuration file you are able to specify default tags by ``tags`` segment.
In a `init <https://docs.python.org/3/library/configparser.html>`_ configuration file you are able to specify default tags by ``tags`` segment.

.. code-block:: python

Expand All @@ -398,6 +398,8 @@ In a ini configuration file you are able to specify default tags by ``tags`` seg
customer = California Miner
data_center = ${env.data_center}

You could also use a `TOML <https://toml.io/en/>`_ format for the configuration file.

Via Environment Properties
__________________________
You are able to specify default tags by environment properties with prefix ``INFLUXDB_V2_TAG_``.
Expand Down
53 changes: 44 additions & 9 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,79 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
"""
Configure client via '*.ini' file in segment 'influx2'.
Configure client via configuration file. The configuration has to be under 'influx' section.

Supported options:
The supported formats:
- https://docs.python.org/3/library/configparser.html
- https://toml.io/en/

Configuration options:
- url
- org
- token
- timeout,
- verify_ssl
- ssl_ca_cert

config.ini example::

[influx2]
url=http://localhost:8086
org=my-org
token=my-token
timeout=6000

[tags]
id = 132-987-655
customer = California Miner
data_center = ${env.data_center}

config.toml example::

[influx2]
url = "http://localhost:8086"
token = "my-token"
org = "my-org"
timeout = 6000

[tags]
id = "132-987-655"
customer = "California Miner"
data_center = "${env.data_center}"

"""
config = configparser.ConfigParser()
config.read(config_file)

url = config['influx2']['url']
token = config['influx2']['token']
def config_value(key: str):
return config['influx2'][key].strip('"')

url = config_value('url')
token = config_value('token')

timeout = None

if config.has_option('influx2', 'timeout'):
timeout = config['influx2']['timeout']
timeout = config_value('timeout')

org = None

if config.has_option('influx2', 'org'):
org = config['influx2']['org']
org = config_value('org')

verify_ssl = True
if config.has_option('influx2', 'verify_ssl'):
verify_ssl = config['influx2']['verify_ssl']
verify_ssl = config_value('verify_ssl')

ssl_ca_cert = None
if config.has_option('influx2', 'ssl_ca_cert'):
ssl_ca_cert = config['influx2']['ssl_ca_cert']
ssl_ca_cert = config_value('ssl_ca_cert')

default_tags = None

if config.has_section('tags'):
default_tags = dict(config.items('tags'))
tags = {k: v.strip('"') for k, v in config.items('tags')}
default_tags = dict(tags)

if timeout:
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
Expand Down
11 changes: 11 additions & 0 deletions tests/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[influx2]
url = "http://localhost:8086"
token = "my-token"
org = "my-org"
active = true
timeout = 6000

[tags]
id = "132-987-655"
customer = "California Miner"
data_center = "${env.data_center}"
20 changes: 20 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ def test_certificate_file(self):
self.assertEqual(health.status, "pass")
self.assertEqual(health.name, "influxdb")

def test_init_from_ini_file(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')

self.assertConfig()

def test_init_from_toml_file(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.toml')

self.assertConfig()

def assertConfig(self):
self.assertEqual("http://localhost:8086", self.client.url)
self.assertEqual("my-org", self.client.org)
self.assertEqual("my-token", self.client.token)
self.assertEqual(6000, self.client.timeout)
self.assertEqual(3, len(self.client.default_tags))
self.assertEqual("132-987-655", self.client.default_tags["id"])
self.assertEqual("California Miner", self.client.default_tags["customer"])
self.assertEqual("${env.data_center}", self.client.default_tags["data_center"])

def test_init_from_file_ssl_default(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')

Expand Down