Skip to content

Commit 9a3fefd

Browse files
authored
Switch to Accept: application/json (#27)
1 parent 745e8e3 commit 9a3fefd

File tree

7 files changed

+40
-84
lines changed

7 files changed

+40
-84
lines changed

elasticsearch_serverless/_async/client/_base.py

-16
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
from .utils import _base64_auth_header, _quote_query
4242

4343
_WARNING_RE = re.compile(r"\"([^\"]*)\"")
44-
_COMPAT_MIMETYPE_TEMPLATE = "application/vnd.elasticsearch+%s; compatible-with=8"
45-
_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)")
46-
_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",)
4744

4845

4946
def resolve_auth_headers(
@@ -148,19 +145,6 @@ async def perform_request(
148145
else:
149146
request_headers = self._headers
150147

151-
def mimetype_header_to_compat(header: str) -> None:
152-
# Converts all parts of a Accept/Content-Type headers
153-
# from application/X -> application/vnd.elasticsearch+X
154-
nonlocal request_headers
155-
mimetype = request_headers.get(header, None)
156-
if mimetype:
157-
request_headers[header] = _COMPAT_MIMETYPE_RE.sub(
158-
_COMPAT_MIMETYPE_SUB, mimetype
159-
)
160-
161-
mimetype_header_to_compat("Accept")
162-
mimetype_header_to_compat("Content-Type")
163-
164148
if params:
165149
target = f"{path}?{_quote_query(params)}"
166150
else:

elasticsearch_serverless/_sync/client/_base.py

-16
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
from .utils import _base64_auth_header, _quote_query
4242

4343
_WARNING_RE = re.compile(r"\"([^\"]*)\"")
44-
_COMPAT_MIMETYPE_TEMPLATE = "application/vnd.elasticsearch+%s; compatible-with=8"
45-
_COMPAT_MIMETYPE_RE = re.compile(r"application/(json|x-ndjson|vnd\.mapbox-vector-tile)")
46-
_COMPAT_MIMETYPE_SUB = _COMPAT_MIMETYPE_TEMPLATE % (r"\g<1>",)
4744

4845

4946
def resolve_auth_headers(
@@ -148,19 +145,6 @@ def perform_request(
148145
else:
149146
request_headers = self._headers
150147

151-
def mimetype_header_to_compat(header: str) -> None:
152-
# Converts all parts of a Accept/Content-Type headers
153-
# from application/X -> application/vnd.elasticsearch+X
154-
nonlocal request_headers
155-
mimetype = request_headers.get(header, None)
156-
if mimetype:
157-
request_headers[header] = _COMPAT_MIMETYPE_RE.sub(
158-
_COMPAT_MIMETYPE_SUB, mimetype
159-
)
160-
161-
mimetype_header_to_compat("Accept")
162-
mimetype_header_to_compat("Content-Type")
163-
164148
if params:
165149
target = f"{path}?{_quote_query(params)}"
166150
else:

test_elasticsearch_serverless/test_async/test_transport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ async def test_client_meta_header_not_sent(self):
243243
calls = client.transport.node_pool.get().calls
244244
assert 1 == len(calls)
245245
assert calls[0][1]["headers"] == {
246-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
246+
"accept": "application/json",
247247
}
248248

249249
async def test_body_surrogates_replaced_encoded_into_bytes(self):
@@ -350,7 +350,7 @@ async def test_unsupported_product_error(headers):
350350
{
351351
"body": None,
352352
"headers": {
353-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
353+
"accept": "application/json",
354354
},
355355
"request_timeout": DEFAULT,
356356
},

test_elasticsearch_serverless/test_client/test_options.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_options_passed_to_perform_request(self):
139139
assert call.pop("client_meta") is DEFAULT
140140
assert call == {
141141
"headers": {
142-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
142+
"accept": "application/json",
143143
},
144144
"body": None,
145145
}
@@ -157,7 +157,7 @@ def test_options_passed_to_perform_request(self):
157157
assert call.pop("client_meta") is DEFAULT
158158
assert call == {
159159
"headers": {
160-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
160+
"accept": "application/json",
161161
},
162162
"body": None,
163163
"request_timeout": 1,
@@ -182,7 +182,7 @@ def test_options_passed_to_perform_request(self):
182182
assert call.pop("client_meta") is DEFAULT
183183
assert call == {
184184
"headers": {
185-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
185+
"accept": "application/json",
186186
},
187187
"body": None,
188188
"request_timeout": 1,
@@ -209,7 +209,7 @@ async def test_options_passed_to_async_perform_request(self):
209209
assert call.pop("client_meta") is DEFAULT
210210
assert call == {
211211
"headers": {
212-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
212+
"accept": "application/json",
213213
},
214214
"body": None,
215215
}
@@ -227,7 +227,7 @@ async def test_options_passed_to_async_perform_request(self):
227227
assert call.pop("client_meta") is DEFAULT
228228
assert call == {
229229
"headers": {
230-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
230+
"accept": "application/json",
231231
},
232232
"body": None,
233233
"request_timeout": 1,
@@ -252,7 +252,7 @@ async def test_options_passed_to_async_perform_request(self):
252252
assert call.pop("client_meta") is DEFAULT
253253
assert call == {
254254
"headers": {
255-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
255+
"accept": "application/json",
256256
},
257257
"body": None,
258258
"request_timeout": 1,
@@ -294,7 +294,7 @@ def test_http_headers_overrides(self):
294294

295295
assert call["headers"] == {
296296
"key": "val",
297-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
297+
"accept": "application/json",
298298
}
299299

300300
client.options(headers={"key1": "val"}).indices.get(index="2")
@@ -303,15 +303,15 @@ def test_http_headers_overrides(self):
303303
assert call["headers"] == {
304304
"key": "val",
305305
"key1": "val",
306-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
306+
"accept": "application/json",
307307
}
308308

309309
client.options(headers={"key": "val2"}).indices.get(index="3")
310310
call = calls[("GET", "/3")][0]
311311

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

317317
client = Elasticsearch(
@@ -338,14 +338,14 @@ def test_user_agent_override(self):
338338
call = calls[("GET", "/1")][0]
339339
assert call["headers"] == {
340340
"user-agent": "custom1",
341-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
341+
"accept": "application/json",
342342
}
343343

344344
client.indices.get(index="2", headers={"user-agent": "custom2"})
345345
call = calls[("GET", "/2")][0]
346346
assert call["headers"] == {
347347
"user-agent": "custom2",
348-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
348+
"accept": "application/json",
349349
}
350350

351351
client = Elasticsearch(
@@ -359,14 +359,14 @@ def test_user_agent_override(self):
359359
call = calls[("GET", "/1")][0]
360360
assert call["headers"] == {
361361
"user-agent": "custom3",
362-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
362+
"accept": "application/json",
363363
}
364364

365365
client.indices.get(index="2", headers={"user-agent": "custom4"})
366366
call = calls[("GET", "/2")][0]
367367
assert call["headers"] == {
368368
"user-agent": "custom4",
369-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
369+
"accept": "application/json",
370370
}
371371

372372
def test_options_timeout_parameters(self):
@@ -387,7 +387,7 @@ def test_options_timeout_parameters(self):
387387
assert call.pop("client_meta") is DEFAULT
388388
assert call == {
389389
"headers": {
390-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
390+
"accept": "application/json",
391391
},
392392
"body": None,
393393
"request_timeout": 1,
@@ -416,7 +416,7 @@ def test_options_timeout_parameters(self):
416416
assert call.pop("client_meta") is DEFAULT
417417
assert call == {
418418
"headers": {
419-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
419+
"accept": "application/json",
420420
},
421421
"body": None,
422422
"request_timeout": 2,
@@ -440,7 +440,7 @@ def test_options_timeout_parameters(self):
440440
assert call.pop("client_meta") is DEFAULT
441441
assert call == {
442442
"headers": {
443-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
443+
"accept": "application/json",
444444
},
445445
"body": None,
446446
}
@@ -461,7 +461,7 @@ def test_options_timeout_parameters(self):
461461
assert call.pop("client_meta") is DEFAULT
462462
assert call == {
463463
"headers": {
464-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
464+
"accept": "application/json",
465465
},
466466
"body": None,
467467
"request_timeout": 1,

test_elasticsearch_serverless/test_client/test_serializers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,30 @@ def test_compat_mode_on_by_default(self):
2929
self.client.get(index="test0", id="1")
3030
assert len(calls) == 1
3131
assert calls[("GET", "/test0/_doc/1")][0]["headers"] == {
32-
"Accept": "application/vnd.elasticsearch+json; compatible-with=8"
32+
"Accept": "application/json"
3333
}
3434

3535
# Search with body
3636
self.client.search(index="test1", query={"match_all": {}})
3737
assert len(calls) == 2
3838
assert calls[("POST", "/test1/_search")][0]["headers"] == {
39-
"Accept": "application/vnd.elasticsearch+json; compatible-with=8",
40-
"Content-Type": "application/vnd.elasticsearch+json; compatible-with=8",
39+
"Accept": "application/json",
40+
"Content-Type": "application/json",
4141
}
4242

4343
# Search without body
4444
self.client.search(index="test2")
4545
assert len(calls) == 3
4646
assert calls[("POST", "/test2/_search")][0]["headers"] == {
47-
"Accept": "application/vnd.elasticsearch+json; compatible-with=8",
47+
"Accept": "application/json",
4848
}
4949

5050
# Bulk uses x-ndjson
5151
self.client.bulk(operations=[])
5252
assert len(calls) == 4
5353
assert calls[("PUT", "/_bulk")][0]["headers"] == {
54-
"Accept": "application/vnd.elasticsearch+json; compatible-with=8",
55-
"Content-Type": "application/vnd.elasticsearch+x-ndjson; compatible-with=8",
54+
"accept": "application/json",
55+
"content-type": "application/x-ndjson",
5656
}
5757

5858
@pytest.mark.parametrize("mime_subtype", ["json", "x-ndjson"])

test_elasticsearch_serverless/test_serializer.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,16 @@ def test_deserializes_text_with_correct_ct(self, data):
194194
)
195195

196196
def test_deserialize_compatibility_header(self):
197-
for content_type in (
198-
"application/vnd.elasticsearch+json;compatible-with=7",
199-
"application/vnd.elasticsearch+json; compatible-with=7",
200-
"application/vnd.elasticsearch+json;compatible-with=8",
201-
"application/vnd.elasticsearch+json; compatible-with=8",
202-
):
203-
assert {"some": "data"} == self.serializers.loads(
204-
'{"some":"data"}', content_type
205-
)
206-
assert b'{"some":"data"}' == self.serializers.dumps(
207-
'{"some":"data"}', content_type
208-
)
209-
210-
for content_type in (
211-
"application/vnd.elasticsearch+x-ndjson;compatible-with=7",
212-
"application/vnd.elasticsearch+x-ndjson; compatible-with=7",
213-
"application/vnd.elasticsearch+x-ndjson;compatible-with=8",
214-
"application/vnd.elasticsearch+x-ndjson; compatible-with=8",
215-
):
216-
assert b'{"some":"data"}\n{"some":"data"}\n' == self.serializers.dumps(
217-
['{"some":"data"}', {"some": "data"}], content_type
218-
)
219-
assert [{"some": "data"}, {"some": "data"}] == self.serializers.loads(
220-
b'{"some":"data"}\n{"some":"data"}\n', content_type
221-
)
197+
assert {"some": "data"} == self.serializers.loads(
198+
'{"some":"data"}', "application/json"
199+
)
200+
assert b'{"some":"data"}' == self.serializers.dumps(
201+
'{"some":"data"}', "application/json"
202+
)
203+
204+
assert b'{"some":"data"}\n{"some":"data"}\n' == self.serializers.dumps(
205+
['{"some":"data"}', {"some": "data"}], "application/x-ndjson"
206+
)
207+
assert [{"some": "data"}, {"some": "data"}] == self.serializers.loads(
208+
b'{"some":"data"}\n{"some":"data"}\n', "application/x-ndjson"
209+
)

test_elasticsearch_serverless/test_transport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def test_client_meta_header_not_sent(self):
253253
calls = client.transport.node_pool.get().calls
254254
assert 1 == len(calls)
255255
assert calls[0][1]["headers"] == {
256-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
256+
"accept": "application/json",
257257
}
258258

259259
def test_meta_header_type_error(self):
@@ -363,7 +363,7 @@ def test_unsupported_product_error(headers):
363363
{
364364
"body": None,
365365
"headers": {
366-
"accept": "application/vnd.elasticsearch+json; compatible-with=8",
366+
"accept": "application/json",
367367
},
368368
"request_timeout": DEFAULT,
369369
},

0 commit comments

Comments
 (0)