Skip to content

Commit 8ebfe01

Browse files
authored
Set Elastic-Api-Version header (#26)
1 parent 9a3fefd commit 8ebfe01

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

elasticsearch_serverless/_sync/client/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
# Default User-Agent used by the client
6262
USER_AGENT = create_user_agent("elasticsearch-py", __versionstr__)
63+
ELASTIC_API_VERSION = "2023-10-31"
6364

6465
_TYPE_HOST = Union[str, Mapping[str, Union[str, int]], NodeConfig]
6566

@@ -97,6 +98,8 @@ def client_node_config(
9798
# Set the 'User-Agent' default header.
9899
headers = HttpHeaders(node_options.pop("headers", ()))
99100
headers.setdefault("user-agent", USER_AGENT)
101+
headers.setdefault("elastic-api-version", ELASTIC_API_VERSION)
102+
100103
node_options["headers"] = headers
101104

102105
# If a custom Requests AuthBase is passed we set that via '_extras'.
@@ -112,6 +115,8 @@ def client_node_config(
112115
headers.update(headers_to_add)
113116

114117
headers.setdefault("user-agent", USER_AGENT)
118+
headers.setdefault("elastic-api-version", ELASTIC_API_VERSION)
119+
115120
headers.freeze()
116121
node_options["headers"] = headers
117122

test_elasticsearch_serverless/test_client/test_options.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from elastic_transport.client_utils import DEFAULT
2121

2222
from elasticsearch_serverless import AsyncElasticsearch, Elasticsearch
23-
from elasticsearch_serverless._sync.client.utils import USER_AGENT
23+
from elasticsearch_serverless._sync.client.utils import ELASTIC_API_VERSION, USER_AGENT
2424
from test_elasticsearch_serverless.test_cases import (
2525
DummyAsyncTransport,
2626
DummyTransport,
@@ -279,7 +279,10 @@ def test_default_node_configs(self):
279279
assert node_config.host == "localhost"
280280
assert node_config.port == 9200
281281
assert node_config.path_prefix == ""
282-
assert node_config.headers == {"user-agent": USER_AGENT}
282+
assert node_config.headers == {
283+
"user-agent": USER_AGENT,
284+
"elastic-api-version": ELASTIC_API_VERSION,
285+
}
283286

284287
def test_http_headers_overrides(self):
285288
client = Elasticsearch(
@@ -324,6 +327,7 @@ def test_http_headers_overrides(self):
324327
assert node_config.headers == {
325328
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
326329
"user-agent": USER_AGENT,
330+
"elastic-api-version": ELASTIC_API_VERSION,
327331
}
328332
assert client._headers == {"key": "val"}
329333

test_elasticsearch_serverless/test_transport.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from elastic_transport.client_utils import DEFAULT
2626

2727
from elasticsearch_serverless import Elasticsearch, __versionstr__
28+
from elasticsearch_serverless._sync.client import utils
2829
from elasticsearch_serverless.exceptions import (
2930
ApiError,
3031
ConnectionError,
@@ -189,8 +190,9 @@ def test_custom_user_agent_on_initialization(self):
189190
"http://localhost:9200", headers={"user-agent": "custom/1.2.3"}
190191
)
191192
headers = [node.config for node in client.transport.node_pool.all()][0].headers
192-
assert list(headers.keys()) == ["user-agent"]
193+
assert list(headers.keys()) == ["user-agent", "elastic-api-version"]
193194
assert headers["user-agent"].startswith(f"elasticsearch-py/{__versionstr__} (")
195+
assert headers["elastic-api-version"] == utils.ELASTIC_API_VERSION
194196

195197
def test_request_with_custom_user_agent_header(self):
196198
client = Elasticsearch(

0 commit comments

Comments
 (0)