Skip to content

Commit 16ed462

Browse files
feat: support env
1 parent c11e856 commit 16ed462

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

influxdb_client_3/__init__.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib.util
22
import os
33
import urllib.parse
4+
from typing import Any
45

56
import pyarrow as pa
67

@@ -48,16 +49,19 @@ def file_parser_options(**kwargs):
4849
"""
4950
return kwargs
5051

51-
52-
def from_env(**kwargs):
52+
# Constants for environment variable names
53+
INFLUX_HOST = "INFLUX_HOST"
54+
INFLUX_TOKEN = "INFLUX_TOKEN"
55+
INFLUX_DATABASE = "INFLUX_DATABASE"
56+
INFLUX_ORG = "INFLUX_ORG"
57+
def from_env(**kwargs: Any) -> 'InfluxDBClient3':
5358
"""
5459
Create an instance of `InfluxDBClient3` using environment variables for configuration.
5560
5661
This function retrieves and validates the following required environment variables:
5762
- `INFLUX_HOST`: The hostname or IP address of the InfluxDB server.
5863
- `INFLUX_TOKEN`: The authentication token used for accessing the server.
5964
- `INFLUX_DATABASE`: The default database for the client operations.
60-
6165
And optional environment variable:
6266
- `INFLUX_ORG`: The organization associated with InfluxDB operations.
6367
Defaults to "default" if not set.
@@ -72,26 +76,26 @@ def from_env(**kwargs):
7276
:return: An initialized `InfluxDBClient3` instance.
7377
:raises ValueError: If any required environment variables are not set.
7478
"""
75-
76-
invalid_env_vars = []
77-
host = os.getenv("INFLUX_HOST")
78-
if host is None:
79-
invalid_env_vars.append("INFLUX_HOST")
80-
81-
token = os.getenv("INFLUX_TOKEN")
82-
if token is None:
83-
invalid_env_vars.append("INFLUX_TOKEN")
84-
85-
database = os.getenv("INFLUX_DATABASE")
86-
if database is None:
87-
invalid_env_vars.append("INFLUX_DATABASE")
88-
89-
org = os.getenv("INFLUX_ORG")
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-
94-
return InfluxDBClient3(host=host, token=token, database=database, org=org, **kwargs)
79+
required_vars = {
80+
INFLUX_HOST: os.getenv(INFLUX_HOST),
81+
INFLUX_TOKEN: os.getenv(INFLUX_TOKEN),
82+
INFLUX_DATABASE: os.getenv(INFLUX_DATABASE)
83+
}
84+
85+
missing_vars = [var for var, value in required_vars.items() if value is None or value == ""]
86+
if missing_vars:
87+
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
88+
89+
org = os.getenv(INFLUX_ORG, "default")
90+
91+
return InfluxDBClient3(
92+
host=required_vars[INFLUX_HOST],
93+
token=required_vars[INFLUX_TOKEN],
94+
database=required_vars[INFLUX_DATABASE],
95+
org=org,
96+
**kwargs
97+
)
98+
9599

96100

97101
def _deep_merge(target, source):

tests/test_influxdb_client_3.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,19 @@ def test_from_env_all_env_vars_set(self):
8484
self.assertEqual(client._org, "test_org")
8585
self.assertEqual(client._token, "test_token")
8686

87-
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_DATABASE': 'test_db'})
88-
def test_from_env_partial_env_vars_set(self):
89-
client = from_env()
90-
self.assertIsInstance(client, InfluxDBClient3)
91-
self.assertEqual(client._client.url, "https://localhost:443")
92-
self.assertEqual(client._database, "test_db")
93-
self.assertEqual(client._org, "default")
94-
self.assertIsNone(client._token)
95-
96-
@patch.dict('os.environ', {}, clear=True)
97-
def test_from_env_no_env_vars_set(self):
98-
client = from_env()
99-
self.assertIsInstance(client, InfluxDBClient3)
100-
self.assertIsNotNone(client._client.url)
101-
self.assertIsNone(client._database)
102-
self.assertIsNone(client._token)
103-
self.assertEqual(client._org, "default")
104-
10587
def test_from_env_with_kargs(self):
10688
client = from_env(
10789
write_client_options=write_client_options(batch_size=10000),
10890
)
10991
self.assertIsInstance(client, InfluxDBClient3)
11092
self.assertEqual(client._write_client_options['batch_size'], 10000)
11193

94+
@patch.dict('os.environ', {'INFLUX_HOST': "", 'INFLUX_TOKEN': "",
95+
'INFLUX_DATABASE': "", 'INFLUX_ORG': ""})
96+
def test_from_env_missing_variables(self):
97+
with self.assertRaises(ValueError) as context:
98+
from_env()
99+
self.assertIn("Missing required environment variables", str(context.exception))
112100

113101
if __name__ == '__main__':
114102
unittest.main()

tests/test_influxdb_client_3_integration.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ async def test_verify_query_async(self):
276276
for item in data:
277277
assert lp_to_py_object(item) in result_list, f"original lp data \"{item}\" should be in result list"
278278

279-
@patch.dict('os.environ', {'INFLUX_HOST': 'https://us-east-1-1.aws.cloud2.influxdata.com',
280-
'INFLUX_TOKEN': 'lDAtMRmhnLp5GjWNVBsieufUb66XZAPxvX3etlmi9wmeq7ispWoL06mwnxmY_BtHKoBhG4lR-c7WfrFgUXy15w==',
281-
'INFLUX_DATABASE': 'bucket0'})
282279
def test_from_env(self):
283280
c = from_env()
284281
with c:

0 commit comments

Comments
 (0)