Skip to content

Commit 77b9896

Browse files
committed
pr feedback
1 parent bb22d44 commit 77b9896

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

elasticsearch/_async/client/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from elastic_transport.client_utils import DEFAULT, DefaultType
4747

4848
from ..._otel import OpenTelemetry
49-
from ..._version import __versionstr__
49+
from ..._version import __versionstr__, _SERVERLESS_API_VERSION
5050
from ...compat import warn_stacklevel
5151
from ...exceptions import (
5252
HTTP_EXCEPTIONS,
@@ -64,7 +64,6 @@
6464
)
6565
_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)")
6666
_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",)
67-
_SERVERLESS_API_VERSION = "2023-10-31"
6867

6968

7069
def resolve_auth_headers(
@@ -411,6 +410,7 @@ class NamespacedClient(BaseClient):
411410
def __init__(self, client: "BaseClient") -> None:
412411
self._client = client
413412
super().__init__(self._client.transport)
413+
self._is_serverless = self._client._is_serverless
414414

415415
async def perform_request(
416416
self,

elasticsearch/_sync/client/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from elastic_transport.client_utils import DEFAULT, DefaultType
4747

4848
from ..._otel import OpenTelemetry
49-
from ..._version import __versionstr__
49+
from ..._version import __versionstr__, _SERVERLESS_API_VERSION
5050
from ...compat import warn_stacklevel
5151
from ...exceptions import (
5252
HTTP_EXCEPTIONS,
@@ -64,7 +64,6 @@
6464
)
6565
_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)")
6666
_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",)
67-
_SERVERLESS_API_VERSION = "2023-10-31"
6867

6968

7069
def resolve_auth_headers(
@@ -411,6 +410,7 @@ class NamespacedClient(BaseClient):
411410
def __init__(self, client: "BaseClient") -> None:
412411
self._client = client
413412
super().__init__(self._client.transport)
413+
self._is_serverless = self._client._is_serverless
414414

415415
def perform_request(
416416
self,

elasticsearch/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717

1818
__versionstr__ = "9.4.0"
1919
__es_specification_commit__ = "56a97adc5f4990c06976811c9091ec6046894946"
20+
_SERVERLESS_API_VERSION = "2023-10-31"

test_elasticsearch/test_async/test_transport.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from elastic_transport._node import NodeApiResponse
3434
from elastic_transport.client_utils import DEFAULT
3535

36-
from elasticsearch import AsyncElasticsearch
36+
from elasticsearch import AsyncElasticsearch, __versionstr__
37+
from elasticsearch._version import _SERVERLESS_API_VERSION
3738
from elasticsearch.exceptions import (
3839
ApiError,
3940
ConnectionError,
@@ -781,13 +782,14 @@ async def test_default_stack_mode_sends_compat_headers(self):
781782
calls = client.transport.node_pool.get().calls
782783
assert 1 == len(calls)
783784
headers = calls[0][1]["headers"]
785+
compat_version = __versionstr__.partition(".")[0]
784786
assert headers["accept"] == (
785-
"application/vnd.elasticsearch+json; compatible-with=9"
787+
f"application/vnd.elasticsearch+json; compatible-with={compat_version}"
786788
)
787789
assert "elastic-api-version" not in headers
788790

789791
@pytest.mark.anyio
790-
async def test_serverless_mode_sends_api_version_header(self):
792+
async def test_serverless_mode_sends_api_version_header_on_get(self):
791793
client = AsyncElasticsearch(
792794
"http://localhost:9200",
793795
meta_header=False,
@@ -799,11 +801,11 @@ async def test_serverless_mode_sends_api_version_header(self):
799801
calls = client.transport.node_pool.get().calls
800802
assert 1 == len(calls)
801803
headers = calls[0][1]["headers"]
802-
assert headers["elastic-api-version"] == "2023-10-31"
804+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
803805
assert headers["accept"] == "application/json"
804806

805807
@pytest.mark.anyio
806-
async def test_serverless_mode_does_not_send_compat_headers(self):
808+
async def test_serverless_mode_sends_plain_headers_on_post(self):
807809
client = AsyncElasticsearch(
808810
"http://localhost:9200",
809811
meta_header=False,
@@ -815,7 +817,7 @@ async def test_serverless_mode_does_not_send_compat_headers(self):
815817
calls = client.transport.node_pool.get().calls
816818
assert 1 == len(calls)
817819
headers = calls[0][1]["headers"]
818-
assert headers["elastic-api-version"] == "2023-10-31"
820+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
819821
assert headers["accept"] == "application/json"
820822
assert headers["content-type"] == "application/json"
821823
assert "compatible-with" not in headers.get("accept", "")
@@ -855,5 +857,16 @@ async def test_serverless_mode_preserved_in_options(self):
855857
calls = client2.transport.node_pool.get().calls
856858
assert 1 == len(calls)
857859
headers = calls[0][1]["headers"]
858-
assert headers["elastic-api-version"] == "2023-10-31"
860+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
859861
assert "compatible-with" not in headers.get("accept", "")
862+
863+
@pytest.mark.anyio
864+
async def test_serverless_mode_inherited_by_namespaces(self):
865+
client = AsyncElasticsearch(
866+
"http://localhost:9200",
867+
meta_header=False,
868+
node_class=DummyNode,
869+
server_mode="serverless",
870+
)
871+
assert client._is_serverless is True
872+
assert client.indices._is_serverless is True

test_elasticsearch/test_transport.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from elastic_transport.client_utils import DEFAULT
3333

3434
from elasticsearch import Elasticsearch, __versionstr__
35+
from elasticsearch._version import _SERVERLESS_API_VERSION
3536
from elasticsearch.exceptions import (
3637
ApiError,
3738
ConnectionError,
@@ -693,12 +694,13 @@ def test_default_stack_mode_sends_compat_headers(self):
693694
calls = client.transport.node_pool.get().calls
694695
assert 1 == len(calls)
695696
headers = calls[0][1]["headers"]
697+
compat_version = __versionstr__.partition(".")[0]
696698
assert headers["accept"] == (
697-
"application/vnd.elasticsearch+json; compatible-with=9"
699+
f"application/vnd.elasticsearch+json; compatible-with={compat_version}"
698700
)
699701
assert "elastic-api-version" not in headers
700702

701-
def test_serverless_mode_sends_api_version_header(self):
703+
def test_serverless_mode_sends_api_version_header_on_get(self):
702704
client = Elasticsearch(
703705
"http://localhost:9200",
704706
meta_header=False,
@@ -710,10 +712,10 @@ def test_serverless_mode_sends_api_version_header(self):
710712
calls = client.transport.node_pool.get().calls
711713
assert 1 == len(calls)
712714
headers = calls[0][1]["headers"]
713-
assert headers["elastic-api-version"] == "2023-10-31"
715+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
714716
assert headers["accept"] == "application/json"
715717

716-
def test_serverless_mode_does_not_send_compat_headers(self):
718+
def test_serverless_mode_sends_plain_headers_on_post(self):
717719
client = Elasticsearch(
718720
"http://localhost:9200",
719721
meta_header=False,
@@ -725,7 +727,7 @@ def test_serverless_mode_does_not_send_compat_headers(self):
725727
calls = client.transport.node_pool.get().calls
726728
assert 1 == len(calls)
727729
headers = calls[0][1]["headers"]
728-
assert headers["elastic-api-version"] == "2023-10-31"
730+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
729731
assert headers["accept"] == "application/json"
730732
assert headers["content-type"] == "application/json"
731733
assert "compatible-with" not in headers.get("accept", "")
@@ -764,5 +766,15 @@ def test_serverless_mode_preserved_in_options(self):
764766
calls = client2.transport.node_pool.get().calls
765767
assert 1 == len(calls)
766768
headers = calls[0][1]["headers"]
767-
assert headers["elastic-api-version"] == "2023-10-31"
769+
assert headers["elastic-api-version"] == _SERVERLESS_API_VERSION
768770
assert "compatible-with" not in headers.get("accept", "")
771+
772+
def test_serverless_mode_inherited_by_namespaces(self):
773+
client = Elasticsearch(
774+
"http://localhost:9200",
775+
meta_header=False,
776+
node_class=DummyNode,
777+
server_mode="serverless",
778+
)
779+
assert client._is_serverless is True
780+
assert client.indices._is_serverless is True

0 commit comments

Comments
 (0)