11import importlib .util
2+ import json
23import os
34import urllib .parse
45from typing import Any , List , Literal , Optional , TYPE_CHECKING
56
67import pyarrow as pa
78
9+ from influxdb_client_3 .version import USER_AGENT
10+ from influxdb_client_3 .write_client ._sync import rest_client as rest
11+
812if TYPE_CHECKING :
913 import pandas as pd
1014 import polars as pl
1418from influxdb_client_3 .exceptions import InfluxDBError
1519from influxdb_client_3 .query .query_api import QueryApi as _QueryApi , QueryApiOptionsBuilder
1620from influxdb_client_3 .read_file import UploadFile
17- from influxdb_client_3 .write_client import InfluxDBClient as _InfluxDBClient , WriteOptions , Point
21+ from influxdb_client_3 .write_client import WriteOptions , Point
1822from influxdb_client_3 .write_client .client .write_api import WriteApi as _WriteApi , SYNCHRONOUS , ASYNCHRONOUS , \
1923 PointSettings , DefaultWriteOptions , WriteType
2024from influxdb_client_3 .write_client .domain .write_precision import WritePrecision
@@ -185,15 +189,19 @@ def _parse_timeout(to: str) -> int:
185189class InfluxDBClient3 :
186190 def __init__ (
187191 self ,
188- host = None ,
192+ host = 'localhost' ,
189193 org = None ,
190194 database = None ,
191195 token = None ,
196+ auth_scheme = None ,
197+ enable_gzip = False ,
198+ gzip_threshold = None ,
192199 write_client_options = None ,
193200 flight_client_options = None ,
194201 write_port_overwrite = None ,
195202 query_port_overwrite = None ,
196203 disable_grpc_compression = False ,
204+ point_settings = None ,
197205 ** kwargs ):
198206 """
199207 Initialize an InfluxDB client.
@@ -212,6 +220,12 @@ def __init__(
212220 :type flight_client_options: dict[str, any]
213221 :param disable_grpc_compression: Disable gRPC compression for Flight query responses. Default is False.
214222 :type disable_grpc_compression: bool
223+ :param point_settings The settings for Points
224+ :type point_settings: PointSettings
225+ :param enable_gzip: Enable GZIP compression for write requests.
226+ :type enable_gzip: bool
227+ :param gzip_threshold: Minimum payload size (bytes) to trigger GZIP when enable_gzip is True.
228+ :type gzip_threshold: int
215229 :key auth_scheme: token authentication scheme. Set to "Bearer" for Edge.
216230 :key bool verify_ssl: Set this to false to skip verifying SSL certificate when calling API from https server.
217231 :key str ssl_ca_cert: Set this to customize the certificate file to verify the peer.
@@ -293,14 +307,44 @@ def __init__(
293307 if write_port_overwrite is not None :
294308 port = write_port_overwrite
295309
296- self ._client = _InfluxDBClient (
297- url = f"{ scheme } ://{ hostname } :{ port } " ,
310+ auth_schema = 'Token' if auth_scheme is None else auth_scheme
311+ default_header = {
312+ 'User-Agent' : USER_AGENT
313+ }
314+ if self ._token is not None :
315+ default_header ['Authorization' ] = f'{ auth_schema } { self ._token } '
316+ self .base_url = f"{ scheme } ://{ hostname } :{ port } "
317+ self .default_header = default_header
318+ self .rest_client = rest .RestClient (
319+ base_url = self .base_url ,
320+ default_header = default_header ,
321+ verify_ssl = kwargs .get ('verify_ssl' , True ),
322+ ssl_ca_cert = kwargs .get ('ssl_ca_cert' , None ),
323+ cert_file = kwargs .get ('cert_file' , None ),
324+ cert_key_file = kwargs .get ('cert_key_file' , None ),
325+ cert_key_password = kwargs .get ('cert_key_password' , None ),
326+ ssl_context = kwargs .get ('ssl_context' , None ),
327+ proxy = kwargs .get ('proxy' , None ),
328+ proxy_headers = kwargs .get ('proxy_headers' , None ),
329+ retries = kwargs .get ('retries' , False )
330+ )
331+
332+ if point_settings is None :
333+ point_settings = PointSettings ()
334+
335+ self ._write_api = _WriteApi (
298336 token = self ._token ,
337+ bucket = self ._database ,
299338 org = self ._org ,
339+ gzip_threshold = gzip_threshold ,
340+ enable_gzip = enable_gzip ,
341+ auth_scheme = auth_scheme ,
300342 timeout = write_timeout ,
301- ** kwargs )
302-
303- self ._write_api = _WriteApi (influxdb_client = self ._client , ** self ._write_client_options )
343+ default_header = default_header ,
344+ rest_client = self .rest_client ,
345+ point_settings = point_settings ,
346+ ** self ._write_client_options
347+ )
304348
305349 if query_port_overwrite is not None :
306350 port = query_port_overwrite
@@ -658,32 +702,28 @@ async def query_async(self, query: str, language: str = "sql", mode: str = "all"
658702 except ArrowException as e :
659703 raise InfluxDB3ClientQueryError (f"Error while executing query: { e } " )
660704
661- def get_server_version (self ) -> str :
705+ def get_server_version (self ) -> Optional [ str ] :
662706 """
663- Get the version of the connected InfluxDB server.
707+ Retrieves the server version by querying the designated endpoint and
708+ extracting the version information from either response headers or
709+ the response body.
664710
665- This method makes a ping request to the server and extracts the version information
666- from either the response headers or response body.
711+ This method interacts with a REST API endpoint to fetch the server's
712+ version details, which might be stored in a specific HTTP header or
713+ available in the response body as part of a JSON structure.
667714
668- :return: The version string of the InfluxDB server.
669- :rtype: str
715+ :return: The version string of the server if available, otherwise None .
716+ :rtype: Optional[ str]
670717 """
671- version = None
672- (resp_body , _ , header ) = self ._client .api_client .call_api (
673- resource_path = "/ping" ,
674- method = "GET" ,
675- response_type = object
676- )
677-
678- for key , value in header .items ():
718+ resp = self .rest_client .request (url = '/ping' , method = "GET" , headers = self .default_header )
719+ for key , value in resp .getheaders ().items ():
679720 if key .lower () == "x-influxdb-version" :
680- version = value
681- break
682-
683- if version is None and isinstance (resp_body , dict ):
684- version = resp_body ['version' ]
721+ return value
685722
686- return version
723+ string_body = resp .get_string_body ()
724+ if string_body is not None :
725+ return json .loads (string_body )['version' ]
726+ return None
687727
688728 def flush (self ):
689729 """
@@ -702,7 +742,6 @@ def close(self):
702742 """Close the client and clean up resources."""
703743 self ._write_api .close ()
704744 self ._query_api .close ()
705- self ._client .close ()
706745
707746 def __enter__ (self ):
708747 return self
0 commit comments