Skip to content

Commit 33e5bd9

Browse files
committed
Allow custom headers in requests
Changes mainly based on sanders41/meilisearch-python-sdk#1013 The repo is quite a bit different from then, though.
1 parent a43baec commit 33e5bd9

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

meilisearch/_httprequests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

1818

1919
class HttpRequests:
20-
def __init__(self, config: Config) -> None:
20+
def __init__(self, config: Config, custom_headers: Optional[Mapping[str, str]] = None) -> None:
2121
self.config = config
2222
self.headers = {
2323
"Authorization": f"Bearer {self.config.api_key}",
2424
"User-Agent": _build_user_agent(config.client_agents),
2525
}
2626

27+
if custom_headers is not None:
28+
self.headers.update(custom_headers)
29+
2730
def send_request(
2831
self,
2932
http_method: Callable,

meilisearch/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
api_key: Optional[str] = None,
3535
timeout: Optional[int] = None,
3636
client_agents: Optional[Tuple[str, ...]] = None,
37+
custom_headers: Optional[Mapping[str, str]] = None,
3738
) -> None:
3839
"""
3940
Parameters
@@ -48,10 +49,13 @@ def __init__(
4849
client_agents (optional):
4950
Used to send additional client agent information for clients extending the functionality
5051
of this client.
52+
custom_headers (optional):
53+
Custom headers to add when sending data to Meilisearch.
5154
"""
55+
5256
self.config = Config(url, api_key, timeout=timeout, client_agents=client_agents)
5357

54-
self.http = HttpRequests(self.config)
58+
self.http = HttpRequests(self.config, custom_headers)
5559

5660
self.task_handler = TaskHandler(self.config)
5761

tests/client/test_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pylint: disable=invalid-name
22

3+
import pytest
4+
35
import meilisearch
46
from tests import BASE_URL, MASTER_KEY
57

@@ -26,3 +28,33 @@ def test_client_timeout_not_set():
2628
response = client.health()
2729
assert client.config.timeout == default_timeout
2830
assert response["status"] == "available"
31+
32+
33+
@pytest.mark.parametrize(
34+
"api_key, custom_headers, expected",
35+
(
36+
("testKey", None, {"Authorization": "Bearer testKey"}),
37+
(
38+
"testKey",
39+
{"header_key_1": "header_value_1", "header_key_2": "header_value_2"},
40+
{
41+
"Authorization": "Bearer testKey",
42+
"header_key_1": "header_value_1",
43+
"header_key_2": "header_value_2",
44+
},
45+
),
46+
(
47+
None,
48+
{"header_key_1": "header_value_1", "header_key_2": "header_value_2"},
49+
{
50+
"header_key_1": "header_value_1",
51+
"header_key_2": "header_value_2",
52+
},
53+
),
54+
(None, None, {}),
55+
),
56+
)
57+
def test_headers(api_key, custom_headers, expected):
58+
client = meilisearch.Client("127.0.0.1:7700", api_key=api_key, custom_headers=custom_headers)
59+
60+
assert client.http.headers.items() >= expected.items()

0 commit comments

Comments
 (0)