From be8d1fa91f243d84ab33ae82aade03fdedc329a5 Mon Sep 17 00:00:00 2001 From: Alexander Kleinjohann Date: Mon, 19 Jan 2026 15:54:39 +0100 Subject: [PATCH 1/3] Propagate path prefix in server urls to all requests --- src/crate/client/connection.py | 4 ++-- src/crate/client/http.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/crate/client/connection.py b/src/crate/client/connection.py index 0638a018c..75781cddf 100644 --- a/src/crate/client/connection.py +++ b/src/crate/client/connection.py @@ -53,8 +53,8 @@ def __init__( ): """ :param servers: - either a string in the form of ':' - or a list of servers in the form of [':', '...'] + either a string in the form of ':/' + or a list of servers in the form of [':/', '...'] :param timeout: (optional) define the retry timeout for unreachable servers in seconds diff --git a/src/crate/client/http.py b/src/crate/client/http.py index 0d9d6c9f2..681ceec40 100644 --- a/src/crate/client/http.py +++ b/src/crate/client/http.py @@ -34,7 +34,7 @@ from base64 import b64encode from decimal import Decimal from time import time -from urllib.parse import urlparse +from urllib.parse import SplitResult, urlparse import orjson import urllib3 @@ -143,6 +143,18 @@ def __init__(self, server, **pool_kw): pool_kw.pop("socket_tcp_keepintvl", None), pool_kw.pop("socket_tcp_keepcnt", None), ) + self.path_prefix = "" + try: + parsed_url = urlparse(server) + except Exception as e: + parsed_url = SplitResult("", "", "", "", "") + logger.warning( + "Unable to extract path prefix from server url: {ex}".format( + ex=e + ) + ) + if parsed_url.path: + self.path_prefix = parsed_url.path self.pool = connection_from_url( server, socket_options=socket_options, @@ -166,6 +178,10 @@ def request( Always set the Content-Length and the Content-Type header. """ + if self.path_prefix: + path = "/{path_prefix}/{path}".format( + path_prefix=self.path_prefix.strip("/"), path=path.strip("/") + ) if headers is None: headers = {} if "Content-Length" not in headers: @@ -276,20 +292,27 @@ def _server_url(server): >>> print(_server_url('a')) http://a + >>> print(_server_url('a/path')) + http://a/path >>> print(_server_url('a:9345')) http://a:9345 + >>> print(_server_url('a:9345/path')) + http://a:9345/path >>> print(_server_url('https://a:9345')) https://a:9345 + >>> print(_server_url('https://a:9345/path')) + https://a:9345/path >>> print(_server_url('https://a')) https://a + >>> print(_server_url('https://a/path')) + https://a/path >>> print(_server_url('demo.crate.io')) http://demo.crate.io """ if not _HTTP_PAT.match(server): server = "http://%s" % server parsed = urlparse(server) - url = "%s://%s" % (parsed.scheme, parsed.netloc) - return url + return parsed.geturl() def _to_server_list(servers): From a3897ef4f417e88b5373fed8019d7dd88ecf09e4 Mon Sep 17 00:00:00 2001 From: Alexander Kleinjohann Date: Mon, 26 Jan 2026 11:24:21 +0100 Subject: [PATCH 2/3] Strip '/' from path prefix once per server, not once per request --- src/crate/client/http.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crate/client/http.py b/src/crate/client/http.py index 681ceec40..3d065f38b 100644 --- a/src/crate/client/http.py +++ b/src/crate/client/http.py @@ -154,7 +154,7 @@ def __init__(self, server, **pool_kw): ) ) if parsed_url.path: - self.path_prefix = parsed_url.path + self.path_prefix = parsed_url.path.strip("/") self.pool = connection_from_url( server, socket_options=socket_options, @@ -180,7 +180,7 @@ def request( """ if self.path_prefix: path = "/{path_prefix}/{path}".format( - path_prefix=self.path_prefix.strip("/"), path=path.strip("/") + path_prefix=self.path_prefix, path=path.strip("/") ) if headers is None: headers = {} From 564ad7492dea9521dd684860779e1b2f8858285e Mon Sep 17 00:00:00 2001 From: Alexander Kleinjohann Date: Mon, 26 Jan 2026 11:26:24 +0100 Subject: [PATCH 3/3] Add path prefix change to CHANGES.rst --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 48c8588f6..4c4713995 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,8 @@ Unreleased - Dropped support for Python versions earlier than 3.10 as they've reached their end of life. +- Parse path prefixes from server URLs and propagate them to all requests. + 2025/01/30 2.0.0 ================