8000 fix: Allow custom headers and other arguments supported by FlightCall… · InfluxCommunity/influxdb3-python@f571d3e · GitHub
[go: up one dir, main page]

Skip to content

Commit f571d3e

Browse files
committed
fix: Allow custom headers and other arguments supported by FlightCallOptions
- Allow passing FlightCallOptions as keyword args in query(). - Merges default args with custom args set by the user, and then passes them into the FlightCallOptions constructor. - Only supports headers for now. - Successfully tested with `iox-debug` header. - TODO: verify that the server is receiving the specified `trace-id` header. - TODO: test InfluxDB support for other FlightCallOptions (e.g. timeout).
1 parent 0ba2250 commit f571d3e

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

influxdb_client_3/__init__.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ def file_parser_options(**kwargs):
4141
return kwargs
4242

4343

44+
def _deep_merge(target, source):
45+
if isinstance(target, dict) and isinstance(source, dict):
46+
for key, value in source.items():
47+
if key in target and isinstance(target[key], (dict, list)) and isinstance(value, (dict, list)):
48+
# If both target and source values are dictionaries or lists, merge them recursively
49+
target[key] = _deep_merge(target[key], value)
50+
else:
51+
# Otherwise, replace the target value with the source value
52+
target[key] = value
53+
elif isinstance(target, list) and isinstance(source, list):
54+
# If both target and source are lists, concatenate them
55+
target.extend(source)
56+
else:
57+
# For other types, simply replace the target with the source
58+
target = source
59+
return target
60+
4461
class InfluxDBClient3:
4562
def __init__(
4663
self,
@@ -172,7 +189,8 @@ def _process_dataframe(self, df, measurement_name, tag_columns, timestamp_column
172189
data_frame_measurement_name=measurement_name,
173190
data_frame_tag_columns=tag_columns,
174191
data_frame_timestamp_column=timestamp_column, **kwargs)
175-
192+
193+
176194
def query(self, query, language="sql", mode="all", database=None,**kwargs ):
177195
"""
178196
Query data from InfluxDB.
@@ -185,7 +203,7 @@ def query(self, query, language="sql", mode="all", database=None,**kwargs ):
185203
:type mode: str
186204
:param database: The database to query from. If not provided, uses the database provided during initialization.
187205
:type database: str
188-
:param kwargs: Additional arguments for the query.
206+
:param kwargs: FlightClientCallOptions for the query.
189207
:return: The queried data.
190208
"""
191209

@@ -194,10 +212,14 @@ def query(self, query, language="sql", mode="all", database=None,**kwargs ):
194212
database = self._database
195213

196214
try:
197-
headers = [(b"authorization", f"Bearer {self._token}".encode('utf-8'))]
198-
199215
# Create an authorization header
200-
_options = FlightCallOptions(headers=headers, **kwargs)
216+
args = {"headers": [(b"authorization", f"Bearer {self._token}".encode('utf-8'))]}
217+
custom_args = {key: value for key, value in kwargs.items()}
218+
# Merge defaults with user-provided arguments
219+
_deep_merge(args, custom_args)
220+
print(args["headers"])
221+
_options = FlightCallOptions(headers=args["headers"])
222+
print(_options)
201223
ticket_data = {"database": database, "sql_query": query, "query_type": language}
202224
ticket = Ticket(json.dumps(ticket_data).encode('utf-8'))
203225
flight_reader = self._flight_client.do_get(ticket, _options)
@@ -225,8 +247,7 @@ def __enter__(self):
225247

226248
def __exit__(self, exc_type, exc_val, exc_tb):
227249
self.close()
228-
229-
250+
230251
__all__ = [
231252
"InfluxDBClient3",
232253
"Point",

0 commit comments

Comments
 (0)
0