Skip to content

Commit 0be41d1

Browse files
authored
feat: possibility to precise which config name to load when loading config from file (influxdata#586)
1 parent 77cac49 commit 0be41d1

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ async with InfluxDBClientAsync(url="http://localhost:8086", token="my-token", or
1212
client_session_kwargs={'trust_env': True}) as client:
1313
pass
1414
```
15+
### Features
16+
1. [#586](https://github.com/influxdata/influxdb-client-python/pull/586): Add `config_name` key argument for ``from_config_file`` function to allow loading a specific configuration from a config file
1517

1618
### Bug Fixes
1719
1. [#583](https://github.com/influxdata/influxdb-client-python/pull/583): Async HTTP client doesn't always use `HTTP_PROXY`/`HTTPS_PROXY` environment variables. [async/await]

influxdb_client/client/_base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
101101
@classmethod
102102
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
103103
config = configparser.ConfigParser()
104+
config_name = kwargs.get('config_name', 'influx2')
104105
is_json = False
105106
try:
106107
config.read(config_file)
@@ -111,11 +112,11 @@ def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_g
111112
is_json = True
112113

113114
def _config_value(key: str):
114-
value = str(config[key]) if is_json else config['influx2'][key]
115+
value = str(config[key]) if is_json else config[config_name][key]
115116
return value.strip('"')
116117

117118
def _has_option(key: str):
118-
return key in config if is_json else config.has_option('influx2', key)
119+
return key in config if is_json else config.has_option(config_name, key)
119120

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

influxdb_client/client/influxdb_client.py

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
9191
:param debug: Enable verbose logging of http requests
9292
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
9393
supports the Gzip compression.
94+
:key config_name: Name of the configuration section of the configuration file
9495
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
9596
authentication.
9697
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests

influxdb_client/client/influxdb_client_async.py

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
107107
:param debug: Enable verbose logging of http requests
108108
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
109109
supports the Gzip compression.
110+
:key config_name: Name of the configuration section of the configuration file
110111
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
111112
authentication.
112113
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests

tests/config2.ini

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

tests/test_InfluxDBClient.py

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def test_init_from_ini_file(self):
7575

7676
self.assertConfig()
7777

78+
def test_init_from_ini_file_custom_name(self):
79+
self.client = InfluxDBClient.from_config_file(
80+
f'{os.path.dirname(__file__)}/config2.ini', config_name='test_name')
81+
82+
self.assertConfig()
83+
7884
def test_init_from_toml_file(self):
7985
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.toml')
8086

0 commit comments

Comments
 (0)