Skip to content

Allow custom headers in requests #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@


class HttpRequests:
def __init__(self, config: Config) -> None:
def __init__(self, config: Config, custom_headers: Optional[Mapping[str, str]] = None) -> None:
self.config = config
self.headers = {
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": _build_user_agent(config.client_agents),
}

if custom_headers is not None:
self.headers.update(custom_headers)

def send_request(
self,
http_method: Callable,
Expand Down
6 changes: 5 additions & 1 deletion meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
api_key: Optional[str] = None,
timeout: Optional[int] = None,
client_agents: Optional[Tuple[str, ...]] = None,
custom_headers: Optional[Mapping[str, str]] = None,
) -> None:
"""
Parameters
Expand All @@ -48,10 +49,13 @@ def __init__(
client_agents (optional):
Used to send additional client agent information for clients extending the functionality
of this client.
custom_headers (optional):
Custom headers to add when sending data to Meilisearch.
"""

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

self.http = HttpRequests(self.config)
self.http = HttpRequests(self.config, custom_headers)

self.task_handler = TaskHandler(self.config)

Expand Down
32 changes: 32 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pylint: disable=invalid-name

import pytest

import meilisearch
from tests import BASE_URL, MASTER_KEY

Expand All @@ -26,3 +28,33 @@ def test_client_timeout_not_set():
response = client.health()
assert client.config.timeout == default_timeout
assert response["status"] == "available"


@pytest.mark.parametrize(
"api_key, custom_headers, expected",
(
("testKey", None, {"Authorization": "Bearer testKey"}),
(
"testKey",
{"header_key_1": "header_value_1", "header_key_2": "header_value_2"},
{
"Authorization": "Bearer testKey",
"header_key_1": "header_value_1",
"header_key_2": "header_value_2",
},
),
(
None,
{"header_key_1": "header_value_1", "header_key_2": "header_value_2"},
{
"header_key_1": "header_value_1",
"header_key_2": "header_value_2",
},
),
(None, None, {}),
),
)
def test_headers(api_key, custom_headers, expected):
client = meilisearch.Client("127.0.0.1:7700", api_key=api_key, custom_headers=custom_headers)

assert client.http.headers.items() >= expected.items()