Skip to content

Commit 84ce3bc

Browse files
committed
Fix headers handling
1 parent 428203a commit 84ce3bc

File tree

5 files changed

+32
-36
lines changed

5 files changed

+32
-36
lines changed

elasticsearch/_async/client/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
BaseNode,
2626
BinaryApiResponse,
2727
HeadApiResponse,
28-
HttpHeaders,
2928
NodeConfig,
3029
NodePool,
3130
NodeSelector,
@@ -226,18 +225,6 @@ def __init__(
226225
):
227226
sniff_callback = default_sniff_callback
228227

229-
headers = HttpHeaders()
230-
if headers is not DEFAULT and headers is not None:
231-
headers.update(headers)
232-
if opaque_id is not DEFAULT and opaque_id is not None: # type: ignore[comparison-overlap]
233-
headers["x-opaque-id"] = opaque_id
234-
headers = resolve_auth_headers(
235-
headers,
236-
api_key=api_key,
237-
basic_auth=basic_auth,
238-
bearer_auth=bearer_auth,
239-
)
240-
241228
if _transport is None:
242229
node_configs = client_node_configs(
243230
hosts,
@@ -309,7 +296,7 @@ def __init__(
309296
**transport_kwargs,
310297
)
311298

312-
self._base_client = BaseClient(_transport, headers=headers)
299+
self._base_client = BaseClient(_transport)
313300

314301
# These are set per-request so are stored separately.
315302
self._base_client._request_timeout = request_timeout
@@ -320,7 +307,18 @@ def __init__(
320307
self._base_client._retry_on_status = retry_on_status
321308

322309
else:
323-
self._base_client = BaseClient(_transport, headers=headers)
310+
self._base_client = BaseClient(_transport)
311+
312+
if headers is not DEFAULT and headers is not None:
313+
self._base_client._headers.update(headers)
314+
if opaque_id is not DEFAULT and opaque_id is not None: # type: ignore[comparison-overlap]
315+
self._base_client._headers["x-opaque-id"] = opaque_id
316+
self._base_client._headers = resolve_auth_headers(
317+
self._base_client._headers,
318+
api_key=api_key,
319+
basic_auth=basic_auth,
320+
bearer_auth=bearer_auth,
321+
)
324322

325323
# namespaced clients for compatibility with API names
326324
self.async_search = AsyncSearchClient(self._base_client)

elasticsearch/_async/client/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def _default_sniffed_node_callback(
210210

211211

212212
class BaseClient:
213-
def __init__(self, _transport: AsyncTransport, headers: HttpHeaders) -> None:
213+
def __init__(self, _transport: AsyncTransport) -> None:
214214
self._transport = _transport
215215
self._client_meta: Union[DefaultType, Tuple[Tuple[str, str], ...]] = DEFAULT
216-
self._headers = headers
216+
self._headers = HttpHeaders()
217217
self._request_timeout: Union[DefaultType, Optional[float]] = DEFAULT
218218
self._ignore_status: Union[DefaultType, Collection[int]] = DEFAULT
219219
self._max_retries: Union[DefaultType, int] = DEFAULT

elasticsearch/_sync/client/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
BaseNode,
2525
BinaryApiResponse,
2626
HeadApiResponse,
27-
HttpHeaders,
2827
NodeConfig,
2928
NodePool,
3029
NodeSelector,
@@ -226,18 +225,6 @@ def __init__(
226225
):
227226
sniff_callback = default_sniff_callback
228227

229-
headers = HttpHeaders()
230-
if headers is not DEFAULT and headers is not None:
231-
headers.update(headers)
232-
if opaque_id is not DEFAULT and opaque_id is not None: # type: ignore[comparison-overlap]
233-
headers["x-opaque-id"] = opaque_id
234-
headers = resolve_auth_headers(
235-
headers,
236-
api_key=api_key,
237-
basic_auth=basic_auth,
238-
bearer_auth=bearer_auth,
239-
)
240-
241228
if _transport is None:
242229
node_configs = client_node_configs(
243230
hosts,
@@ -309,7 +296,7 @@ def __init__(
309296
**transport_kwargs,
310297
)
311298

312-
self._base_client = BaseClient(_transport, headers=headers)
299+
self._base_client = BaseClient(_transport)
313300

314301
# These are set per-request so are stored separately.
315302
self._base_client._request_timeout = request_timeout
@@ -320,7 +307,18 @@ def __init__(
320307
self._base_client._retry_on_status = retry_on_status
321308

322309
else:
323-
self._base_client = BaseClient(_transport, headers=headers)
310+
self._base_client = BaseClient(_transport)
311+
312+
if headers is not DEFAULT and headers is not None:
313+
self._base_client._headers.update(headers)
314+
if opaque_id is not DEFAULT and opaque_id is not None: # type: ignore[comparison-overlap]
315+
self._base_client._headers["x-opaque-id"] = opaque_id
316+
self._base_client._headers = resolve_auth_headers(
317+
self._base_client._headers,
318+
api_key=api_key,
319+
basic_auth=basic_auth,
320+
bearer_auth=bearer_auth,
321+
)
324322

325323
# namespaced clients for compatibility with API names
326324
self.async_search = AsyncSearchClient(self._base_client)

elasticsearch/_sync/client/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def _default_sniffed_node_callback(
210210

211211

212212
class BaseClient:
213-
def __init__(self, _transport: Transport, headers: HttpHeaders) -> None:
213+
def __init__(self, _transport: Transport) -> None:
214214
self._transport = _transport
215215
self._client_meta: Union[DefaultType, Tuple[Tuple[str, str], ...]] = DEFAULT
216-
self._headers = headers
216+
self._headers = HttpHeaders()
217217
self._request_timeout: Union[DefaultType, Optional[float]] = DEFAULT
218218
self._ignore_status: Union[DefaultType, Collection[int]] = DEFAULT
219219
self._max_retries: Union[DefaultType, int] = DEFAULT

test_elasticsearch/test_client/test_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def test_default_node_configs(self):
290290
headers={"key": "val"},
291291
basic_auth=("username", "password"),
292292
)
293-
assert client._headers == {
293+
assert client._base_client._headers == {
294294
"key": "val",
295295
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
296296
}
@@ -347,7 +347,7 @@ def test_http_headers_overrides(self):
347347
"authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
348348
"user-agent": USER_AGENT,
349349
}
350-
assert client._headers == {"key": "val"}
350+
assert client._base_client._headers == {"key": "val"}
351351

352352
def test_user_agent_override(self):
353353
client = Elasticsearch(

0 commit comments

Comments
 (0)