Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion influxdb_client_3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_influxdb_client_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
14 changes: 7 additions & 7 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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) \
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_write_local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading