Skip to content

Commit 87cac81

Browse files
authored
Merge pull request #483 from sanders41/user-agent
Add user agent
2 parents 1909231 + 4322a9d commit 87cac81

File tree

6 files changed

+377
-341
lines changed

6 files changed

+377
-341
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from meilisearch_python_async._version import get_version
12
from meilisearch_python_async.client import Client
23

4+
__version__ = get_version()
5+
6+
37
__all__ = ["Client"]

meilisearch_python_async/_http_requests.py

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

3+
from functools import lru_cache
34
from typing import Any, Callable
45

56
from httpx import (
@@ -11,6 +12,7 @@
1112
Response,
1213
)
1314

15+
from meilisearch_python_async._version import get_version
1416
from meilisearch_python_async.errors import (
1517
MeilisearchApiError,
1618
MeilisearchCommunicationError,
@@ -27,19 +29,18 @@ async def _send_request(
2729
http_method: Callable,
2830
path: str,
2931
body: Any | None = None,
30-
content_type: str = "applicaiton/json",
32+
content_type: str = "applicaton/json",
3133
) -> Response:
34+
headers = {"user-agent": user_agent()}
3235
try:
3336
if not body:
3437
response = await http_method(path)
3538
elif content_type != "application/json":
36-
response = await http_method(
37-
path, content=body, headers={"Content-Type": content_type}
38-
)
39+
headers["Content-Type"] = content_type
40+
response = await http_method(path, content=body, headers=headers)
3941
else:
40-
response = await http_method(
41-
path, json=body, headers={"Content-Type": content_type}
42-
)
42+
headers["Content-Type"] = content_type
43+
response = await http_method(path, json=body, headers=headers)
4344

4445
response.raise_for_status()
4546
return response
@@ -73,3 +74,8 @@ async def put(
7374

7475
async def delete(self, path: str, body: dict | None = None) -> Response:
7576
return await self._send_request(self.http_client.delete, path, body)
77+
78+
79+
@lru_cache(maxsize=1)
80+
def user_agent() -> str:
81+
return f"Meilisearch Python Async (v{get_version()})"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from functools import lru_cache
4+
from pathlib import Path
5+
6+
try:
7+
import tomli as tomllib # type: ignore
8+
except ModuleNotFoundError:
9+
import tomllib # type: ignore
10+
11+
12+
@lru_cache(maxsize=1)
13+
def get_version() -> str:
14+
pyproject = Path().absolute() / "pyproject.toml"
15+
with open(pyproject, "rb") as f:
16+
data = tomllib.load(f)
17+
return data["tool"]["poetry"]["version"]

0 commit comments

Comments
 (0)