Skip to content

feat: allow to use client's optional configs for initialization from file or environment properties #510

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
Oct 12, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 1.34.0 [unreleased]

### Features
1. [#510](https://github.com/influxdata/influxdb-client-python/pull/510): Allow to use client's optional configs for initialization from file or environment properties

## 1.33.0 [2022-09-29]

Expand Down
8 changes: 4 additions & 4 deletions influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _version(self, response) -> str:
return "unknown"

@classmethod
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
config = configparser.ConfigParser()
is_json = False
try:
Expand Down Expand Up @@ -169,10 +169,10 @@ def _has_section(key: str):
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,
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic),
profilers=profilers, proxy=proxy)
profilers=profilers, proxy=proxy, **kwargs)

@classmethod
def _from_env_properties(cls, debug=None, enable_gzip=False):
def _from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086")
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000")
Expand All @@ -196,7 +196,7 @@ def _from_env_properties(cls, debug=None, enable_gzip=False):
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,
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic),
profilers=profilers)
profilers=profilers, **kwargs)


# noinspection PyMethodMayBeStatic
Expand Down
26 changes: 22 additions & 4 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ 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):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **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.
: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
except batching writes. As a default there is no one retry strategy.

The supported formats:
- https://docs.python.org/3/library/configparser.html
- https://toml.io/en/
Expand Down Expand Up @@ -153,13 +162,22 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz

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

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False):
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
"""
Configure client via environment properties.

: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 str proxy: Set this to configure the http proxy to be used (ex. http://localhost:3128)
: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
except batching writes. As a default there is no one retry strategy.

Supported environment properties:
- INFLUXDB_V2_URL
- INFLUXDB_V2_ORG
Expand All @@ -172,7 +190,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
- INFLUXDB_V2_PROFILERS
- INFLUXDB_V2_TAG
"""
return super(InfluxDBClient, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip)
return super(InfluxDBClient, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip, **kwargs)

def write_api(self, write_options=WriteOptions(), point_settings=PointSettings(), **kwargs) -> WriteApi:
"""
Expand Down
28 changes: 23 additions & 5 deletions influxdb_client/client/influxdb_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, url, token: str = None, org: str = None, debug=None, timeout=

from .._async.api_client import ApiClientAsync
self.api_client = ApiClientAsync(configuration=self.conf, header_name=self.auth_header_name,
header_value=self.auth_header_value, retries=self.retries, **kwargs)
header_value=self.auth_header_value, **kwargs)

async def __aenter__(self) -> 'InfluxDBClientAsync':
"""
Expand All @@ -93,10 +93,19 @@ async def close(self):
self.api_client = None

@classmethod
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **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.
: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
except batching writes. As a default there is no one retry strategy.

The supported formats:
- https://docs.python.org/3/library/configparser.html
- https://toml.io/en/
Expand Down Expand Up @@ -169,13 +178,22 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz

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

@classmethod
def from_env_properties(cls, debug=None, enable_gzip=False):
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
"""
Configure client via environment properties.

: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 str proxy: Set this to configure the http proxy to be used (ex. http://localhost:3128)
: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
except batching writes. As a default there is no one retry strategy.

Supported environment properties:
- INFLUXDB_V2_URL
- INFLUXDB_V2_ORG
Expand All @@ -188,7 +206,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
- INFLUXDB_V2_PROFILERS
- INFLUXDB_V2_TAG
"""
return super(InfluxDBClientAsync, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip)
return super(InfluxDBClientAsync, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip, **kwargs)

async def ping(self) -> bool:
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from urllib3.exceptions import NewConnectionError, HTTPError

from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write.retry import WritesRetry
from influxdb_client.client.write_api import WriteOptions, WriteType, SYNCHRONOUS
from tests.base_test import BaseTest

Expand Down Expand Up @@ -94,6 +95,11 @@ def test_init_from_file_ssl_default(self):

self.assertTrue(self.client.api_client.configuration.verify_ssl)

def test_init_from_file_kwargs(self):
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini', retries=retry)
self.assertEqual(self.client.retries, retry)

def test_init_from_file_ssl(self):
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config-disabled-ssl.ini')

Expand Down Expand Up @@ -141,6 +147,11 @@ def test_init_from_env_connection_pool_maxsize(self):

self.assertEqual(29, self.client.api_client.configuration.connection_pool_maxsize)

def test_init_from_env_kwargs(self):
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
self.client = InfluxDBClient.from_env_properties(retries=retry)
self.assertEqual(self.client.retries, retry)

def _start_http_server(self):
import http.server
import ssl
Expand Down
19 changes: 19 additions & 0 deletions tests/test_InfluxDBClientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
from influxdb_client.client.query_api import QueryOptions
from influxdb_client.client.warnings import MissingPivotFunction
from influxdb_client.client.write.retry import WritesRetry
from tests.base_test import generate_name


Expand Down Expand Up @@ -250,6 +251,15 @@ async def test_init_from_ini_file(self):

await client_from_config.close()

@async_test
async def test_init_from_file_kwargs(self):
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
client_from_config = InfluxDBClientAsync.from_config_file(f'{os.path.dirname(__file__)}/config.ini',
retries=retry)
self.assertEqual(client_from_config.retries, retry)

await client_from_config.close()

@async_test
async def test_init_from_env(self):
os.environ["INFLUXDB_V2_URL"] = "http://localhost:8086"
Expand All @@ -264,6 +274,15 @@ async def test_init_from_env(self):

await client_from_envs.close()

@async_test
async def test_init_from_kwargs(self):
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
client_from_envs = InfluxDBClientAsync.from_env_properties(retries=retry)

self.assertEqual(client_from_envs.retries, retry)

await client_from_envs.close()

def test_initialize_out_side_async_context(self):
with pytest.raises(InfluxDBError) as e:
InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org")
Expand Down