Skip to content

Commit 7ae903a

Browse files
meili-bors[bot]alallemasanders41
authored
Merge #771
771: Adding a header parameters to the client constructor r=alallema a=alallema To enable and facilitate the integration of a user-agent into all SDKs. [see related issue](meilisearch/integration-guides#150) and specifically for [the docs-scraper integration](meilisearch/docs-scraper#387) a new parameter client_agent is now available in the client constructor. Co-authored-by: alallema <[email protected]> Co-authored-by: Amélie <[email protected]> Co-authored-by: Paul Sanders <[email protected]>
2 parents afe0dcf + 95bd4bc commit 7ae903a

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

meilisearch/_httprequests.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

33
import json
4-
from typing import Any, Callable, Dict, List, Optional, Union
4+
from functools import lru_cache
5+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
56

67
import requests
78

@@ -19,7 +20,7 @@ def __init__(self, config: Config) -> None:
1920
self.config = config
2021
self.headers = {
2122
"Authorization": f"Bearer {self.config.api_key}",
22-
"User-Agent": qualified_version(),
23+
"User-Agent": _build_user_agent(config.client_agents),
2324
}
2425

2526
def send_request(
@@ -107,3 +108,12 @@ def __validate(request: requests.Response) -> Any:
107108
return HttpRequests.__to_json(request)
108109
except requests.exceptions.HTTPError as err:
109110
raise MeilisearchApiError(str(err), request) from err
111+
112+
113+
@lru_cache(maxsize=1)
114+
def _build_user_agent(client_agents: Optional[Tuple[str]] = None) -> str:
115+
user_agent = qualified_version()
116+
if not client_agents:
117+
return user_agent
118+
119+
return f"{user_agent};{';'.join(client_agents)}"

meilisearch/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import hmac
99
import json
1010
import re
11-
from typing import Any, Dict, List, Optional, Union
11+
from typing import Any, Dict, List, Optional, Tuple, Union
1212
from urllib import parse
1313

1414
from meilisearch._httprequests import HttpRequests
@@ -29,7 +29,11 @@ class Client:
2929
"""
3030

3131
def __init__(
32-
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
32+
self,
33+
url: str,
34+
api_key: Optional[str] = None,
35+
timeout: Optional[int] = None,
36+
client_agents: Optional[Tuple[str]] = None,
3337
) -> None:
3438
"""
3539
Parameters
@@ -39,7 +43,7 @@ def __init__(
3943
api_key:
4044
The optional API key for Meilisearch
4145
"""
42-
self.config = Config(url, api_key, timeout=timeout)
46+
self.config = Config(url, api_key, timeout=timeout, client_agents=client_agents)
4347

4448
self.http = HttpRequests(self.config)
4549

meilisearch/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Optional
3+
from typing import Optional, Tuple
44

55

66
class Config:
@@ -35,7 +35,11 @@ class Paths:
3535
swap = "swap-indexes"
3636

3737
def __init__(
38-
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
38+
self,
39+
url: str,
40+
api_key: Optional[str] = None,
41+
timeout: Optional[int] = None,
42+
client_agents: Optional[Tuple[str]] = None,
3943
) -> None:
4044
"""
4145
Parameters
@@ -49,4 +53,5 @@ def __init__(
4953
self.url = url
5054
self.api_key = api_key
5155
self.timeout = timeout
56+
self.client_agents = client_agents
5257
self.paths = self.Paths()

tests/client/test_http_requests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ def test_get_headers_from_http_requests_instance():
1111

1212
assert http.headers["Authorization"] == f"Bearer {MASTER_KEY}"
1313
assert http.headers["User-Agent"] == qualified_version()
14+
15+
16+
def test_get_headers_with_multiple_user_agent():
17+
"""Tests getting defined headers from instance in HttpRequests."""
18+
config = Config(
19+
BASE_URL,
20+
MASTER_KEY,
21+
timeout=None,
22+
client_agents=("Meilisearch Package1 (v1.1.1)", "Meilisearch Package2 (v2.2.2)"),
23+
)
24+
http = HttpRequests(config=config)
25+
26+
assert http.headers["Authorization"] == f"Bearer {MASTER_KEY}"
27+
assert (
28+
http.headers["User-Agent"]
29+
== qualified_version() + ";Meilisearch Package1 (v1.1.1);Meilisearch Package2 (v2.2.2)"
30+
)

0 commit comments

Comments
 (0)