diff --git a/CHANGELOG.md b/CHANGELOG.md index 26c43d52..2aaa175a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/influxdb_client/client/_base.py b/influxdb_client/client/_base.py index 5cbc7fbf..95aef3db 100644 --- a/influxdb_client/client/_base.py +++ b/influxdb_client/client/_base.py @@ -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: @@ -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") @@ -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 diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index 223c4848..042087da 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -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/ @@ -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 @@ -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: """ diff --git a/influxdb_client/client/influxdb_client_async.py b/influxdb_client/client/influxdb_client_async.py index 717b54ab..cdd8eade 100644 --- a/influxdb_client/client/influxdb_client_async.py +++ b/influxdb_client/client/influxdb_client_async.py @@ -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': """ @@ -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/ @@ -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 @@ -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: """ diff --git a/tests/test_InfluxDBClient.py b/tests/test_InfluxDBClient.py index 5d6943b0..c4b9b2a5 100644 --- a/tests/test_InfluxDBClient.py +++ b/tests/test_InfluxDBClient.py @@ -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 @@ -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') @@ -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 diff --git a/tests/test_InfluxDBClientAsync.py b/tests/test_InfluxDBClientAsync.py index a40aac95..ec339235 100644 --- a/tests/test_InfluxDBClientAsync.py +++ b/tests/test_InfluxDBClientAsync.py @@ -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 @@ -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" @@ -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")