Skip to content

Commit 3818093

Browse files
committed
fix: resolve write option kwargs before post_write call
1 parent 3c9251c commit 3818093

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

influxdb_client_3/write_client/client/write_api.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ def __init__(self,
320320
# TODO above message has link to Influxdb2 API __NOT__ Influxdb3 API !!! - illustrates different API
321321
warnings.warn(message, DeprecationWarning)
322322

323+
def _resolve_write_request_options(self, kwargs):
324+
no_sync = kwargs.pop('no_sync', self._write_options.no_sync)
325+
accept_partial = kwargs.pop('accept_partial', self._write_options.accept_partial)
326+
use_v2_api = kwargs.pop('use_v2_api', self._write_options.use_v2_api)
327+
if use_v2_api and no_sync:
328+
raise ValueError("invalid write options: no_sync cannot be used with use_v2_api")
329+
return no_sync, accept_partial, use_v2_api
330+
323331
def write(self, bucket: str, org: str = None,
324332
record: Union[
325333
str, Iterable['str'], Point, Iterable['Point'], dict, Iterable['dict'], bytes, Iterable['bytes'],
@@ -400,20 +408,21 @@ def write(self, bucket: str, org: str = None,
400408
write_precision = self._write_options.write_precision
401409

402410
self._write_options.validate()
411+
kwargs = dict(kwargs)
412+
no_sync, accept_partial, use_v2_api = self._resolve_write_request_options(kwargs)
403413

404414
if 'tag_order' in kwargs:
405415
kwargs['tag_order'] = sanitize_tag_order(kwargs.get('tag_order'))
406416
else:
407417
kwargs['tag_order'] = self._write_options.tag_order
408418

409419
if self._write_options.write_type is WriteType.batching:
420+
kwargs['no_sync'] = no_sync
421+
kwargs['accept_partial'] = accept_partial
422+
kwargs['use_v2_api'] = use_v2_api
410423
return self._write_batching(bucket, org, record,
411424
write_precision, **kwargs)
412425

413-
no_sync = self._write_options.no_sync
414-
accept_partial = self._write_options.accept_partial
415-
use_v2_api = self._write_options.use_v2_api
416-
417426
payloads = defaultdict(list)
418427
self._serialize(record, write_precision, payloads, **kwargs)
419428

@@ -602,9 +611,8 @@ def _retry_callback_delegate(exception):
602611
else:
603612
_retry_callback_delegate = None
604613

605-
no_sync = self._write_options.no_sync
606-
accept_partial = self._write_options.accept_partial
607-
use_v2_api = self._write_options.use_v2_api
614+
kwargs = dict(kwargs)
615+
no_sync, accept_partial, use_v2_api = self._resolve_write_request_options(kwargs)
608616

609617
retry = self._write_options.to_retry_strategy(retry_callback=_retry_callback_delegate)
610618

tests/test_write_local_server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ def test_write_with_no_sync_true(self, httpserver: HTTPServer):
7777
method="POST", uri="/api/v3/write_lp",
7878
query_string={"org": "ORG", "db": "DB", "precision": "microsecond", "no_sync": "true"}))
7979

80+
def test_write_with_no_sync_true_in_kwargs(self, httpserver: HTTPServer):
81+
self.set_response_status(httpserver, 200)
82+
83+
InfluxDBClient3(
84+
host=(httpserver.url_for("/")), org="ORG", database="DB", token="TOKEN",
85+
write_client_options=write_client_options(
86+
write_options=WriteOptions(write_type=WriteType.synchronous, write_precision=WritePrecision.US)
87+
)
88+
).write(self.SAMPLE_RECORD, no_sync=True)
89+
90+
self.assert_request_made(httpserver, RequestMatcher(
91+
method="POST", uri="/api/v3/write_lp",
92+
query_string={"org": "ORG", "db": "DB", "precision": "microsecond", "no_sync": "true"}))
93+
8094
def test_write_with_accept_partial_false(self, httpserver: HTTPServer):
8195
self.set_response_status(httpserver, 200)
8296

@@ -113,6 +127,20 @@ def test_write_with_use_v2_api_true(self, httpserver: HTTPServer):
113127
method="POST", uri="/api/v2/write",
114128
query_string={"org": "ORG", "bucket": "DB", "precision": "us"}))
115129

130+
def test_write_with_use_v2_api_true_in_kwargs(self, httpserver: HTTPServer):
131+
self.set_response_status(httpserver, 200)
132+
133+
InfluxDBClient3(
134+
host=(httpserver.url_for("/")), org="ORG", database="DB", token="TOKEN",
135+
write_client_options=write_client_options(
136+
write_options=WriteOptions(write_type=WriteType.synchronous, write_precision=WritePrecision.US)
137+
)
138+
).write(self.SAMPLE_RECORD, use_v2_api=True, accept_partial=False)
139+
140+
self.assert_request_made(httpserver, RequestMatcher(
141+
method="POST", uri="/api/v2/write",
142+
query_string={"org": "ORG", "bucket": "DB", "precision": "us"}))
143+
116144
def test_write_with_v3_on_v2_server(self, httpserver: HTTPServer):
117145
self.set_response_status(httpserver, HTTPStatus.METHOD_NOT_ALLOWED)
118146

@@ -186,6 +214,17 @@ def test_write_invalid_use_v2_api_and_no_sync(self, httpserver: HTTPServer):
186214
with pytest.raises(ValueError, match=r".*invalid write options: no_sync cannot be used with use_v2_api.*"):
187215
client.write(self.SAMPLE_RECORD)
188216

217+
def test_write_invalid_use_v2_api_and_no_sync_in_kwargs(self, httpserver: HTTPServer):
218+
client = InfluxDBClient3(
219+
host=(httpserver.url_for("/")), org="ORG", database="DB", token="TOKEN",
220+
write_client_options=write_client_options(
221+
write_options=WriteOptions(write_type=WriteType.synchronous)
222+
)
223+
)
224+
225+
with pytest.raises(ValueError, match=r".*invalid write options: no_sync cannot be used with use_v2_api.*"):
226+
client.write(self.SAMPLE_RECORD, use_v2_api=True, no_sync=True)
227+
189228
def test_write_with_timeout_in_write_options(self, httpserver: HTTPServer):
190229
self.delay_response(httpserver, 0.5)
191230

0 commit comments

Comments
 (0)