Add support for Elasticsearch api versioning header

Originally added in Elasticsearch in commit
2c5f1d1569, removed in OpenSearch in
3eac282c57

Note 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:
Rushi Agrawal
2021-08-28 00:23:46 +05:30
parent 1c539d1255
commit ea397e0f92
3 changed files with 32 additions and 0 deletions
+6
View File
@@ -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())
+5
View File
@@ -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:
+21
View File
@@ -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"{}"):