|
| 1 | +import importlib.util |
1 | 2 | import os |
2 | 3 | import urllib.parse |
| 4 | + |
3 | 5 | import pyarrow as pa |
4 | | -import importlib.util |
5 | 6 |
|
6 | 7 | from influxdb_client_3.query.query_api import QueryApi as _QueryApi, QueryApiOptionsBuilder |
7 | 8 | from influxdb_client_3.read_file import UploadFile |
@@ -52,24 +53,44 @@ def from_env(**kwargs): |
52 | 53 | """ |
53 | 54 | Create an instance of `InfluxDBClient3` using environment variables for configuration. |
54 | 55 |
|
55 | | - This function retrieves the following environment variables: |
| 56 | + This function retrieves and validates the following required environment variables: |
56 | 57 | - `INFLUX_HOST`: The hostname or IP address of the InfluxDB server. |
57 | 58 | - `INFLUX_TOKEN`: The authentication token used for accessing the server. |
58 | 59 | - `INFLUX_DATABASE`: The default database for the client operations. |
| 60 | + |
| 61 | + And optional environment variable: |
59 | 62 | - `INFLUX_ORG`: The organization associated with InfluxDB operations. |
| 63 | + Defaults to "default" if not set. |
60 | 64 |
|
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. |
63 | 67 |
|
64 | 68 | :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. |
66 | 72 | :return: An initialized `InfluxDBClient3` instance. |
| 73 | + :raises ValueError: If any required environment variables are not set. |
67 | 74 | """ |
| 75 | + |
| 76 | + invalid_env_vars = [] |
68 | 77 | host = os.getenv("INFLUX_HOST") |
| 78 | + if host is None: |
| 79 | + invalid_env_vars.append("INFLUX_HOST") |
| 80 | + |
69 | 81 | token = os.getenv("INFLUX_TOKEN") |
| 82 | + if token is None: |
| 83 | + invalid_env_vars.append("INFLUX_TOKEN") |
| 84 | + |
70 | 85 | database = os.getenv("INFLUX_DATABASE") |
| 86 | + if database is None: |
| 87 | + invalid_env_vars.append("INFLUX_DATABASE") |
| 88 | + |
71 | 89 | org = os.getenv("INFLUX_ORG") |
72 | 90 |
|
| 91 | + if len(invalid_env_vars) > 0: |
| 92 | + raise ValueError(f"The following environment variables are None or empty: {', '.join(invalid_env_vars)}") |
| 93 | + |
73 | 94 | return InfluxDBClient3(host=host, token=token, database=database, org=org, **kwargs) |
74 | 95 |
|
75 | 96 |
|
|
0 commit comments