Skip to content

Commit 86a89b6

Browse files
committed
Adding _build_user_agent method
1 parent d403503 commit 86a89b6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

meilisearch/_httprequests.py

Lines changed: 11 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": _build_user_agent(config.client_agents),
23+
"User-Agent": self._build_user_agent(config.client_agents),
2324
}
2425

2526
def send_request(
@@ -107,3 +108,11 @@ 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+
@lru_cache(maxsize=1)
113+
def _build_user_agent(client_agents: Optional[Tuple[str]] = None) -> str:
114+
user_agent = qualified_version()
115+
if not client_agents:
116+
return user_agent
117+
118+
return f"{user_agent};{';'.join(client_agents)}"

0 commit comments

Comments
 (0)