Skip to content

feat: add possibility to initialize client by json file #467

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

Merged
merged 2 commits into from
Jul 23, 2022
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.31.0 [unreleased]

### Features
1. [#467](https://github.com/influxdata/influxdb-client-python/pull/467): Add possibility to initialize client by json file

### Bug Fixes
1. [#462](https://github.com/influxdata/influxdb-client-python/pull/462): Redact the `Authorization` HTTP header from log

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ In a `init <https://docs.python.org/3/library/configparser.html>`_ configuration
customer = California Miner
data_center = ${env.data_center}

You can also use a `TOML <https://toml.io/en/>`_ format for the configuration file.
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.

Via Environment Properties
__________________________
Expand Down
65 changes: 41 additions & 24 deletions influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,50 +105,67 @@ def _version(self, response) -> str:
@classmethod
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
config = configparser.ConfigParser()
config.read(config_file)
is_json = False
try:
config.read(config_file)
except configparser.ParsingError:
with open(config_file) as json_file:
import json
config = json.load(json_file)
is_json = True

def config_value(key: str):
return config['influx2'][key].strip('"')
def _config_value(key: str):
value = str(config[key]) if is_json else config['influx2'][key]
return value.strip('"')

url = config_value('url')
token = config_value('token')
def _has_option(key: str):
return key in config if is_json else config.has_option('influx2', key)

def _has_section(key: str):
return key in config if is_json else config.has_section(key)

url = _config_value('url')
token = _config_value('token')

timeout = None
if config.has_option('influx2', 'timeout'):
timeout = config_value('timeout')
if _has_option('timeout'):
timeout = _config_value('timeout')

org = None
if config.has_option('influx2', 'org'):
org = config_value('org')
if _has_option('org'):
org = _config_value('org')

verify_ssl = True
if config.has_option('influx2', 'verify_ssl'):
verify_ssl = config_value('verify_ssl')
if _has_option('verify_ssl'):
verify_ssl = _config_value('verify_ssl')

ssl_ca_cert = None
if config.has_option('influx2', 'ssl_ca_cert'):
ssl_ca_cert = config_value('ssl_ca_cert')
if _has_option('ssl_ca_cert'):
ssl_ca_cert = _config_value('ssl_ca_cert')

connection_pool_maxsize = None
if config.has_option('influx2', 'connection_pool_maxsize'):
connection_pool_maxsize = config_value('connection_pool_maxsize')
if _has_option('connection_pool_maxsize'):
connection_pool_maxsize = _config_value('connection_pool_maxsize')

auth_basic = False
if config.has_option('influx2', 'auth_basic'):
auth_basic = config_value('auth_basic')
if _has_option('auth_basic'):
auth_basic = _config_value('auth_basic')

default_tags = None
if config.has_section('tags'):
tags = {k: v.strip('"') for k, v in config.items('tags')}
default_tags = dict(tags)
if _has_section('tags'):
if is_json:
default_tags = config['tags']
else:
tags = {k: v.strip('"') for k, v in config.items('tags')}
default_tags = dict(tags)

profilers = None
if config.has_option('influx2', 'profilers'):
profilers = [x.strip() for x in config_value('profilers').split(',')]
if _has_option('profilers'):
profilers = [x.strip() for x in _config_value('profilers').split(',')]

proxy = None
if config.has_option('influx2', 'proxy'):
proxy = config_value('proxy')
if _has_option('proxy'):
proxy = _config_value('proxy')

return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
Expand Down
19 changes: 19 additions & 0 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
The supported formats:
- https://docs.python.org/3/library/configparser.html
- https://toml.io/en/
- https://www.json.org/json-en.html

Configuration options:
- url
Expand Down Expand Up @@ -132,6 +133,24 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
customer = "California Miner"
data_center = "${env.data_center}"

config.json example::

{
"url": "http://localhost:8086",
"token": "my-token",
"org": "my-org",
"active": true,
"timeout": 6000,
"connection_pool_maxsize": 55,
"auth_basic": false,
"profilers": "query, operator",
"tags": {
"id": "132-987-655",
"customer": "California Miner",
"data_center": "${env.data_center}"
}
}

"""
return super(InfluxDBClient, cls)._from_config_file(config_file=config_file, debug=debug,
enable_gzip=enable_gzip)
Expand Down
19 changes: 19 additions & 0 deletions influxdb_client/client/influxdb_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
The supported formats:
- https://docs.python.org/3/library/configparser.html
- https://toml.io/en/
- https://www.json.org/json-en.html

Configuration options:
- url
Expand Down Expand Up @@ -148,6 +149,24 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
customer = "California Miner"
data_center = "${env.data_center}"

config.json example::

{
"url": "http://localhost:8086",
"token": "my-token",
"org": "my-org",
"active": true,
"timeout": 6000,
"connection_pool_maxsize": 55,
"auth_basic": false,
"profilers": "query, operator",
"tags": {
"id": "132-987-655",
"customer": "California Miner",
"data_center": "${env.data_center}"
}
}

"""
return super(InfluxDBClientAsync, cls)._from_config_file(config_file=config_file, debug=debug,
enable_gzip=enable_gzip)
Expand Down
15 changes: 15 additions & 0 deletions tests/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"url": "http://localhost:8086",
"token": "my-token",
"org": "my-org",
"active": true,
"timeout": 6000,
"connection_pool_maxsize": 55,
"auth_basic": false,
"profilers": "query, operator",
"tags": {
"id": "132-987-655",
"customer": "California Miner",
"data_center": "${env.data_center}"
}
}
5 changes: 5 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def test_init_from_toml_file(self):

self.assertConfig()

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

self.assertConfig()

def assertConfig(self):
self.assertEqual("http://localhost:8086", self.client.url)
self.assertEqual("my-org", self.client.org)
Expand Down