Skip to content

Commit 27ec117

Browse files
Revert "feat: support-basic-env"
This reverts commit cf910c0.
1 parent 79412b9 commit 27ec117

File tree

5 files changed

+9
-203
lines changed

5 files changed

+9
-203
lines changed

influxdb_client_3/__init__.py

Lines changed: 4 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
import importlib.util
2-
import os
31
import urllib.parse
4-
from typing import Any
5-
62
import pyarrow as pa
3+
import importlib.util
74

85
from influxdb_client_3.query.query_api import QueryApi as _QueryApi, QueryApiOptionsBuilder
96
from influxdb_client_3.read_file import UploadFile
107
from influxdb_client_3.write_client import InfluxDBClient as _InfluxDBClient, WriteOptions, Point
118
from influxdb_client_3.write_client.client.exceptions import InfluxDBError
129
from influxdb_client_3.write_client.client.write_api import WriteApi as _WriteApi, SYNCHRONOUS, ASYNCHRONOUS, \
13-
PointSettings, WriteType, DefaultWriteOptions
10+
PointSettings
1411
from influxdb_client_3.write_client.domain.write_precision import WritePrecision
1512

1613
polars = importlib.util.find_spec("polars") is not None
1714

18-
INFLUX_HOST = "INFLUX_HOST"
19-
INFLUX_TOKEN = "INFLUX_TOKEN"
20-
INFLUX_DATABASE = "INFLUX_DATABASE"
21-
INFLUX_ORG = "INFLUX_ORG"
22-
INFLUX_PRECISION = "INFLUX_PRECISION"
23-
INFLUX_AUTH_SCHEME = "INFLUX_AUTH_SCHEME"
24-
2515

2616
def write_client_options(**kwargs):
2717
"""
@@ -93,27 +83,6 @@ def _merge_options(defaults, exclude_keys=None, custom=None):
9383
return _deep_merge(defaults, {key: value for key, value in custom.items() if key not in exclude_keys})
9484

9585

96-
def _parse_precision(precision):
97-
"""
98-
Parses the precision value and ensures it is valid.
99-
100-
This function checks that the given `precision` is one of the allowed
101-
values defined in `WritePrecision`. If the precision is invalid, it
102-
raises a `ValueError`. The function returns the valid precision value
103-
if it passes validation.
104-
105-
:param precision: The precision value to be validated.
106-
Must be one of WritePrecision.NS, WritePrecision.MS,
107-
WritePrecision.S, or WritePrecision.US.
108-
:return: The valid precision value.
109-
:rtype: WritePrecision
110-
:raises ValueError: If the provided precision is not valid.
111-
"""
112-
if precision not in [WritePrecision.NS, WritePrecision.MS, WritePrecision.S, WritePrecision.US]:
113-
raise ValueError(f"Invalid precision value: {precision}")
114-
return precision
115-
116-
11786
class InfluxDBClient3:
11887
def __init__(
11988
self,
@@ -167,23 +136,8 @@ def __init__(
167136
self._org = org if org is not None else "default"
168137
self._database = database
169138
self._token = token
170-
171-
write_type = DefaultWriteOptions.write_type.value
172-
write_precision = DefaultWriteOptions.write_precision.value
173-
if isinstance(write_client_options, dict) and write_client_options.get('write_options') is not None:
174-
write_opts = write_client_options['write_options']
175-
write_type = getattr(write_opts, 'write_type', write_type)
176-
write_precision = getattr(write_opts, 'write_precision', write_precision)
177-
178-
write_options = WriteOptions(
179-
write_type=write_type,
180-
write_precision=write_precision
181-
)
182-
183-
self._write_client_options = {
184-
"write_options": write_options,
185-
**(write_client_options or {})
186-
}
139+
self._write_client_options = write_client_options if write_client_options is not None \
140+
else default_client_options(write_options=SYNCHRONOUS)
187141

188142
# Parse the host input
189143
parsed_url = urllib.parse.urlparse(host)
@@ -224,63 +178,6 @@ def __init__(
224178
flight_client_options=flight_client_options,
225179
proxy=kwargs.get("proxy", None), options=q_opts_builder.build())
226180

227-
@classmethod
228-
def from_env(cls, **kwargs: Any) -> 'InfluxDBClient3':
229-
230-
"""
231-
Creates an instance of InfluxDBClient3 configured by specific environment
232-
variables. This method automatically loads configuration settings,
233-
such as connection details, security parameters, and performance
234-
options, from environment variables and initializes the client
235-
accordingly.
236-
237-
:param cls:
238-
The class used to create the client instance.
239-
:param kwargs:
240-
Additional optional parameters that can be passed to customize the
241-
configuration or override specific settings derived from the
242-
environment variables.
243-
244-
:raises ValueError:
245-
If any required environment variables are missing or have empty
246-
values.
247-
248-
:return:
249-
An initialized instance of the `InfluxDBClient3` class with all the
250-
configuration settings applied.
251-
:rtype:
252-
InfluxDBClient3
253-
"""
254-
255-
required_vars = {
256-
INFLUX_HOST: os.getenv(INFLUX_HOST),
257-
INFLUX_TOKEN: os.getenv(INFLUX_TOKEN),
258-
INFLUX_DATABASE: os.getenv(INFLUX_DATABASE)
259-
}
260-
missing_vars = [var for var, value in required_vars.items() if value is None or value == ""]
261-
if missing_vars:
262-
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
263-
264-
write_options = WriteOptions(write_type=WriteType.synchronous)
265-
266-
precision = os.getenv(INFLUX_PRECISION)
267-
if precision is not None:
268-
write_options.write_precision = _parse_precision(precision)
269-
write_client_option = {'write_options': write_options}
270-
271-
if os.getenv(INFLUX_AUTH_SCHEME) is not None:
272-
kwargs['auth_scheme'] = os.getenv(INFLUX_AUTH_SCHEME)
273-
274-
org = os.getenv(INFLUX_ORG, "default")
275-
return InfluxDBClient3(
276-
host=required_vars[INFLUX_HOST],
277-
token=required_vars[INFLUX_TOKEN],
278-
database=required_vars[INFLUX_DATABASE],
279-
write_client_options=write_client_option,
280-
org=org,
281-
**kwargs
282-
)
283-
284181
def write(self, record=None, database=None, **kwargs):
285182
"""
286183
Write data to InfluxDB.

influxdb_client_3/write_client/client/write_api.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ class WriteType(Enum):
3939
synchronous = 3
4040

4141

42-
class DefaultWriteOptions(Enum):
43-
write_type = WriteType.synchronous
44-
write_precision = WritePrecision.NS
45-
46-
4742
class WriteOptions(object):
4843
"""Write configuration."""
4944

@@ -56,7 +51,6 @@ def __init__(self, write_type: WriteType = WriteType.batching,
5651
max_retry_time=180_000,
5752
exponential_base=2,
5853
max_close_wait=300_000,
59-
write_precision=DEFAULT_WRITE_PRECISION,
6054
write_scheduler=ThreadPoolScheduler(max_workers=1)) -> None:
6155
"""
6256
Create write api configuration.
@@ -72,8 +66,7 @@ def __init__(self, write_type: WriteType = WriteType.batching,
7266
:param max_retry_delay: the maximum delay between each retry attempt in milliseconds
7367
:param max_retry_time: total timeout for all retry attempts in milliseconds, if 0 retry is disabled
7468
:param exponential_base: base for the exponential retry delay
75-
:param max_close_wait: the maximum time to wait for writes to be flushed if close() is called
76-
:param write_precision: the time precision for the data written to InfluxDB.
69+
:parama max_close_wait: the maximum time to wait for writes to be flushed if close() is called
7770
:param write_scheduler:
7871
"""
7972
self.write_type = write_type
@@ -87,7 +80,6 @@ def __init__(self, write_type: WriteType = WriteType.batching,
8780
self.exponential_base = exponential_base
8881
self.write_scheduler = write_scheduler
8982
self.max_close_wait = max_close_wait
90-
self.write_precision = write_precision
9183

9284
def to_retry_strategy(self, **kwargs):
9385
"""
@@ -298,7 +290,7 @@ def write(self, bucket: str, org: str = None,
298290
str, Iterable['str'], Point, Iterable['Point'], dict, Iterable['dict'], bytes, Iterable['bytes'],
299291
Observable, NamedTuple, Iterable['NamedTuple'], 'dataclass', Iterable['dataclass']
300292
] = None,
301-
write_precision: WritePrecision = None, **kwargs) -> Any:
293+
write_precision: WritePrecision = DEFAULT_WRITE_PRECISION, **kwargs) -> Any:
302294
"""
303295
Write time-series data into InfluxDB.
304296
@@ -369,9 +361,6 @@ def write(self, bucket: str, org: str = None,
369361

370362
self._append_default_tags(record)
371363

372-
if write_precision is None:
373-
write_precision = self._write_options.write_precision
374-
375364
if self._write_options.write_type is WriteType.batching:
376365
return self._write_batching(bucket, org, record,
377366
write_precision, **kwargs)
@@ -454,11 +443,8 @@ def __del__(self):
454443
pass
455444

456445
def _write_batching(self, bucket, org, data,
457-
precision=None,
446+
precision=DEFAULT_WRITE_PRECISION,
458447
**kwargs):
459-
if precision is None:
460-
precision = self._write_options.write_precision
461-
462448
if isinstance(data, bytes):
463449
_key = _BatchItemKey(bucket, org, precision)
464450
self._subject.on_next(_BatchItem(key=_key, data=data))
@@ -468,8 +454,7 @@ def _write_batching(self, bucket, org, data,
468454
precision, **kwargs)
469455

470456
elif isinstance(data, Point):
471-
write_precision = data.write_precision if data.write_precision is not None else precision
472-
self._write_batching(bucket, org, data.to_line_protocol(), write_precision, **kwargs)
457+
self._write_batching(bucket, org, data.to_line_protocol(), data.write_precision, **kwargs)
473458

474459
elif isinstance(data, dict):
475460
self._write_batching(bucket, org, Point.from_dict(data, write_precision=precision, **kwargs),

tests/test_influxdb_client_3.py

Lines changed: 1 addition & 54 deletions
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, WritePrecision, DefaultWriteOptions
4+
from influxdb_client_3 import InfluxDBClient3
55
from tests.util import asyncio_run
66
from tests.util.mocks import ConstantFlightServer, ConstantData
77

@@ -74,59 +74,6 @@ 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-
def test_default_client(self):
78-
expected_precision = DefaultWriteOptions.write_precision.value
79-
expected_write_type = DefaultWriteOptions.write_type.value
80-
81-
def verify_client_write_options(c):
82-
write_options = c._write_client_options.get('write_options')
83-
self.assertEqual(write_options.write_precision, expected_precision)
84-
self.assertEqual(write_options.write_type, expected_write_type)
85-
86-
self.assertEqual(c._write_api._write_options.write_precision, expected_precision)
87-
self.assertEqual(c._write_api._write_options.write_type, expected_write_type)
88-
89-
env_client = InfluxDBClient3.from_env()
90-
verify_client_write_options(env_client)
91-
92-
default_client = InfluxDBClient3()
93-
verify_client_write_options(default_client)
94-
95-
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
96-
'INFLUX_DATABASE': 'test_db', 'INFLUX_ORG': 'test_org',
97-
'INFLUX_PRECISION': WritePrecision.MS})
98-
def test_from_env_all_env_vars_set(self):
99-
client = InfluxDBClient3.from_env()
100-
self.assertIsInstance(client, InfluxDBClient3)
101-
self.assertEqual(client._client.url, "https://localhost:443")
102-
self.assertEqual(client._database, "test_db")
103-
self.assertEqual(client._org, "test_org")
104-
self.assertEqual(client._token, "test_token")
105-
write_options = client._write_client_options.get("write_options")
106-
self.assertEqual(write_options.write_precision, WritePrecision.MS)
107-
client._write_api._point_settings = {}
108-
109-
@patch.dict('os.environ', {'INFLUX_HOST': "", 'INFLUX_TOKEN': "",
110-
'INFLUX_DATABASE': "", 'INFLUX_ORG': ""})
111-
def test_from_env_missing_variables(self):
112-
with self.assertRaises(ValueError) as context:
113-
InfluxDBClient3.from_env()
114-
self.assertIn("Missing required environment variables", str(context.exception))
115-
116-
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
117-
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': WritePrecision.MS})
118-
def test_parse_valid_write_precision(self):
119-
client = InfluxDBClient3.from_env()
120-
self.assertIsInstance(client, InfluxDBClient3)
121-
self.assertEqual(client._write_client_options.get('write_options').write_precision, WritePrecision.MS)
122-
123-
@patch.dict('os.environ', {'INFLUX_HOST': 'localhost', 'INFLUX_TOKEN': 'test_token',
124-
'INFLUX_DATABASE': 'test_db', 'INFLUX_PRECISION': 'invalid_value'})
125-
def test_parse_invalid_write_precision(self):
126-
with self.assertRaises(ValueError) as context:
127-
InfluxDBClient3.from_env()
128-
self.assertIn("Invalid precision value: invalid_value", str(context.exception))
129-
13077

13178
if __name__ == '__main__':
13279
unittest.main()

tests/test_influxdb_client_3_integration.py

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

98
import pyarrow
109
import pytest
@@ -275,23 +274,3 @@ async def test_verify_query_async(self):
275274
result_list = result.to_pylist()
276275
for item in data:
277276
assert lp_to_py_object(item) in result_list, f"original lp data \"{item}\" should be in result list"
278-
279-
def test_from_env(self):
280-
with InfluxDBClient3.from_env() as client:
281-
id_test = time.time_ns()
282-
client.write(f"integration_test_python,type=used value=123.0,id_test={id_test}i")
283-
284-
sql = 'SELECT * FROM integration_test_python where type=$type and id_test=$id_test'
285-
data = client.query(sql, mode="pandas", query_parameters={'type': 'used', 'id_test': id_test})
286-
287-
self.assertIsNotNone(data)
288-
self.assertEqual(1, len(data))
289-
self.assertEqual(id_test, data['id_test'][0])
290-
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 InfluxDBClient3.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)

tests/test_polars.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def setUp(self):
4343
database="my_db",
4444
token="my_token"
4545
)
46-
self.client._write_api._point_settings = PointSettings()
4746

4847
def test_write_polars(self):
4948
import polars as pl
@@ -78,7 +77,6 @@ def test_write_polars_batching(self):
7877
write_options=WriteOptions(batch_size=2)
7978
)
8079
)
81-
self.client._write_api._point_settings = PointSettings()
8280
self.client._write_api._write_options = WriteOptions(batch_size=2)
8381
self.client._write_api._write_service = Mock(spec=WriteService)
8482

0 commit comments

Comments
 (0)