diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c776a..2878bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # 0.17.0 [unreleased] +### Bug Fixes + +1. [#132](https://github.com/InfluxCommunity/influxdb3-python/pull/132): Fixes support for IPv6 addresses in the `host` parameter of the client. The client now correctly handles IPv6 addresses by enclosing them in square brackets. + ### CI 1. [#164](https://github.com/InfluxCommunity/influxdb3-python/pull/164): Fix pipelines not downloading the correct python images. diff --git a/influxdb_client_3/__init__.py b/influxdb_client_3/__init__.py index bbfa89b..6c1128c 100644 --- a/influxdb_client_3/__init__.py +++ b/influxdb_client_3/__init__.py @@ -15,6 +15,8 @@ PointSettings, DefaultWriteOptions, WriteType from influxdb_client_3.write_client.domain.write_precision import WritePrecision +from ipaddress import IPv6Address, AddressValueError + polars = importlib.util.find_spec("polars") is not None INFLUX_HOST = "INFLUX_HOST" @@ -266,6 +268,12 @@ def __init__( hostname = parsed_url.hostname if parsed_url.hostname else host port = parsed_url.port if parsed_url.port else 443 + try: + IPv6Address(hostname) + hostname = f"[{hostname}]" + except AddressValueError: + pass + # Construct the clients using the parsed values if write_port_overwrite is not None: port = write_port_overwrite diff --git a/tests/test_influxdb_client_3.py b/tests/test_influxdb_client_3.py index 7bab679..96dc2a9 100644 --- a/tests/test_influxdb_client_3.py +++ b/tests/test_influxdb_client_3.py @@ -135,6 +135,30 @@ async def test_query_async(self): assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list + @asyncio_run + async def test_query_async_ipv6(self): + with ConstantFlightServer() as server: + client = InfluxDBClient3( + host=f"http://[::1]:{server.port}", + org="my_org", + database="my_db", + token="my_token", + ) + + query = "SELECT * FROM my_data" + + table = await client.query_async(query=query, language="sql") + + result_list = table.to_pylist() + + cd = ConstantData() + for item in cd.to_list(): + assert item in result_list + + assert {'data': 'database', 'reference': 'my_db', 'value': -1.0} in result_list + assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list + assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list + def test_write_api_custom_options_no_error(self): write_options = WriteOptions(write_type=WriteType.batching) write_client_option = {'write_options': write_options} @@ -352,6 +376,5 @@ def test_get_version_fail(self): host=f'http://{server.host}:{server.port}', org="ORG", database="DB", token="TOKEN" ).get_server_version() - if __name__ == '__main__': unittest.main()