Skip to content

Commit f02716a

Browse files
authored
feat: add possibility to initialize client by json file (#467)
1 parent c218556 commit f02716a

File tree

7 files changed

+103
-25
lines changed

7 files changed

+103
-25
lines changed

CHANGELOG.md

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

3+
### Features
4+
1. [#467](https://github.com/influxdata/influxdb-client-python/pull/467): Add possibility to initialize client by json file
5+
36
### Bug Fixes
47
1. [#462](https://github.com/influxdata/influxdb-client-python/pull/462): Redact the `Authorization` HTTP header from log
58

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ In a `init <https://docs.python.org/3/library/configparser.html>`_ configuration
572572
customer = California Miner
573573
data_center = ${env.data_center}
574574
575-
You can also use a `TOML <https://toml.io/en/>`_ format for the configuration file.
575+
You can also use a `TOML <https://toml.io/en/>`_ or a`JSON <https://www.json.org/json-en.html>`_ format for the configuration file.
576576

577577
Via Environment Properties
578578
__________________________

influxdb_client/client/_base.py

+41-24
Original file line numberDiff line numberDiff line change
@@ -105,50 +105,67 @@ def _version(self, response) -> str:
105105
@classmethod
106106
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
107107
config = configparser.ConfigParser()
108-
config.read(config_file)
108+
is_json = False
109+
try:
110+
config.read(config_file)
111+
except configparser.ParsingError:
112+
with open(config_file) as json_file:
113+
import json
114+
config = json.load(json_file)
115+
is_json = True
109116

110-
def config_value(key: str):
111-
return config['influx2'][key].strip('"')
117+
def _config_value(key: str):
118+
value = str(config[key]) if is_json else config['influx2'][key]
119+
return value.strip('"')
112120

113-
url = config_value('url')
114-
token = config_value('token')
121+
def _has_option(key: str):
122+
return key in config if is_json else config.has_option('influx2', key)
123+
124+
def _has_section(key: str):
125+
return key in config if is_json else config.has_section(key)
126+
127+
url = _config_value('url')
128+
token = _config_value('token')
115129

116130
timeout = None
117-
if config.has_option('influx2', 'timeout'):
118-
timeout = config_value('timeout')
131+
if _has_option('timeout'):
132+
timeout = _config_value('timeout')
119133

120134
org = None
121-
if config.has_option('influx2', 'org'):
122-
org = config_value('org')
135+
if _has_option('org'):
136+
org = _config_value('org')
123137

124138
verify_ssl = True
125-
if config.has_option('influx2', 'verify_ssl'):
126-
verify_ssl = config_value('verify_ssl')
139+
if _has_option('verify_ssl'):
140+
verify_ssl = _config_value('verify_ssl')
127141

128142
ssl_ca_cert = None
129-
if config.has_option('influx2', 'ssl_ca_cert'):
130-
ssl_ca_cert = config_value('ssl_ca_cert')
143+
if _has_option('ssl_ca_cert'):
144+
ssl_ca_cert = _config_value('ssl_ca_cert')
131145

132146
connection_pool_maxsize = None
133-
if config.has_option('influx2', 'connection_pool_maxsize'):
134-
connection_pool_maxsize = config_value('connection_pool_maxsize')
147+
if _has_option('connection_pool_maxsize'):
148+
connection_pool_maxsize = _config_value('connection_pool_maxsize')
135149

136150
auth_basic = False
137-
if config.has_option('influx2', 'auth_basic'):
138-
auth_basic = config_value('auth_basic')
151+
if _has_option('auth_basic'):
152+
auth_basic = _config_value('auth_basic')
139153

140154
default_tags = None
141-
if config.has_section('tags'):
142-
tags = {k: v.strip('"') for k, v in config.items('tags')}
143-
default_tags = dict(tags)
155+
if _has_section('tags'):
156+
if is_json:
157+
default_tags = config['tags']
158+
else:
159+
tags = {k: v.strip('"') for k, v in config.items('tags')}
160+
default_tags = dict(tags)
144161

145162
profilers = None
146-
if config.has_option('influx2', 'profilers'):
147-
profilers = [x.strip() for x in config_value('profilers').split(',')]
163+
if _has_option('profilers'):
164+
profilers = [x.strip() for x in _config_value('profilers').split(',')]
148165

149166
proxy = None
150-
if config.has_option('influx2', 'proxy'):
151-
proxy = config_value('proxy')
167+
if _has_option('proxy'):
168+
proxy = _config_value('proxy')
152169

153170
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
154171
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,

influxdb_client/client/influxdb_client.py

+19
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
8484
The supported formats:
8585
- https://docs.python.org/3/library/configparser.html
8686
- https://toml.io/en/
87+
- https://www.json.org/json-en.html
8788
8889
Configuration options:
8990
- url
@@ -132,6 +133,24 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
132133
customer = "California Miner"
133134
data_center = "${env.data_center}"
134135
136+
config.json example::
137+
138+
{
139+
"url": "http://localhost:8086",
140+
"token": "my-token",
141+
"org": "my-org",
142+
"active": true,
143+
"timeout": 6000,
144+
"connection_pool_maxsize": 55,
145+
"auth_basic": false,
146+
"profilers": "query, operator",
147+
"tags": {
148+
"id": "132-987-655",
149+
"customer": "California Miner",
150+
"data_center": "${env.data_center}"
151+
}
152+
}
153+
135154
"""
136155
return super(InfluxDBClient, cls)._from_config_file(config_file=config_file, debug=debug,
137156
enable_gzip=enable_gzip)

influxdb_client/client/influxdb_client_async.py

+19
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
100100
The supported formats:
101101
- https://docs.python.org/3/library/configparser.html
102102
- https://toml.io/en/
103+
- https://www.json.org/json-en.html
103104
104105
Configuration options:
105106
- url
@@ -148,6 +149,24 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
148149
customer = "California Miner"
149150
data_center = "${env.data_center}"
150151
152+
config.json example::
153+
154+
{
155+
"url": "http://localhost:8086",
156+
"token": "my-token",
157+
"org": "my-org",
158+
"active": true,
159+
"timeout": 6000,
160+
"connection_pool_maxsize": 55,
161+
"auth_basic": false,
162+
"profilers": "query, operator",
163+
"tags": {
164+
"id": "132-987-655",
165+
"customer": "California Miner",
166+
"data_center": "${env.data_center}"
167+
}
168+
}
169+
151170
"""
152171
return super(InfluxDBClientAsync, cls)._from_config_file(config_file=config_file, debug=debug,
153172
enable_gzip=enable_gzip)

tests/config.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"url": "http://localhost:8086",
3+
"token": "my-token",
4+
"org": "my-org",
5+
"active": true,
6+
"timeout": 6000,
7+
"connection_pool_maxsize": 55,
8+
"auth_basic": false,
9+
"profilers": "query, operator",
10+
"tags": {
11+
"id": "132-987-655",
12+
"customer": "California Miner",
13+
"data_center": "${env.data_center}"
14+
}
15+
}

tests/test_InfluxDBClient.py

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def test_init_from_toml_file(self):
6565

6666
self.assertConfig()
6767

68+
def test_init_from_json_file(self):
69+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.json')
70+
71+
self.assertConfig()
72+
6873
def assertConfig(self):
6974
self.assertEqual("http://localhost:8086", self.client.url)
7075
self.assertEqual("my-org", self.client.org)

0 commit comments

Comments
 (0)