Add support for Elasticsearch api versioning header
Originally added in Elasticsearch in commit2c5f1d1569, removed in OpenSearch in3eac282c57Note that this just adds support of 'elasticsearch' header, there's no 'opensearch' header support as such. Signed-off-by: Rushi Agrawal <[email protected]>
This commit is contained in:
@@ -28,6 +28,7 @@ import binascii
|
||||
import gzip
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import warnings
|
||||
from platform import python_version
|
||||
@@ -127,6 +128,11 @@ class Connection(object):
|
||||
if opaque_id:
|
||||
self.headers["x-opaque-id"] = opaque_id
|
||||
|
||||
if os.getenv("ELASTIC_CLIENT_APIVERSIONING") == "1":
|
||||
self.headers.setdefault(
|
||||
"accept", "application/vnd.elasticsearch+json;compatible-with=7"
|
||||
)
|
||||
|
||||
self.headers.setdefault("content-type", "application/json")
|
||||
self.headers.setdefault("user-agent", self._get_default_user_agent())
|
||||
|
||||
|
||||
@@ -163,6 +163,11 @@ class Deserializer(object):
|
||||
if not mimetype:
|
||||
deserializer = self.default
|
||||
else:
|
||||
# Treat 'application/vnd.elasticsearch+json'
|
||||
# as application/json for compatibility.
|
||||
if mimetype == "application/vnd.elasticsearch+json":
|
||||
mimetype = "application/json"
|
||||
|
||||
# split out charset
|
||||
mimetype, _, _ = mimetype.partition(";")
|
||||
try:
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
import gzip
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import ssl
|
||||
import warnings
|
||||
@@ -173,6 +174,26 @@ class TestBaseConnection(TestCase):
|
||||
conn = Connection(**kwargs)
|
||||
assert conn.host == expected_host
|
||||
|
||||
def test_compatibility_accept_header(self):
|
||||
try:
|
||||
conn = Connection()
|
||||
assert "accept" not in conn.headers
|
||||
|
||||
os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "0"
|
||||
|
||||
conn = Connection()
|
||||
assert "accept" not in conn.headers
|
||||
|
||||
os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "1"
|
||||
|
||||
conn = Connection()
|
||||
assert (
|
||||
conn.headers["accept"]
|
||||
== "application/vnd.elasticsearch+json;compatible-with=7"
|
||||
)
|
||||
finally:
|
||||
os.environ.pop("ELASTIC_CLIENT_APIVERSIONING")
|
||||
|
||||
|
||||
class TestUrllib3Connection(TestCase):
|
||||
def _get_mock_connection(self, connection_params={}, response_body=b"{}"):
|
||||
|
||||
Reference in New Issue
Block a user