Skip to content

Commit e3e3ecc

Browse files
bednarrhajek
authored andcommitted
feat: client could be configured via TOML file (#203)
1 parent 90be233 commit e3e3ecc

File tree

5 files changed

+81
-10
lines changed

5 files changed

+81
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.16.0 [unreleased]
22

3+
### Features
4+
1. [#203](https://github.com/influxdata/influxdb-client-python/pull/203): Allow configuring client via TOML file.
5+
36
### Documentation
47
1. [#202](https://github.com/influxdata/influxdb-client-python/pull/202): Added an example how to use RxPY and sync batching
58

README.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ _______
379379
Via Configuration file
380380
______________________
381381

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

384384
.. code-block:: python
385385
@@ -398,6 +398,8 @@ In a ini configuration file you are able to specify default tags by ``tags`` seg
398398
customer = California Miner
399399
data_center = ${env.data_center}
400400
401+
You could also use a `TOML <https://toml.io/en/>`_ format for the configuration file.
402+
401403
Via Environment Properties
402404
__________________________
403405
You are able to specify default tags by environment properties with prefix ``INFLUXDB_V2_TAG_``.

influxdb_client/client/influxdb_client.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -84,44 +84,79 @@ def __exit__(self, exc_type, exc_value, traceback):
8484
@classmethod
8585
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
8686
"""
87-
Configure client via '*.ini' file in segment 'influx2'.
87+
Configure client via configuration file. The configuration has to be under 'influx' section.
8888
89-
Supported options:
89+
The supported formats:
90+
- https://docs.python.org/3/library/configparser.html
91+
- https://toml.io/en/
92+
93+
Configuration options:
9094
- url
9195
- org
9296
- token
9397
- timeout,
9498
- verify_ssl
9599
- ssl_ca_cert
100+
101+
config.ini example::
102+
103+
[influx2]
104+
url=http://localhost:8086
105+
org=my-org
106+
token=my-token
107+
timeout=6000
108+
109+
[tags]
110+
id = 132-987-655
111+
customer = California Miner
112+
data_center = ${env.data_center}
113+
114+
config.toml example::
115+
116+
[influx2]
117+
url = "http://localhost:8086"
118+
token = "my-token"
119+
org = "my-org"
120+
timeout = 6000
121+
122+
[tags]
123+
id = "132-987-655"
124+
customer = "California Miner"
125+
data_center = "${env.data_center}"
126+
96127
"""
97128
config = configparser.ConfigParser()
98129
config.read(config_file)
99130

100-
url = config['influx2']['url']
101-
token = config['influx2']['token']
131+
def config_value(key: str):
132+
return config['influx2'][key].strip('"')
133+
134+
url = config_value('url')
135+
token = config_value('token')
102136

103137
timeout = None
104138

105139
if config.has_option('influx2', 'timeout'):
106-
timeout = config['influx2']['timeout']
140+
timeout = config_value('timeout')
107141

108142
org = None
109143

110144
if config.has_option('influx2', 'org'):
111-
org = config['influx2']['org']
145+
org = config_value('org')
112146

113147
verify_ssl = True
114148
if config.has_option('influx2', 'verify_ssl'):
115-
verify_ssl = config['influx2']['verify_ssl']
149+
verify_ssl = config_value('verify_ssl')
116150

117151
ssl_ca_cert = None
118152
if config.has_option('influx2', 'ssl_ca_cert'):
119-
ssl_ca_cert = config['influx2']['ssl_ca_cert']
153+
ssl_ca_cert = config_value('ssl_ca_cert')
120154

121155
default_tags = None
122156

123157
if config.has_section('tags'):
124-
default_tags = dict(config.items('tags'))
158+
tags = {k: v.strip('"') for k, v in config.items('tags')}
159+
default_tags = dict(tags)
125160

126161
if timeout:
127162
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,

tests/config.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[influx2]
2+
url = "http://localhost:8086"
3+
token = "my-token"
4+
org = "my-org"
5+
active = true
6+
timeout = 6000
7+
8+
[tags]
9+
id = "132-987-655"
10+
customer = "California Miner"
11+
data_center = "${env.data_center}"

tests/test_InfluxDBClient.py

+20
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ def test_certificate_file(self):
4848
self.assertEqual(health.status, "pass")
4949
self.assertEqual(health.name, "influxdb")
5050

51+
def test_init_from_ini_file(self):
52+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')
53+
54+
self.assertConfig()
55+
56+
def test_init_from_toml_file(self):
57+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.toml')
58+
59+
self.assertConfig()
60+
61+
def assertConfig(self):
62+
self.assertEqual("http://localhost:8086", self.client.url)
63+
self.assertEqual("my-org", self.client.org)
64+
self.assertEqual("my-token", self.client.token)
65+
self.assertEqual(6000, self.client.timeout)
66+
self.assertEqual(3, len(self.client.default_tags))
67+
self.assertEqual("132-987-655", self.client.default_tags["id"])
68+
self.assertEqual("California Miner", self.client.default_tags["customer"])
69+
self.assertEqual("${env.data_center}", self.client.default_tags["data_center"])
70+
5171
def test_init_from_file_ssl_default(self):
5272
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')
5373

0 commit comments

Comments
 (0)