From 1810918e433c0551b808ffd67b56bee49538f937 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Sun, 26 Jul 2026 09:22:50 +0200 Subject: [PATCH] move headers to function --- .../main/inse/integrations/elasticsearch.py | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index afc08614..83532750 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -110,6 +110,10 @@ def _index_to_cluster(index: ESIndex) -> Optional[ESCluster]: return None +def _headers(api_key: str, content_type: str = "application/json") -> Dict[str, str]: + return {"Authorization": f"ApiKey {api_key}", "Content-Type": content_type} + + def _wrap_exception(e: Exception) -> Exception: logger.exception(f"Elasticsearch raised an exception: {e}") if isinstance(e, httpx.HTTPStatusError): @@ -177,8 +181,6 @@ async def search( elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} - payload: Dict[str, Any] = {} if body is not None: @@ -221,7 +223,7 @@ async def search( try: response = await client.post( - f"{elasticsearch_url}/{index_str}/_search", json=payload, headers=headers + f"{elasticsearch_url}/{index_str}/_search", json=payload, headers=_headers(api_key) ) response.raise_for_status() return response.json() @@ -234,14 +236,13 @@ async def open_point_in_time( ) -> str: """Opens a point-in-time on the given index and returns its id.""" elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} client = get_client() try: response = await client.post( f"{elasticsearch_url}/{index}/_pit", params={"keep_alive": keep_alive}, - headers=headers, + headers=_headers(api_key), ) response.raise_for_status() return response.json()["id"] @@ -252,7 +253,6 @@ async def open_point_in_time( async def close_point_in_time(pit_id: str, cluster: str = "nes") -> None: """Closes a previously opened point-in-time.""" elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} client = get_client() try: @@ -260,7 +260,7 @@ async def close_point_in_time(pit_id: str, cluster: str = "nes") -> None: "DELETE", f"{elasticsearch_url}/_pit", json={"id": pit_id}, - headers=headers, + headers=_headers(api_key), ) response.raise_for_status() except Exception as exc: @@ -281,7 +281,6 @@ async def scan( ``index.max_result_window``, so it can stream result sets larger than 10k. """ elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} pit_id = await open_point_in_time(index, keep_alive=keep_alive, cluster=cluster) search_after: Optional[List] = None @@ -301,7 +300,7 @@ async def scan( try: response = await client.post( - f"{elasticsearch_url}/_search", json=payload, headers=headers + f"{elasticsearch_url}/_search", json=payload, headers=_headers(api_key) ) response.raise_for_status() except Exception as exc: @@ -340,7 +339,6 @@ async def get( elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} params = {} if source is not None: @@ -349,7 +347,7 @@ async def get( client = get_client() try: response = await client.get( - f"{elasticsearch_url}/{index}/_doc/{doc_id}", params=params, headers=headers + f"{elasticsearch_url}/{index}/_doc/{doc_id}", params=params, headers=_headers(api_key) ) response.raise_for_status() return response.json() @@ -364,14 +362,13 @@ async def update( cluster: ESCluster = ESCluster.NES, ) -> Dict[str, Any]: elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} client = get_client() try: response = await client.post( f"{elasticsearch_url}/{index}/_update/{doc_id}", json={"doc": doc}, - headers=headers, + headers=_headers(api_key), ) response.raise_for_status() return response.json() @@ -386,14 +383,13 @@ async def index_doc( cluster: ESCluster = ESCluster.NES, ) -> Dict[str, Any]: elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} client = get_client() try: response = await client.put( f"{elasticsearch_url}/{index}/_doc/{doc_id}", json=document, - headers=headers, + headers=_headers(api_key), ) response.raise_for_status() return response.json() @@ -432,7 +428,6 @@ async def count( cluster = clusters.pop() elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/json"} if isinstance(index, list): index_str = ",".join(str(idx) for idx in index) @@ -443,7 +438,7 @@ async def count( client = get_client() try: response = await client.post( - f"{elasticsearch_url}/{index_str}/_count", json=payload, headers=headers + f"{elasticsearch_url}/{index_str}/_count", json=payload, headers=_headers(api_key) ) response.raise_for_status() return response.json().get("count", 0) @@ -474,7 +469,7 @@ async def msearch( cluster = c elasticsearch_url, api_key = _cluster_credentials(cluster) - headers = {"Authorization": f"ApiKey {api_key}", "Content-Type": "application/x-ndjson"} + headers = _headers(api_key, content_type="application/x-ndjson") lines = [] for idx, body in searches: @@ -493,7 +488,7 @@ async def msearch( response = await client.post( f"{elasticsearch_url}/_msearch", content=ndjson_payload.encode("utf-8"), - headers=headers, + headers=_headers(api_key), ) response.raise_for_status() res_json = response.json()