|
7 | 7 | import os
|
8 | 8 | from typing import Iterable
|
9 | 9 |
|
| 10 | +from typing_extensions import deprecated |
| 11 | + |
10 | 12 | from influxdb_client_3.write_client.client.write.dataframe_serializer import DataframeSerializer
|
11 | 13 | from influxdb_client_3.write_client.configuration import Configuration
|
12 | 14 | from influxdb_client_3.write_client.rest import _UTF_8_encoding
|
@@ -173,6 +175,7 @@ def _has_section(key: str):
|
173 | 175 | profilers=profilers, proxy=proxy, **kwargs)
|
174 | 176 |
|
175 | 177 | @classmethod
|
| 178 | + @deprecated('Use _from_env() instead.') |
176 | 179 | def _from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
|
177 | 180 | url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086")
|
178 | 181 | token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
|
@@ -203,6 +206,53 @@ def _from_env_properties(cls, debug=None, enable_gzip=False, **kwargs):
|
203 | 206 | connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic),
|
204 | 207 | profilers=profilers, **kwargs)
|
205 | 208 |
|
| 209 | + @classmethod |
| 210 | + def _from_env(cls, debug=None, enable_gzip=False, **kwargs): |
| 211 | + """ |
| 212 | + Creates and configures an instance of the class using environment variable values. The method loads |
| 213 | + configuration values for connecting to an InfluxDB server instance from preset environment variables. |
| 214 | + Options include connection details such as host, token, organization, and optional parameters |
| 215 | + like SSL settings, profiling, and default tags. Non-specified parameters fallback to defaults |
| 216 | + or None, ensuring a straightforward integration with varied InfluxDB setups. |
| 217 | +
|
| 218 | + :param debug: Determines whether debugging mode is enabled. |
| 219 | + :type debug: Optional[bool] |
| 220 | + :param enable_gzip: Indicates whether gzip compression is enabled for requests. |
| 221 | + :type enable_gzip: bool |
| 222 | + :param kwargs: Additional keyword arguments to configure the instance. |
| 223 | + :type kwargs: dict |
| 224 | + :return: Instance of the class configured using the provided environmental settings. |
| 225 | + :rtype: cls |
| 226 | + """ |
| 227 | + url = os.getenv('INFLUX_HOST', "http://localhost:8086") |
| 228 | + token = os.getenv('INFLUX_TOKEN', "my-token") |
| 229 | + org = os.getenv('INFLUX_ORG', "my-org") |
| 230 | + timeout = os.getenv('INFLUX_TIMEOUT', "10000") |
| 231 | + verify_ssl = os.getenv('INFLUX_VERIFY_SSL', "True") |
| 232 | + ssl_ca_cert = os.getenv('INFLUX_SSL_CA_CERT', None) |
| 233 | + cert_file = os.getenv('INFLUX_CERT_FILE', None) |
| 234 | + cert_key_file = os.getenv('INFLUX_CERT_KEY_FILE', None) |
| 235 | + cert_key_password = os.getenv('INFLUX_CERT_KEY_PASSWORD', None) |
| 236 | + connection_pool_maxsize = os.getenv('INFLUX_CONNECTION_POOL_MAXSIZE', None) |
| 237 | + auth_basic = os.getenv('INFLUX_AUTH_BASIC', "False") |
| 238 | + |
| 239 | + prof = os.getenv("INFLUX_PROFILERS", None) |
| 240 | + profilers = None |
| 241 | + if prof is not None: |
| 242 | + profilers = [x.strip() for x in prof.split(',')] |
| 243 | + |
| 244 | + default_tags = dict() |
| 245 | + |
| 246 | + for key, value in os.environ.items(): |
| 247 | + if key.startswith("INFLUX_TAG_"): |
| 248 | + default_tags[key[11:].lower()] = value |
| 249 | + |
| 250 | + return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags, |
| 251 | + enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert, |
| 252 | + cert_file=cert_file, cert_key_file=cert_key_file, cert_key_password=cert_key_password, |
| 253 | + connection_pool_maxsize=_to_int(connection_pool_maxsize), auth_basic=_to_bool(auth_basic), |
| 254 | + profilers=profilers, **kwargs) |
| 255 | + |
206 | 256 |
|
207 | 257 | class _BaseWriteApi(object):
|
208 | 258 | def __init__(self, influxdb_client, point_settings=None):
|
|
0 commit comments