Skip to content

Commit

Permalink
feat: possibility to precise which config name to load when loading c…
Browse files Browse the repository at this point in the history
…onfig from file (#586)
  • Loading branch information
aaalloc authored Jun 29, 2023
1 parent 77cac49 commit 0be41d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
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` 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]
Expand Down
5 changes: 3 additions & 2 deletions influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions influxdb_client/client/influxdb_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit 0be41d1

Please sign in to comment.