From 5cd40041b2f1a09c597f98e145132fb7cf2fe721 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Thu, 16 Nov 2023 17:09:47 +0400 Subject: [PATCH] Switch to Accept: application/json --- .../_async/client/_base.py | 16 -------- .../_sync/client/_base.py | 16 -------- .../test_async/test_transport.py | 4 +- .../test_client/test_options.py | 34 ++++++++--------- .../test_client/test_serializers.py | 12 +++--- .../test_serializer.py | 38 +++++++------------ .../test_transport.py | 4 +- 7 files changed, 40 insertions(+), 84 deletions(-) diff --git a/elasticsearch_serverless/_async/client/_base.py b/elasticsearch_serverless/_async/client/_base.py index 8b9bbd1..db8c307 100644 --- a/elasticsearch_serverless/_async/client/_base.py +++ b/elasticsearch_serverless/_async/client/_base.py @@ -41,9 +41,6 @@ from .utils import _base64_auth_header, _quote_query _WARNING_RE = re.compile(r"\"([^\"]*)\"") -_COMPAT_MIMETYPE_TEMPLATE = "application/vnd.elasticsearch+%s; compatible-with=8" -_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)") -_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",) def resolve_auth_headers( @@ -148,19 +145,6 @@ async def perform_request( else: request_headers = self._headers - def mimetype_header_to_compat(header: str) -> None: - # Converts all parts of a Accept/Content-Type headers - # from application/X -> application/vnd.elasticsearch+X - nonlocal request_headers - mimetype = request_headers.get(header, None) - if mimetype: - request_headers[header] = _COMPAT_MIMETYPE_RE.sub( - _COMPAT_MIMETYPE_SUB, mimetype - ) - - mimetype_header_to_compat("Accept") - mimetype_header_to_compat("Content-Type") - if params: target = f"{path}?{_quote_query(params)}" else: diff --git a/elasticsearch_serverless/_sync/client/_base.py b/elasticsearch_serverless/_sync/client/_base.py index 0f83ae3..0331b51 100644 --- a/elasticsearch_serverless/_sync/client/_base.py +++ b/elasticsearch_serverless/_sync/client/_base.py @@ -41,9 +41,6 @@ from .utils import _base64_auth_header, _quote_query _WARNING_RE = re.compile(r"\"([^\"]*)\"") -_COMPAT_MIMETYPE_TEMPLATE = "application/vnd.elasticsearch+%s; compatible-with=8" -_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)") -_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",) def resolve_auth_headers( @@ -148,19 +145,6 @@ def perform_request( else: request_headers = self._headers - def mimetype_header_to_compat(header: str) -> None: - # Converts all parts of a Accept/Content-Type headers - # from application/X -> application/vnd.elasticsearch+X - nonlocal request_headers - mimetype = request_headers.get(header, None) - if mimetype: - request_headers[header] = _COMPAT_MIMETYPE_RE.sub( - _COMPAT_MIMETYPE_SUB, mimetype - ) - - mimetype_header_to_compat("Accept") - mimetype_header_to_compat("Content-Type") - if params: target = f"{path}?{_quote_query(params)}" else: diff --git a/test_elasticsearch_serverless/test_async/test_transport.py b/test_elasticsearch_serverless/test_async/test_transport.py index 2665630..1764e38 100644 --- a/test_elasticsearch_serverless/test_async/test_transport.py +++ b/test_elasticsearch_serverless/test_async/test_transport.py @@ -243,7 +243,7 @@ async def test_client_meta_header_not_sent(self): calls = client.transport.node_pool.get().calls assert 1 == len(calls) assert calls[0][1]["headers"] == { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } async def test_body_surrogates_replaced_encoded_into_bytes(self): @@ -350,7 +350,7 @@ async def test_unsupported_product_error(headers): { "body": None, "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "request_timeout": DEFAULT, }, diff --git a/test_elasticsearch_serverless/test_client/test_options.py b/test_elasticsearch_serverless/test_client/test_options.py index 7686123..8fa2cbe 100644 --- a/test_elasticsearch_serverless/test_client/test_options.py +++ b/test_elasticsearch_serverless/test_client/test_options.py @@ -139,7 +139,7 @@ def test_options_passed_to_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, } @@ -157,7 +157,7 @@ def test_options_passed_to_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, @@ -182,7 +182,7 @@ def test_options_passed_to_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, @@ -209,7 +209,7 @@ async def test_options_passed_to_async_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, } @@ -227,7 +227,7 @@ async def test_options_passed_to_async_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, @@ -252,7 +252,7 @@ async def test_options_passed_to_async_perform_request(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, @@ -294,7 +294,7 @@ def test_http_headers_overrides(self): assert call["headers"] == { "key": "val", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client.options(headers={"key1": "val"}).indices.get(index="2") @@ -303,7 +303,7 @@ def test_http_headers_overrides(self): assert call["headers"] == { "key": "val", "key1": "val", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client.options(headers={"key": "val2"}).indices.get(index="3") @@ -311,7 +311,7 @@ def test_http_headers_overrides(self): assert call["headers"] == { "key": "val2", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client = Elasticsearch( @@ -338,14 +338,14 @@ def test_user_agent_override(self): call = calls[("GET", "/1")][0] assert call["headers"] == { "user-agent": "custom1", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client.indices.get(index="2", headers={"user-agent": "custom2"}) call = calls[("GET", "/2")][0] assert call["headers"] == { "user-agent": "custom2", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client = Elasticsearch( @@ -359,14 +359,14 @@ def test_user_agent_override(self): call = calls[("GET", "/1")][0] assert call["headers"] == { "user-agent": "custom3", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } client.indices.get(index="2", headers={"user-agent": "custom4"}) call = calls[("GET", "/2")][0] assert call["headers"] == { "user-agent": "custom4", - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } def test_options_timeout_parameters(self): @@ -387,7 +387,7 @@ def test_options_timeout_parameters(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, @@ -416,7 +416,7 @@ def test_options_timeout_parameters(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 2, @@ -440,7 +440,7 @@ def test_options_timeout_parameters(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, } @@ -461,7 +461,7 @@ def test_options_timeout_parameters(self): assert call.pop("client_meta") is DEFAULT assert call == { "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "body": None, "request_timeout": 1, diff --git a/test_elasticsearch_serverless/test_client/test_serializers.py b/test_elasticsearch_serverless/test_client/test_serializers.py index 6115e43..c09237c 100644 --- a/test_elasticsearch_serverless/test_client/test_serializers.py +++ b/test_elasticsearch_serverless/test_client/test_serializers.py @@ -29,30 +29,30 @@ def test_compat_mode_on_by_default(self): self.client.get(index="test0", id="1") assert len(calls) == 1 assert calls[("GET", "/test0/_doc/1")][0]["headers"] == { - "Accept": "application/vnd.elasticsearch+json; compatible-with=8" + "Accept": "application/json" } # Search with body self.client.search(index="test1", query={"match_all": {}}) assert len(calls) == 2 assert calls[("POST", "/test1/_search")][0]["headers"] == { - "Accept": "application/vnd.elasticsearch+json; compatible-with=8", - "Content-Type": "application/vnd.elasticsearch+json; compatible-with=8", + "Accept": "application/json", + "Content-Type": "application/json", } # Search without body self.client.search(index="test2") assert len(calls) == 3 assert calls[("POST", "/test2/_search")][0]["headers"] == { - "Accept": "application/vnd.elasticsearch+json; compatible-with=8", + "Accept": "application/json", } # Bulk uses x-ndjson self.client.bulk(operations=[]) assert len(calls) == 4 assert calls[("PUT", "/_bulk")][0]["headers"] == { - "Accept": "application/vnd.elasticsearch+json; compatible-with=8", - "Content-Type": "application/vnd.elasticsearch+x-ndjson; compatible-with=8", + "accept": "application/json", + "content-type": "application/x-ndjson", } @pytest.mark.parametrize("mime_subtype", ["json", "x-ndjson"]) diff --git a/test_elasticsearch_serverless/test_serializer.py b/test_elasticsearch_serverless/test_serializer.py index 4f24573..ee63cc4 100644 --- a/test_elasticsearch_serverless/test_serializer.py +++ b/test_elasticsearch_serverless/test_serializer.py @@ -194,28 +194,16 @@ def test_deserializes_text_with_correct_ct(self, data): ) def test_deserialize_compatibility_header(self): - for content_type in ( - "application/vnd.elasticsearch+json;compatible-with=7", - "application/vnd.elasticsearch+json; compatible-with=7", - "application/vnd.elasticsearch+json;compatible-with=8", - "application/vnd.elasticsearch+json; compatible-with=8", - ): - assert {"some": "data"} == self.serializers.loads( - '{"some":"data"}', content_type - ) - assert b'{"some":"data"}' == self.serializers.dumps( - '{"some":"data"}', content_type - ) - - for content_type in ( - "application/vnd.elasticsearch+x-ndjson;compatible-with=7", - "application/vnd.elasticsearch+x-ndjson; compatible-with=7", - "application/vnd.elasticsearch+x-ndjson;compatible-with=8", - "application/vnd.elasticsearch+x-ndjson; compatible-with=8", - ): - assert b'{"some":"data"}\n{"some":"data"}\n' == self.serializers.dumps( - ['{"some":"data"}', {"some": "data"}], content_type - ) - assert [{"some": "data"}, {"some": "data"}] == self.serializers.loads( - b'{"some":"data"}\n{"some":"data"}\n', content_type - ) + assert {"some": "data"} == self.serializers.loads( + '{"some":"data"}', "application/json" + ) + assert b'{"some":"data"}' == self.serializers.dumps( + '{"some":"data"}', "application/json" + ) + + assert b'{"some":"data"}\n{"some":"data"}\n' == self.serializers.dumps( + ['{"some":"data"}', {"some": "data"}], "application/x-ndjson" + ) + assert [{"some": "data"}, {"some": "data"}] == self.serializers.loads( + b'{"some":"data"}\n{"some":"data"}\n', "application/x-ndjson" + ) diff --git a/test_elasticsearch_serverless/test_transport.py b/test_elasticsearch_serverless/test_transport.py index 095f6ec..66328ab 100644 --- a/test_elasticsearch_serverless/test_transport.py +++ b/test_elasticsearch_serverless/test_transport.py @@ -253,7 +253,7 @@ def test_client_meta_header_not_sent(self): calls = client.transport.node_pool.get().calls assert 1 == len(calls) assert calls[0][1]["headers"] == { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", } def test_meta_header_type_error(self): @@ -363,7 +363,7 @@ def test_unsupported_product_error(headers): { "body": None, "headers": { - "accept": "application/vnd.elasticsearch+json; compatible-with=8", + "accept": "application/json", }, "request_timeout": DEFAULT, },