diff --git a/CHANGELOG.md b/CHANGELOG.md index 0838da55..17cbbcce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ async with InfluxDBClientAsync(url="http://localhost:8086", token="my-token", or client_session_kwargs={'trust_env': True}) as client: pass ``` +### Features +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 ### Bug Fixes 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] diff --git a/influxdb_client/client/_base.py b/influxdb_client/client/_base.py index a3bc05d4..08e8ec54 100644 --- a/influxdb_client/client/_base.py +++ b/influxdb_client/client/_base.py @@ -101,6 +101,7 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or @classmethod def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs): config = configparser.ConfigParser() + config_name = kwargs.get('config_name', 'influx2') is_json = False try: config.read(config_file) @@ -111,11 +112,11 @@ def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_g is_json = True def _config_value(key: str): - value = str(config[key]) if is_json else config['influx2'][key] + value = str(config[key]) if is_json else config[config_name][key] return value.strip('"') def _has_option(key: str): - return key in config if is_json else config.has_option('influx2', key) + return key in config if is_json else config.has_option(config_name, key) def _has_section(key: str): return key in config if is_json else config.has_section(key) diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index ca8dcb36..6079aac0 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -91,6 +91,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz :param debug: Enable verbose logging of http requests :param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints supports the Gzip compression. + :key config_name: Name of the configuration section of the configuration file :key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy authentication. :key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests diff --git a/influxdb_client/client/influxdb_client_async.py b/influxdb_client/client/influxdb_client_async.py index 76f014f3..a8a2d173 100644 --- a/influxdb_client/client/influxdb_client_async.py +++ b/influxdb_client/client/influxdb_client_async.py @@ -107,6 +107,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz :param debug: Enable verbose logging of http requests :param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints supports the Gzip compression. + :key config_name: Name of the configuration section of the configuration file :key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy authentication. :key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests diff --git a/tests/config2.ini b/tests/config2.ini new file mode 100644 index 00000000..d7ea854a --- /dev/null +++ b/tests/config2.ini @@ -0,0 +1,13 @@ +[test_name] +url=http://localhost:8086 +org=my-org +token=my-token +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} \ No newline at end of file diff --git a/tests/test_InfluxDBClient.py b/tests/test_InfluxDBClient.py index fdd76390..ca37291b 100644 --- a/tests/test_InfluxDBClient.py +++ b/tests/test_InfluxDBClient.py @@ -75,6 +75,12 @@ def test_init_from_ini_file(self): self.assertConfig() + def test_init_from_ini_file_custom_name(self): + self.client = InfluxDBClient.from_config_file( + f'{os.path.dirname(__file__)}/config2.ini', config_name='test_name') + + self.assertConfig() + def test_init_from_toml_file(self): self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.toml')