Skip to content

Commit 6e04189

Browse files
authored
Merge pull request #1172 from eun2ce/fix/custom-headers-index
Fix: propagate custom_headers to Index instances
2 parents 23debbd + 3f85693 commit 6e04189

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

meilisearch/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ def __init__(
7474

7575
self.config = Config(url, api_key, timeout=timeout, client_agents=client_agents)
7676

77+
# Store custom headers so they can be propagated to sub-clients (Index, TaskHandler, etc.)
78+
self._custom_headers = custom_headers
79+
7780
self.http = HttpRequests(self.config, custom_headers)
7881

79-
self.task_handler = TaskHandler(self.config)
82+
self.task_handler = TaskHandler(self.config, custom_headers)
8083

8184
def create_index(self, uid: str, options: Optional[Mapping[str, Any]] = None) -> TaskInfo:
8285
"""Create an index.
@@ -99,7 +102,7 @@ def create_index(self, uid: str, options: Optional[Mapping[str, Any]] = None) ->
99102
MeilisearchApiError
100103
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
101104
"""
102-
return Index.create(self.config, uid, options)
105+
return Index.create(self.config, uid, options, custom_headers=self._custom_headers)
103106

104107
def delete_index(self, uid: str) -> TaskInfo:
105108
"""Deletes an index
@@ -153,6 +156,7 @@ def get_indexes(self, parameters: Optional[Mapping[str, Any]] = None) -> Dict[st
153156
index["primaryKey"],
154157
index["createdAt"],
155158
index["updatedAt"],
159+
custom_headers=self._custom_headers,
156160
)
157161
for index in response["results"]
158162
]
@@ -201,7 +205,7 @@ def get_index(self, uid: str) -> Index:
201205
MeilisearchApiError
202206
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
203207
"""
204-
return Index(self.config, uid).fetch_info()
208+
return Index(self.config, uid, custom_headers=self._custom_headers).fetch_info()
205209

206210
def get_raw_index(self, uid: str) -> Dict[str, Any]:
207211
"""Get the index as a dictionary.
@@ -239,7 +243,7 @@ def index(self, uid: str) -> Index:
239243
An Index instance.
240244
"""
241245
if uid is not None:
242-
return Index(self.config, uid=uid)
246+
return Index(self.config, uid=uid, custom_headers=self._custom_headers)
243247
raise ValueError("The index UID should not be None")
244248

245249
def multi_search(

meilisearch/index.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def __init__(
6666
primary_key: Optional[str] = None,
6767
created_at: Optional[Union[datetime, str]] = None,
6868
updated_at: Optional[Union[datetime, str]] = None,
69+
custom_headers: Optional[Mapping[str, str]] = None,
6970
) -> None:
7071
"""
7172
Parameters
@@ -78,8 +79,8 @@ def __init__(
7879
Primary-key of the index.
7980
"""
8081
self.config = config
81-
self.http = HttpRequests(config)
82-
self.task_handler = TaskHandler(config)
82+
self.http = HttpRequests(config, custom_headers)
83+
self.task_handler = TaskHandler(config, custom_headers)
8384
self.uid = uid
8485
self.primary_key = primary_key
8586
self.created_at = iso_to_date_time(created_at)
@@ -175,7 +176,12 @@ def get_primary_key(self) -> str | None:
175176
return self.fetch_info().primary_key
176177

177178
@staticmethod
178-
def create(config: Config, uid: str, options: Optional[Mapping[str, Any]] = None) -> TaskInfo:
179+
def create(
180+
config: Config,
181+
uid: str,
182+
options: Optional[Mapping[str, Any]] = None,
183+
custom_headers: Optional[Mapping[str, str]] = None,
184+
) -> TaskInfo:
179185
"""Create the index.
180186
181187
Parameters
@@ -199,7 +205,7 @@ def create(config: Config, uid: str, options: Optional[Mapping[str, Any]] = None
199205
if options is None:
200206
options = {}
201207
payload = {**options, "uid": uid}
202-
task = HttpRequests(config).post(config.paths.index, payload)
208+
task = HttpRequests(config, custom_headers).post(config.paths.index, payload)
203209

204210
return TaskInfo(**task)
205211

meilisearch/task.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from datetime import datetime
44
from time import sleep
5-
from typing import Any, MutableMapping, Optional
5+
from typing import Any, Mapping, MutableMapping, Optional
66
from urllib import parse
77

88
from meilisearch._httprequests import HttpRequests
@@ -19,13 +19,13 @@ class TaskHandler:
1919
https://www.meilisearch.com/docs/reference/api/tasks
2020
"""
2121

22-
def __init__(self, config: Config):
22+
def __init__(self, config: Config, custom_headers: Optional[Mapping[str, str]] = None):
2323
"""Parameters
2424
----------
2525
config: Config object containing permission and location of Meilisearch.
2626
"""
2727
self.config = config
28-
self.http = HttpRequests(config)
28+
self.http = HttpRequests(config, custom_headers)
2929

3030
def get_batches(self, parameters: Optional[MutableMapping[str, Any]] = None) -> BatchResults:
3131
"""Get all task batches.

tests/client/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ def test_headers(api_key, custom_headers, expected):
5858
client = meilisearch.Client("127.0.0.1:7700", api_key=api_key, custom_headers=custom_headers)
5959

6060
assert client.http.headers.items() >= expected.items()
61+
62+
63+
def test_index_inherits_custom_headers():
64+
custom_headers = {"header_key_1": "header_value_1", "header_key_2": "header_value_2"}
65+
client = meilisearch.Client("127.0.0.1:7700", api_key=None, custom_headers=custom_headers)
66+
67+
index = client.index("movies")
68+
69+
# Index-level HttpRequests instance should also include the custom headers
70+
assert index.http.headers.items() >= custom_headers.items()

0 commit comments

Comments
 (0)