8000 feat: allow to use client's optional configs for initialization from … · finaln1/influxdb-client-python@42432f9 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 42432f9

Browse files
authored
feat: allow to use client's optional configs for initialization from file or environment properties (influxdata#510)
1 parent 295a5d6 commit 42432f9

File tree

6 files changed

+81
-13
lines changed

6 files changed

+81
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 1.34.0 [unreleased]
22

3+
### Features
4+
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
35

46
## 1.33.0 [2022-09-29]
57

influxdb_client/client/_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _version(self, response) -> str:
102102
return "unknown"
103103

104104
@classmethod
105-
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
105+
def _from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
106106
config = configparser.ConfigParser()
107107
is_json = False
108108
try:
@@ -169,10 +169,10 @@ def _has_section(key: str):
169169
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
170170
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
171171
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic),
172-
profilers=profilers, proxy=proxy)
172+
profilers=profilers, proxy=proxy, **kwargs)
173173

174174
@classmethod
175-
def _from_env_properties(cls, debug=None, enable_gzip=False):
175+
def _from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
176176
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086")
177177
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
178178
timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000")
@@ -196,7 +196,7 @@ def _from_env_properties(cls, debug=None, enable_gzip=False):
196196
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
197197
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
198198
connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic),
199-
profilers=profilers)
199+
profilers=profilers, **kwargs)
200200

201201

202202
# noinspection PyMethodMayBeStatic

influxdb_client/client/influxdb_client.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,19 @@ def __exit__(self, exc_type, exc_value, traceback):
7777
self.close()
7878

7979
@classmethod
80-
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
80+
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
8181
"""
8282
Configure client via configuration file. The configuration has to be under 'influx' section.
8383
84+
:param config_file: Path to configuration file
85+
:param debug: Enable verbose logging of http requests
86+
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
87+
supports the Gzip compression.
88+
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
89+
authentication.
90+
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
91+
except batching writes. As a default there is no one retry strategy.
92+
8493
The supported formats:
8594
- https://docs.python.org/3/library/configparser.html
8695
- https://toml.io/en/
@@ -153,13 +162,22 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
153162
154163
"""
155164
return super(InfluxDBClient, cls)._from_config_file(config_file=config_file, debug=debug,
156-
enable_gzip=enable_gzip)
165+
enable_gzip=enable_gzip, **kwargs)
157166

158167
@classmethod
159-
def from_env_properties(cls, debug=None, enable_gzip=False):
168+
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
160169
"""
161170
Configure client via environment properties.
162171
172+
:param debug: Enable verbose logging of http requests
173+
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
174+
supports the Gzip compression.
175+
:key str proxy: Set this to configure the http proxy to be used (ex. http://localhost:3128)
176+
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
177+
authentication.
178+
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
179+
except batching writes. As a default there is no one retry strategy.
180+
163181
Supported environment properties:
164182
- INFLUXDB_V2_URL
165183
- INFLUXDB_V2_ORG
@@ -172,7 +190,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
172190
- INFLUXDB_V2_PROFILERS
173191
- INFLUXDB_V2_TAG
174192
"""
175-
return super(InfluxDBClient, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip)
193+
return super(InfluxDBClient, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip, **kwargs)
176194

177195
def write_api(self, write_options=WriteOptions(), point_settings=PointSettings(), **kwargs) -> WriteApi:
178196
"""

influxdb_client/client/influxdb_client_async.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, url, token: str = None, org: str = None, debug=None, timeout=
7272

7373
from .._async.api_client import ApiClientAsync
7474
self.api_client = ApiClientAsync(configuration=self.conf, header_name=self.auth_header_name,
75-
header_value=self.auth_header_value, retries=self.retries, **kwargs)
75+
header_value=self.auth_header_value, **kwargs)
7676

7777
async def __aenter__(self) -> 'InfluxDBClientAsync':
7878
"""
@@ -93,10 +93,19 @@ async def close(self):
9393
self.api_client = None
9494

9595
@classmethod
96-
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False):
96+
def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False, **kwargs):
9797
"""
9898
Configure client via configuration file. The configuration has to be under 'influx' section.
9999
100+
:param config_file: Path to configuration file
101+
:param debug: Enable verbose logging of http requests
102+
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
103+
supports the Gzip compression.
104+
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
105+
authentication.
106+
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
107+
except batching writes. As a default there is no one retry strategy.
108+
100109
The supported formats:
101110
- https://docs.python.org/3/library/configparser.html
102111
- https://toml.io/en/
@@ -169,13 +178,22 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
169178
170179
"""
171180
return super(InfluxDBClientAsync, cls)._from_config_file(config_file=config_file, debug=debug,
172-
enable_gzip=enable_gzip)
181+
enable_gzip=enable_gzip, **kwargs)
173182

174183
@classmethod
175-
def from_env_properties(cls, debug=None, enable_gzip=False):
184+
def from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
176185
"""
177186
Configure client via environment properties.
178187
188+
:param debug: Enable verbose logging of http requests
189+
:param enable_gzip: Enable Gzip compression for http requests. Currently, only the "Write" and "Query" endpoints
190+
supports the Gzip compression.
191+
:key str proxy: Set this to configure the http proxy to be used (ex. http://localhost:3128)
192+
:key str proxy_headers: A dictionary containing headers that will be sent to the proxy. Could be used for proxy
193+
authentication.
194+
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
195+
except batching writes. As a default there is no one retry strategy.
196+
179197
Supported environment properties:
180198
- INFLUXDB_V2_URL
181199
- INFLUXDB_V2_ORG
@@ -188,7 +206,7 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
188206
- INFLUXDB_V2_PROFILERS
189207
- INFLUXDB_V2_TAG
190208
"""
191-
return super(InfluxDBClientAsync, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip)
209+
return super(InfluxDBClientAsync, cls)._from_env_properties(debug=debug, enable_gzip=enable_gzip, **kwargs)
192210

193211
async def ping(self) -> bool:
194212
"""

tests/test_InfluxDBClient.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from urllib3.exceptions import NewConnectionError, HTTPError
1313

1414
from influxdb_client import InfluxDBClient, Point
15+
from influxdb_client.client.write.retry import WritesRetry
1516
from influxdb_client.client.write_api import WriteOptions, WriteType, SYNCHRONOUS
1617
from tests.base_test import BaseTest
1718

@@ -94,6 +95,11 @@ def test_init_from_file_ssl_default(self):
9495

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

98+
def test_init_from_file_kwargs(self):
99+
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
100+
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini', retries=retry)
101+
self.assertEqual(self.client.retries, retry)
102+
97103
def test_init_from_file_ssl(self):
98104
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config-disabled-ssl.ini')
99105

@@ -141,6 +147,11 @@ def test_init_from_env_connection_pool_maxsize(self):
141147

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

150+
def test_init_from_env_kwargs(self):
151+
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
152+
self.client = InfluxDBClient.from_env_properties(retries=retry)
153+
self.assertEqual(self.client.retries, retry)
154+
144155
def _start_http_server(self):
145156
import http.server
146157
import ssl

tests/test_InfluxDBClientAsync.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
1414
from influxdb_client.client.query_api import QueryOptions
1515
from influxdb_client.client.warnings import MissingPivotFunction
16+
from influxdb_client.client.write.retry import WritesRetry
1617
from tests.base_test import generate_name
1718

1819

@@ -250,6 +251,15 @@ async def test_init_from_ini_file(self):
250251

251252
await client_from_config.close()
252253

254+
@async_test
255+
async def test_init_from_file_kwargs(self):
256+
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
257+
client_from_config = InfluxDBClientAsync.from_config_file(f'{os.path.dirname(__file__)}/config.ini',
258+
retries=retry)
259+
self.assertEqual(client_from_config.retries, retry)
260+
261+
await client_from_config.close()
262+
253263
@async_test
254264
async def test_init_from_env(self):
255265
os.environ["INFLUXDB_V2_URL"] = "http://localhost:8086"
@@ -264,6 +274,15 @@ async def test_init_from_env(self):
264274

265275
await client_from_envs.close()
266276

277+
@async_test
278+
async def test_init_from_kwargs(self):
279+
retry = WritesRetry(total=1, retry_interval=2, exponential_base=3)
280+
client_from_envs = InfluxDBClientAsync.from_env_properties(retries=retry)
281+
282+
self.assertEqual(client_from_envs.retries, retry)
283+
284+
await client_from_envs.close()
285+
267286
def test_initialize_out_side_async_context(self):
268287
with pytest.raises(InfluxDBError) as e:
269288
InfluxDBClientAsync(url="http://localhost:8086", token="my-token", org="my-org")

0 commit comments

Comments
 (0)
0