Skip to content

Commit c7b3101

Browse files
wip
1 parent 88507cf commit c7b3101

File tree

5 files changed

+118
-32
lines changed

5 files changed

+118
-32
lines changed

influxdb_client_3/__init__.py

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
INFLUX_PROFILERS = "INFLUX_PROFILERS"
3333
INFLUX_TAG = "INFLUX_TAG"
3434

35+
3536
def write_client_options(**kwargs):
3637
"""
3738
Function for providing additional arguments for the WriteApi client.
@@ -104,11 +105,19 @@ def _merge_options(defaults, exclude_keys=None, custom=None):
104105

105106
def _parse_precision(precision):
106107
"""
107-
Parse and validate precision value.
108-
109-
:param precision: Precision value to validate
110-
:return: Validated precision value
111-
:raises ValueError: If precision is invalid
108+
Parses the precision value and ensures it is valid.
109+
110+
This function checks that the given `precision` is one of the allowed
111+
values defined in `WritePrecision`. If the precision is invalid, it
112+
raises a `ValueError`. The function returns the valid precision value
113+
if it passes validation.
114+
115+
:param precision: The precision value to be validated.
116+
Must be one of WritePrecision.NS, WritePrecision.MS,
117+
WritePrecision.S, or WritePrecision.US.
118+
:return: The valid precision value.
119+
:rtype: WritePrecision
120+
:raises ValueError: If the provided precision is not valid.
112121
"""
113122
if precision not in [WritePrecision.NS, WritePrecision.MS, WritePrecision.S, WritePrecision.US]:
114123
raise ValueError(f"Invalid precision value: {precision}")
@@ -117,11 +126,18 @@ def _parse_precision(precision):
117126

118127
def _parse_gzip_threshold(threshold):
119128
"""
120-
Parse and validate gzip threshold value.
121-
122-
:param threshold: Threshold value to validate
123-
:return: Validated threshold value
124-
:raises ValueError: If threshold is invalid
129+
Parses and validates the provided threshold value.
130+
131+
This function ensures that the given threshold is a valid integer value,
132+
and it raises an appropriate error if the threshold is not valid. It also
133+
enforces that the threshold value is non-negative.
134+
135+
:param threshold: The input threshold value to be parsed and validated.
136+
:type threshold: Any
137+
:return: The validated threshold value as an integer.
138+
:rtype: int
139+
:raises ValueError: If the provided threshold is not an integer or if it is
140+
negative.
125141
"""
126142
try:
127143
threshold = int(threshold)
@@ -248,30 +264,32 @@ def __init__(
248264
flight_client_options=flight_client_options,
249265
proxy=kwargs.get("proxy", None), options=q_opts_builder.build())
250266

251-
252267
@classmethod
253268
def from_env(cls, **kwargs: Any) -> 'InfluxDBClient3':
254269

255270
"""
256-
Create an instance of `InfluxDBClient3` using environment variables for configuration.
257-
258-
This function retrieves and validates the following required environment variables:
259-
- `INFLUX_HOST`: The hostname or IP address of the InfluxDB server.
260-
- `INFLUX_TOKEN`: The authentication token used for accessing the server.
261-
- `INFLUX_DATABASE`: The default database for the client operations.
262-
And optional environment variable:
263-
- `INFLUX_ORG`: The organization associated with InfluxDB operations.
264-
Defaults to "default" if not set.
265-
266-
If any of the required environment variables are not set, a ValueError will be
267-
raised with details about the missing variables.
268-
269-
:param kwargs: Additional keyword arguments that will be passed to the
270-
`InfluxDBClient3` constructor for customization. This allows for
271-
configuring specific client behaviors like write_client_options,
272-
flight_client_options, SSL settings, etc.
273-
:return: An initialized `InfluxDBClient3` instance.
274-
:raises ValueError: If any required environment variables are not set.
271+
Creates an instance of InfluxDBClient3 configured by specific environment
272+
variables. This method automatically loads configuration settings,
273+
such as connection details, security parameters, and performance
274+
options, from environment variables and initializes the client
275+
accordingly.
276+
277+
:param cls:
278+
The class used to create the client instance.
279+
:param kwargs:
280+
Additional optional parameters that can be passed to customize the
281+
configuration or override specific settings derived from the
282+
environment variables.
283+
284+
:raises ValueError:
285+
If any required environment variables are missing or have empty
286+
values.
287+
288+
:return:
289+
An initialized instance of the `InfluxDBClient3` class with all the
290+
configuration settings applied.
291+
:rtype:
292+
InfluxDBClient3
275293
"""
276294

277295
required_vars = {
@@ -344,7 +362,6 @@ def from_env(cls, **kwargs: Any) -> 'InfluxDBClient3':
344362
**kwargs
345363
)
346364

347-
348365
def write(self, record=None, database=None, **kwargs):
349366
"""
350367
Write data to InfluxDB.

influxdb_client_3/write_client/_sync/api_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ def __call_api(
179179
response_data.getheaders())
180180

181181
def check_should_compress(self, body: bytearray, gzip_threshold: int, enable_gzip: bool) -> bool:
182+
"""
183+
Determines whether the given body should be compressed based on its size,
184+
a defined threshold for compression, and a flag indicating whether
185+
compression is enabled.
186+
187+
This function evaluates whether the body meets the required criteria for
188+
compression. Compression may be enabled explicitly or conditionally
189+
based on the body size exceeding the provided threshold.
190+
191+
:param body: The content to be evaluated for compression.
192+
:type body: bytearray
193+
:param gzip_threshold: The minimum size threshold for compression to be applied.
194+
:type gzip_threshold: int
195+
:param enable_gzip: A flag indicating whether gzip compression is enabled.
196+
It can explicitly enable or disable compression, or conditionally
197+
allow compression if the body size exceeds the threshold.
198+
:type enable_gzip: bool
199+
:return: Returns True if the body meets the criteria for compression;
200+
otherwise, returns False.
201+
:rtype: bool
202+
"""
182203
body_size = len(body)
183204
if enable_gzip is True or (enable_gzip is not False and (gzip_threshold and body_size >= gzip_threshold)):
184205
return True

influxdb_client_3/write_client/client/_base.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,50 @@
3636
class _BaseClient(object):
3737
def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, gzip_threshold=None, org: str = None,
3838
default_tags: dict = None, http_client_logger: str = None, **kwargs) -> None:
39+
"""
40+
Initializes the configuration for an HTTP client with support for customizable settings
41+
such as authentication token, debugging, timeout, and gzip compression. This class
42+
encapsulates the client configuration, logging setup, and authentication mechanisms
43+
to allow seamless interaction with an HTTP backend.
44+
45+
:param url: The base URL for the HTTP client.
46+
:type url: str
47+
:param token: The authentication token to be used in requests.
48+
:type token: str
49+
:param debug: Enables debug mode for logging client operations. Defaults to None.
50+
:type debug: bool, optional
51+
:param timeout: The timeout duration for HTTP requests in milliseconds. Defaults to 10,000.
52+
:type timeout: int
53+
:param enable_gzip: Flag to enable or disable gzip compression. Defaults to False.
54+
:type enable_gzip: bool
55+
:param gzip_threshold: The threshold size for enabling gzip compression, if applicable.
56+
:type gzip_threshold: int, optional
57+
:param org: The organization identifier to be associated with the client.
58+
:type org: str, optional
59+
:param default_tags: A dictionary of default tags to add to outgoing requests for metadata purposes.
60+
:type default_tags: dict, optional
61+
:param http_client_logger: The logger name to use for HTTP client-specific logging.
62+
:type http_client_logger: str, optional
63+
:param kwargs: Additional optional parameters to customize the HTTP client:
64+
65+
- verify_ssl: Flag to enable or disable SSL certificate verification. Defaults to True.
66+
- ssl_ca_cert: Path to the CA certificate file for verifying SSL. Defaults to None.
67+
- cert_file: Path to a client SSL certificate file for authentication. Defaults to None.
68+
- cert_key_file: Path to the client’s SSL key file, if separate. Defaults to None.
69+
- cert_key_password: Password for the client’s SSL key file, if applicable. Defaults to None.
70+
- ssl_context: SSLContext object to configure custom SSL parameters. Defaults to None.
71+
- proxy: Proxy server URL, if a proxy is to be used. Defaults to None.
72+
- proxy_headers: A dictionary containing custom headers for the proxy server. Defaults to None.
73+
- connection_pool_maxsize: Defines the maximum pool size for connections.
74+
Default inherits from the configuration.
75+
- retries: Determines if request retrying is enabled. Defaults to None.
76+
- profilers: Profilers for performance tracking. Defaults to None.
77+
- username: Username for basic authentication. Defaults to None.
78+
- password: Password for basic authentication. Defaults to None.
79+
- auth_scheme: Custom authentication scheme to use with the token. Defaults to "Token".
80+
- auth_basic: Boolean flag to enable HTTP Basic Authentication. Defaults to False.
81+
82+
"""
3983
self.url = url
4084
self.org = org
4185

influxdb_client_3/write_client/client/influxdb_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def __init__(self, url, token: str = None,
4949
except batching writes. As a default there is no one retry strategy.
5050
:key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that
5151
does not use auth-enabled but is protected by a reverse proxy with basic authentication.
52-
(defaults to false, don't set to true when talking to InfluxDB 2)
52+
(defaults to false, don't set to true when talking to InfluxDB 2).
53+
:key int gzip_threshold: If the payload size is larger than this gzip_threshold, then
54+
the payload will be zipped.
5355
:key str username: ``username`` to authenticate via username and password credentials to the InfluxDB 2.x
5456
:key str password: ``password`` to authenticate via username and password credentials to the InfluxDB 2.x
5557
:key list[str] profilers: list of enabled Flux profilers

influxdb_client_3/write_client/client/write_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def __init__(self, write_type: WriteType = WriteType.batching,
8080
:param max_close_wait: the maximum time to wait for writes to be flushed if close() is called
8181
:param write_precision: the time precision for the data written to InfluxDB.
8282
:param write_scheduler:
83+
:param gzip_threshold: if the payload size is larger than the gzip_threshold, the payload will be zipped.
84+
:param enable_gzip: set true to enable to zip the payload.
8385
"""
8486
self.write_type = write_type
8587
self.batch_size = batch_size

0 commit comments

Comments
 (0)