diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad8a29..ff067fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # 0.17.0 [unreleased] +### Features + +1. [#173](https://github.com/InfluxCommunity/influxdb3-python/pull/173): Supporting URL with path prefix. + ### 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..645b03d 100644 --- a/influxdb_client_3/__init__.py +++ b/influxdb_client_3/__init__.py @@ -266,12 +266,16 @@ def __init__( hostname = parsed_url.hostname if parsed_url.hostname else host port = parsed_url.port if parsed_url.port else 443 + path = "" + if parsed_url.scheme is not None and parsed_url.hostname is not None and parsed_url.port is not None: + path = parsed_url.path + # Construct the clients using the parsed values if write_port_overwrite is not None: port = write_port_overwrite self._client = _InfluxDBClient( - url=f"{scheme}://{hostname}:{port}", + url=f"{scheme}://{hostname}:{port}{path}", token=self._token, org=self._org, timeout=write_timeout, diff --git a/tests/test_influxdb_client_3.py b/tests/test_influxdb_client_3.py index 7bab679..6792507 100644 --- a/tests/test_influxdb_client_3.py +++ b/tests/test_influxdb_client_3.py @@ -67,6 +67,14 @@ def test_token_auth_scheme_explicit(self): ) self.assertEqual(client._client.auth_header_value, "my_scheme my_token") + def test_write_port_overwrite(self): + with InfluxDBClient3( + host="http://localhost:8080", + write_port_overwrite=8086, + token="my_token", + ) as client: + self.assertEqual(client._client.url, "http://localhost:8086") + def test_write_options(self): client = InfluxDBClient3( host="localhost", @@ -352,6 +360,33 @@ def test_get_version_fail(self): host=f'http://{server.host}:{server.port}', org="ORG", database="DB", token="TOKEN" ).get_server_version() + def test_url_with_path_prefix(self): + server = self.http_server + server.expect_request('/prefix/prefix1/ping').respond_with_json( + response_json={"version": "3.0"}, + ) + with InfluxDBClient3( + host=f'http://{server.host}:{server.port}/prefix/prefix1', + org="ORG", + database="DB", + token="TOKEN" + ) as client: + assert client.get_server_version() == "3.0" + + def test_url_error_without_path_prefix(self): + server = self.http_server + server.expect_request('/prefix/ping').respond_with_json( + response_json={"version": "3.0"}, + ) + with InfluxDBClient3( + host=f'http://{server.host}:{server.port}', + org="ORG", + database="DB", + token="TOKEN" + ) as client: + with self.assertRaises(ApiException): + client.get_server_version() + if __name__ == '__main__': unittest.main() diff --git a/tests/test_query.py b/tests/test_query.py index b951bdf..d15c1db 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -138,7 +138,7 @@ def test_query_proxy_base_client(self): ) assert client._query_api._proxy == test_proxy - assert ('grpc.http_proxy', test_proxy) in\ + assert ('grpc.http_proxy', test_proxy) in \ client._query_api._flight_client_options.get('generic_options') def create_cert_file(self, file_name): @@ -154,9 +154,9 @@ def test_query_api_options_builder(self): cert_file = "cert_test.pem" self.create_cert_file(cert_file) builder = QueryApiOptionsBuilder() - options = builder.proxy(proxy_name)\ - .root_certs(cert_file)\ - .tls_verify(False)\ + options = builder.proxy(proxy_name) \ + .root_certs(cert_file) \ + .tls_verify(False) \ .build() try: @@ -175,7 +175,7 @@ def test_query_client_with_options(self): cert_chain = 'mTLS_explicit_chain' self.create_cert_file(cert_file) test_flight_client_options = {'private_key': private_key, 'cert_chain': cert_chain} - options = QueryApiOptionsBuilder()\ + options = QueryApiOptionsBuilder() \ .proxy(proxy_name) \ .root_certs(cert_file) \ .tls_verify(False) \ @@ -436,7 +436,7 @@ async def fibo(iters): @asyncio_run async def test_query_async_timeout(self): with pytest.raises(FlightTimedOutError): - with ConstantFlightServer() as server: + with ConstantFlightServerDelayed(delay=1) as server: connection_string = f"grpc://localhost:{server.port}" token = "my_token" database = "my_database" @@ -452,7 +452,7 @@ async def test_query_async_timeout(self): def test_query_timeout_per_call_override(self): with pytest.raises(FlightTimedOutError): - with ConstantFlightServer() as server: + with ConstantFlightServerDelayed(delay=1) as server: connection_string = f"grpc://localhost:{server.port}" token = "my_token" database = "my_database" diff --git a/tests/test_write_local_server.py b/tests/test_write_local_server.py index 9e7e406..2fe9bc0 100644 --- a/tests/test_write_local_server.py +++ b/tests/test_write_local_server.py @@ -40,6 +40,22 @@ def test_write_default_params(self, httpserver: HTTPServer): method="POST", uri="/api/v2/write", query_string={"org": "ORG", "bucket": "DB", "precision": "ns"})) + def test_write_with_path_prefix(self): + with HTTPServer(host="localhost", port=8086, ssl_context=None) as httpserver: + httpserver.expect_request("/prefix/prefix1/api/v2/write").respond_with_data(status=200) + + with InfluxDBClient3( + host=(httpserver.url_for("/prefix/prefix1")), org="ORG", database="DB", token="TOKEN", + write_client_options=write_client_options( + write_options=WriteOptions(write_type=WriteType.synchronous) + ) + ) as client: + client.write(self.SAMPLE_RECORD) + + self.assert_request_made(httpserver, RequestMatcher( + method="POST", uri="/prefix/prefix1/api/v2/write", + query_string={"org": "ORG", "bucket": "DB", "precision": "ns"})) + def test_write_with_write_options(self, httpserver: HTTPServer): self.set_response_status(httpserver, 200)