Skip to content

Commit b1b8f9c

Browse files
feat: support env
1 parent ef85c7f commit b1b8f9c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

influxdb_client_3/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def file_parser_options(**kwargs):
5656
INFLUX_DATABASE = "INFLUX_DATABASE"
5757
INFLUX_ORG = "INFLUX_ORG"
5858
INFLUX_PRECISION = "INFLUX_PRECISION"
59+
INFLUX_AUTH_SCHEME = "INFLUX_AUTH_SCHEME"
5960

6061

6162
def from_env(**kwargs: Any) -> 'InfluxDBClient3':
@@ -93,6 +94,9 @@ def from_env(**kwargs: Any) -> 'InfluxDBClient3':
9394

9495
org = os.getenv(INFLUX_ORG, "default")
9596

97+
if os.getenv(INFLUX_AUTH_SCHEME) is not None:
98+
kwargs['auth_scheme'] = os.getenv(INFLUX_AUTH_SCHEME)
99+
96100
write_client_option = None
97101
if os.getenv(INFLUX_PRECISION) is not None:
98102
write_client_option = default_client_options(

tests/test_influxdb_client_3_integration.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import string
55
import time
66
import unittest
7+
from unittest.mock import patch
78

89
import pyarrow
910
import pytest
@@ -276,15 +277,21 @@ async def test_verify_query_async(self):
276277
assert lp_to_py_object(item) in result_list, f"original lp data \"{item}\" should be in result list"
277278

278279
def test_from_env(self):
279-
c = from_env()
280-
with c:
280+
with from_env() as client:
281281
id_test = time.time_ns()
282-
c.write(f"integration_test_python,type=used value=123.0,id_test={id_test}i")
282+
client.write(f"integration_test_python,type=used value=123.0,id_test={id_test}i")
283283

284284
sql = 'SELECT * FROM integration_test_python where type=$type and id_test=$id_test'
285-
data = c.query(sql, mode="pandas", query_parameters={'type': 'used', 'id_test': id_test})
285+
data = client.query(sql, mode="pandas", query_parameters={'type': 'used', 'id_test': id_test})
286286

287287
self.assertIsNotNone(data)
288288
self.assertEqual(1, len(data))
289289
self.assertEqual(id_test, data['id_test'][0])
290290
self.assertEqual(123.0, data['value'][0])
291+
292+
@patch.dict('os.environ', {'INFLUX_AUTH_SCHEME': 'invalid_schema'})
293+
def test_from_env_invalid_auth_schema(self):
294+
with from_env() as client:
295+
with self.assertRaises(InfluxDBError) as err:
296+
client.write("integration_test_python,type=used value=123.0")
297+
self.assertEqual('unauthorized access', err.exception.message)

0 commit comments

Comments
 (0)