Skip to content
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

feat: possibility to precise which config name to load when loading config from file #586

Merged
merged 13 commits into from
Jun 29, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 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]
Expand Down
8 changes: 5 additions & 3 deletions influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
pass

@classmethod
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def _from_config_file(cls, config_file: str = "config.ini", debug=None,
config_name: str = "influx2", enable_gzip=False, **kwargs):

config = configparser.ConfigParser()
is_json = False
try:
Expand All @@ -111,11 +113,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)
Expand Down
7 changes: 5 additions & 2 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ def __exit__(self, exc_type, exc_value, traceback):
self.close()

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False,
config_name: str = "influx2", **kwargs):
"""
Configure client via configuration file. The configuration has to be under 'influx' section.

:param config_file: Path to configuration file
: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.
:param 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
Expand Down Expand Up @@ -173,7 +175,8 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
}

"""
return InfluxDBClient._from_config_file(config_file=config_file, debug=debug, enable_gzip=enable_gzip, **kwargs)
return InfluxDBClient._from_config_file(config_file=config_file, config_name=config_name,
debug=debug, enable_gzip=enable_gzip, **kwargs)

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
Expand Down
8 changes: 5 additions & 3 deletions influxdb_client/client/influxdb_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ async def close(self):
self.api_client = None

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False,
config_name: str = "influx2", **kwargs):
"""
Configure client via configuration file. The configuration has to be under 'influx' section.

:param config_file: Path to configuration file
: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.
:param 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
Expand Down Expand Up @@ -189,8 +191,8 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
}

"""
return InfluxDBClientAsync._from_config_file(config_file=config_file, debug=debug,
enable_gzip=enable_gzip, **kwargs)
return InfluxDBClientAsync._from_config_file(config_file=config_file, config_name=config_name,
debug=debug, enable_gzip=enable_gzip, **kwargs)

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions tests/config2.ini
Original file line number Diff line number Diff line change
@@ -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}
6 changes: 6 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down