Skip to content

Commit 455f9fc

Browse files
committed
Support both host and hosts
But only one host in hosts list
1 parent 34f25a0 commit 455f9fc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

elasticsearch_serverless/_async/client/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class AsyncElasticsearch(BaseClient):
121121
def __init__(
122122
self,
123123
host: t.Optional[_TYPE_HOST] = None,
124+
hosts: t.Optional[t.List[_TYPE_HOST]] = None,
124125
*,
125126
# API
126127
cloud_id: t.Optional[str] = None,
@@ -166,6 +167,14 @@ def __init__(
166167
# Internal use only
167168
_transport: t.Optional[AsyncTransport] = None,
168169
) -> None:
170+
if host is not None and hosts is not None:
171+
raise ValueError("Can't specify both 'host' and 'hosts'")
172+
173+
if hosts is not None:
174+
if len(hosts) > 1:
175+
raise ValueError("Can't specify more than one host in 'hosts'")
176+
host = hosts[0]
177+
169178
if host is None and cloud_id is None and _transport is None:
170179
raise ValueError("Either 'host' or 'cloud_id' must be specified")
171180

elasticsearch_serverless/_sync/client/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Elasticsearch(BaseClient):
121121
def __init__(
122122
self,
123123
host: t.Optional[_TYPE_HOST] = None,
124+
hosts: t.Optional[t.List[_TYPE_HOST]] = None,
124125
*,
125126
# API
126127
cloud_id: t.Optional[str] = None,
@@ -166,6 +167,14 @@ def __init__(
166167
# Internal use only
167168
_transport: t.Optional[Transport] = None,
168169
) -> None:
170+
if host is not None and hosts is not None:
171+
raise ValueError("Can't specify both 'host' and 'hosts'")
172+
173+
if hosts is not None:
174+
if len(hosts) > 1:
175+
raise ValueError("Can't specify more than one host in 'hosts'")
176+
host = hosts[0]
177+
169178
if host is None and cloud_id is None and _transport is None:
170179
raise ValueError("Either 'host' or 'cloud_id' must be specified")
171180

test_elasticsearch_serverless/test_client/test_options.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,19 @@ def test_options_timeout_parameters(self):
469469
"retry_on_status": (404,),
470470
"retry_on_timeout": True,
471471
}
472+
473+
def test_host_vs_hosts(self):
474+
with pytest.raises(ValueError) as e:
475+
Elasticsearch(
476+
host="http://localhost:9200",
477+
hosts=["http://localhost:9200", "http://localhost:9201"],
478+
transport_class=DummyTransport,
479+
)
480+
assert str(e.value) == "Can't specify both 'host' and 'hosts'"
481+
482+
with pytest.raises(ValueError) as e:
483+
Elasticsearch(
484+
hosts=["http://localhost:9200", "http://localhost:9201"],
485+
transport_class=DummyTransport,
486+
)
487+
assert str(e.value) == "Can't specify more than one host in 'hosts'"

0 commit comments

Comments
 (0)