Skip to content

Switch to Accept: application/json #27

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 18, 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
16 changes: 0 additions & 16 deletions elasticsearch_serverless/_async/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 0 additions & 16 deletions elasticsearch_serverless/_sync/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test_elasticsearch_serverless/test_async/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
},
Expand Down
34 changes: 17 additions & 17 deletions test_elasticsearch_serverless/test_client/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -303,15 +303,15 @@ 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")
call = calls[("GET", "/3")][0]

assert call["headers"] == {
"key": "val2",
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
"accept": "application/json",
}

client = Elasticsearch(
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions test_elasticsearch_serverless/test_client/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
38 changes: 13 additions & 25 deletions test_elasticsearch_serverless/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
4 changes: 2 additions & 2 deletions test_elasticsearch_serverless/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
},
Expand Down