Skip to content

Set Elastic-Api-Version header #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2023
Merged
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
5 changes: 5 additions & 0 deletions elasticsearch_serverless/_sync/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

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

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

Expand Down Expand Up @@ -97,6 +98,8 @@ def client_node_config(
# Set the 'User-Agent' default header.
headers = HttpHeaders(node_options.pop("headers", ()))
headers.setdefault("user-agent", USER_AGENT)
headers.setdefault("elastic-api-version", ELASTIC_API_VERSION)

node_options["headers"] = headers

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

headers.setdefault("user-agent", USER_AGENT)
headers.setdefault("elastic-api-version", ELASTIC_API_VERSION)

headers.freeze()
node_options["headers"] = headers

Expand Down
8 changes: 6 additions & 2 deletions test_elasticsearch_serverless/test_client/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from elastic_transport.client_utils import DEFAULT

from elasticsearch_serverless import AsyncElasticsearch, Elasticsearch
from elasticsearch_serverless._sync.client.utils import USER_AGENT
from elasticsearch_serverless._sync.client.utils import ELASTIC_API_VERSION, USER_AGENT
from test_elasticsearch_serverless.test_cases import (
DummyAsyncTransport,
DummyTransport,
Expand Down Expand Up @@ -279,7 +279,10 @@ def test_default_node_configs(self):
assert node_config.host == "localhost"
assert node_config.port == 9200
assert node_config.path_prefix == ""
assert node_config.headers == {"user-agent": USER_AGENT}
assert node_config.headers == {
"user-agent": USER_AGENT,
"elastic-api-version": ELASTIC_API_VERSION,
}

def test_http_headers_overrides(self):
client = Elasticsearch(
Expand Down Expand Up @@ -324,6 +327,7 @@ def test_http_headers_overrides(self):
assert node_config.headers == {
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
"user-agent": USER_AGENT,
"elastic-api-version": ELASTIC_API_VERSION,
}
assert client._headers == {"key": "val"}

Expand Down
4 changes: 3 additions & 1 deletion test_elasticsearch_serverless/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from elastic_transport.client_utils import DEFAULT

from elasticsearch_serverless import Elasticsearch, __versionstr__
from elasticsearch_serverless._sync.client import utils
from elasticsearch_serverless.exceptions import (
ApiError,
ConnectionError,
Expand Down Expand Up @@ -189,8 +190,9 @@ def test_custom_user_agent_on_initialization(self):
"http://localhost:9200", headers={"user-agent": "custom/1.2.3"}
)
headers = [node.config for node in client.transport.node_pool.all()][0].headers
assert list(headers.keys()) == ["user-agent"]
assert list(headers.keys()) == ["user-agent", "elastic-api-version"]
assert headers["user-agent"].startswith(f"elasticsearch-py/{__versionstr__} (")
assert headers["elastic-api-version"] == utils.ELASTIC_API_VERSION

def test_request_with_custom_user_agent_header(self):
client = Elasticsearch(
Expand Down