Skip to content

Commit c11e856

Browse files
feat: support env
1 parent 3fb187e commit c11e856

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

influxdb_client_3/__init__.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import importlib.util
12
import os
23
import urllib.parse
4+
35
import pyarrow as pa
4-
import importlib.util
56

67
from influxdb_client_3.query.query_api import QueryApi as _QueryApi, QueryApiOptionsBuilder
78
from influxdb_client_3.read_file import UploadFile
@@ -52,24 +53,44 @@ def from_env(**kwargs):
5253
"""
5354
Create an instance of `InfluxDBClient3` using environment variables for configuration.
5455
55-
This function retrieves the following environment variables:
56+
This function retrieves and validates the following required environment variables:
5657
- `INFLUX_HOST`: The hostname or IP address of the InfluxDB server.
5758
- `INFLUX_TOKEN`: The authentication token used for accessing the server.
5859
- `INFLUX_DATABASE`: The default database for the client operations.
60+
61+
And optional environment variable:
5962
- `INFLUX_ORG`: The organization associated with InfluxDB operations.
63+
Defaults to "default" if not set.
6064
61-
If any of these variables are not set, their respective parameters will
62-
default to `None`.
65+
If any of the required environment variables are not set, a ValueError will be
66+
raised with details about the missing variables.
6367
6468
:param kwargs: Additional keyword arguments that will be passed to the
65-
`InfluxDBClient3` constructor for customization.
69+
`InfluxDBClient3` constructor for customization. This allows for
70+
configuring specific client behaviors like write_client_options,
71+
flight_client_options, SSL settings, etc.
6672
:return: An initialized `InfluxDBClient3` instance.
73+
:raises ValueError: If any required environment variables are not set.
6774
"""
75+
76+
invalid_env_vars = []
6877
host = os.getenv("INFLUX_HOST")
78+
if host is None:
79+
invalid_env_vars.append("INFLUX_HOST")
80+
6981
token = os.getenv("INFLUX_TOKEN")
82+
if token is None:
83+
invalid_env_vars.append("INFLUX_TOKEN")
84+
7085
database = os.getenv("INFLUX_DATABASE")
86+
if database is None:
87+
invalid_env_vars.append("INFLUX_DATABASE")
88+
7189
org = os.getenv("INFLUX_ORG")
7290

91+
if len(invalid_env_vars) > 0:
92+
raise ValueError(f"The following environment variables are None or empty: {', '.join(invalid_env_vars)}")
93+
7394
return InfluxDBClient3(host=host, token=token, database=database, org=org, **kwargs)
7495

7596

0 commit comments

Comments
 (0)