Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 2 deletions src/crate/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(
):
"""
:param servers:
either a string in the form of '<hostname>:<port>'
or a list of servers in the form of ['<hostname>:<port>', '...']
either a string in the form of '<hostname>:<port>/<path>'
or a list of servers in the form of ['<hostname>:<port>/<path>', '...']
:param timeout:
(optional)
define the retry timeout for unreachable servers in seconds
Expand Down
29 changes: 26 additions & 3 deletions src/crate/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
Loading