Skip to content

Commit 4f70184

Browse files
feat: support env
1 parent 4317ade commit 4f70184

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

influxdb_client_3/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import urllib.parse
23
import pyarrow as pa
34
import importlib.util
@@ -47,6 +48,32 @@ def file_parser_options(**kwargs):
4748
return kwargs
4849

4950

51+
def from_env(**kwargs):
52+
"""
53+
Create an instance of `InfluxDBClient3` using environment variables for configuration.
54+
55+
This function retrieves the following environment variables:
56+
- `INFLUX_HOST`: The hostname or IP address of the InfluxDB server.
57+
- `INFLUX_TOKEN`: The authentication token used for accessing the server.
58+
- `INFLUX_DATABASE`: The default database for the client operations.
59+
- `INFLUX_ORG`: The organization associated with InfluxDB operations.
60+
61+
If any of these variables are not set, their respective parameters will
62+
default to `None`.
63+
64+
:param kwargs: Additional keyword arguments that will be passed to the
65+
`InfluxDBClient3` constructor for customization.
66+
:return: An initialized `InfluxDBClient3` instance.
67+
"""
68+
69+
host = os.getenv("INFLUX_HOST")
70+
token = os.getenv("INFLUX_TOKEN")
71+
database = os.getenv("INFLUX_DATABASE")
72+
org = os.getenv("INFLUX_ORG")
73+
74+
return InfluxDBClient3(host=host, token=token, database=database, org=org, **kwargs)
75+
76+
5077
def _deep_merge(target, source):
5178
"""
5279
Performs a deep merge of dictionaries or lists,

tests/test_influxdb_client_3.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from unittest.mock import patch
33

4-
from influxdb_client_3 import InfluxDBClient3
4+
from influxdb_client_3 import InfluxDBClient3, from_env
55
from tests.util import asyncio_run
66
from tests.util.mocks import ConstantFlightServer, ConstantData
77

@@ -74,6 +74,40 @@ async def test_query_async(self):
7474
assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list
7575
assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list
7676

77+
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
78+
'INFLUX_DATABASE': 'test_db', 'INFLUX_ORG': 'test_org'})
79+
def test_from_env_all_env_vars_set(self):
80+
client = from_env()
81+
self.assertIsInstance(client, InfluxDBClient3)
82+
self.assertEqual(client._client.url, "https://localhost:443")
83+
self.assertEqual(client._database, "test_db")
84+
self.assertEqual(client._org, "test_org")
85+
self.assertEqual(client._token, "test_token")
86+
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+
105+
def test_from_env_with_kargs(self):
106+
client = from_env(
107+
write_client_options = write_client_options(batch_size=10000),
108+
)
109+
self.assertIsInstance(client, InfluxDBClient3)
110+
self.assertEqual(client._write_client_options['batch_size'], 10000)
77111

78112
if __name__ == '__main__':
79113
unittest.main()

tests/test_influxdb_client_3_integration.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import string
55
import time
66
import unittest
7+
from unittest.mock import patch
78

89
import pyarrow
910
import pytest
1011
from pyarrow._flight import FlightError
1112

12-
from influxdb_client_3 import InfluxDBClient3, InfluxDBError, write_client_options, WriteOptions
13+
from influxdb_client_3 import InfluxDBClient3, InfluxDBError, write_client_options, WriteOptions, from_env
1314
from tests.util import asyncio_run, lp_to_py_object
1415

1516

@@ -274,3 +275,20 @@ async def test_verify_query_async(self):
274275
result_list = result.to_pylist()
275276
for item in data:
276277
assert lp_to_py_object(item) in result_list, f"original lp data \"{item}\" should be in result list"
278+
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'})
282+
def test_from_env(self):
283+
c = from_env()
284+
with c:
285+
id_test = time.time_ns()
286+
c.write(f"integration_test_python,type=used value=123.0,id_test={id_test}i")
287+
288+
sql = 'SELECT * FROM integration_test_python where type=$type and id_test=$id_test'
289+
data = c.query(sql, mode="pandas", query_parameters={'type': 'used', 'id_test': id_test})
290+
291+
self.assertIsNotNone(data)
292+
self.assertEqual(1, len(data))
293+
self.assertEqual(id_test, data['id_test'][0])
294+
self.assertEqual(123.0, data['value'][0])

0 commit comments

Comments
 (0)