Skip to content

Commit 8568097

Browse files
authored
feat: client could be configured via TOML file (#203)
1 parent 4f1e14e commit 8568097

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
@@ -69,44 +69,79 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
6969
@classmethod
7070
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
7171
"""
72-
Configure client via '*.ini' file in segment 'influx2'.
72+
Configure client via configuration file. The configuration has to be under 'influx' section.
7373
74-
Supported options:
74+
The supported formats:
75+
- https://docs.python.org/3/library/configparser.html
76+
- https://toml.io/en/
77+
78+
Configuration options:
7579
- url
7680
- org
7781
- token
7882
- timeout,
7983
- verify_ssl
8084
- ssl_ca_cert
85+
86+
config.ini example::
87+
88+
[influx2]
89+
url=http://localhost:8086
90+
org=my-org
91+
token=my-token
92+
timeout=6000
93+
94+
[tags]
95+
id = 132-987-655
96+
customer = California Miner
97+
data_center = ${env.data_center}
98+
99+
config.toml example::
100+
101+
[influx2]
102+
url = "http://localhost:8086"
103+
token = "my-token"
104+
org = "my-org"
105+
timeout = 6000
106+
107+
[tags]
108+
id = "132-987-655"
109+
customer = "California Miner"
110+
data_center = "${env.data_center}"
111+
81112
"""
82113
config = configparser.ConfigParser()
83114
config.read(config_file)
84115

85-
url = config['influx2']['url']
86-
token = config['influx2']['token']
116+
def config_value(key: str):
117+
return config['influx2'][key].strip('"')
118+
119+
url = config_value('url')
120+
token = config_value('token')
87121

88122
timeout = None
89123

90124
if config.has_option('influx2', 'timeout'):
91-
timeout = config['influx2']['timeout']
125+
timeout = config_value('timeout')
92126

93127
org = None
94128

95129
if config.has_option('influx2', 'org'):
96-
org = config['influx2']['org']
130+
org = config_value('org')
97131

98132
verify_ssl = True
99133
if config.has_option('influx2', 'verify_ssl'):
100-
verify_ssl = config['influx2']['verify_ssl']
134+
verify_ssl = config_value('verify_ssl')
101135

102136
ssl_ca_cert = None
103137
if config.has_option('influx2', 'ssl_ca_cert'):
104-
ssl_ca_cert = config['influx2']['ssl_ca_cert']
138+
ssl_ca_cert = config_value('ssl_ca_cert')
105139

106140
default_tags = None
107141

108142
if config.has_section('tags'):
109-
default_tags = dict(config.items('tags'))
143+
tags = {k: v.strip('"') for k, v in config.items('tags')}
144+
default_tags = dict(tags)
110145

111146
if timeout:
112147
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
@@ -47,6 +47,26 @@ def test_certificate_file(self):
4747
self.assertEqual(health.status, "pass")
4848
self.assertEqual(health.name, "influxdb")
4949

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

0 commit comments

Comments
 (0)