meilisearch-python-sdk 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of meilisearch-python-sdk might be problematic. Click here for more details.

@@ -0,0 +1,138 @@
1
+ from __future__ import annotations
2
+
3
+ from functools import lru_cache
4
+ from typing import Any, Callable
5
+
6
+ from httpx import (
7
+ AsyncClient,
8
+ Client,
9
+ ConnectError,
10
+ ConnectTimeout,
11
+ HTTPError,
12
+ RemoteProtocolError,
13
+ Response,
14
+ )
15
+
16
+ from meilisearch_python_sdk._version import VERSION
17
+ from meilisearch_python_sdk.errors import (
18
+ MeilisearchApiError,
19
+ MeilisearchCommunicationError,
20
+ MeilisearchError,
21
+ )
22
+
23
+
24
+ class AsyncHttpRequests:
25
+ def __init__(self, http_client: AsyncClient) -> None:
26
+ self.http_client = http_client
27
+
28
+ async def _send_request(
29
+ self,
30
+ http_method: Callable,
31
+ path: str,
32
+ body: Any | None = None,
33
+ content_type: str = "applicaton/json",
34
+ ) -> Response:
35
+ headers = build_headers(content_type)
36
+ try:
37
+ if not body:
38
+ response = await http_method(path)
39
+ elif content_type == "application/json":
40
+ response = await http_method(path, json=body, headers=headers)
41
+ else:
42
+ response = await http_method(path, content=body, headers=headers)
43
+
44
+ response.raise_for_status()
45
+ return response
46
+
47
+ except (ConnectError, ConnectTimeout, RemoteProtocolError) as err:
48
+ raise MeilisearchCommunicationError(str(err)) from err
49
+ except HTTPError as err:
50
+ if "response" in locals():
51
+ raise MeilisearchApiError(str(err), response) from err
52
+ else:
53
+ # Fail safe just in case error happens before response is created
54
+ raise MeilisearchError(str(err)) # pragma: no cover
55
+
56
+ async def get(self, path: str) -> Response:
57
+ return await self._send_request(self.http_client.get, path)
58
+
59
+ async def patch(
60
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
61
+ ) -> Response:
62
+ return await self._send_request(self.http_client.patch, path, body, content_type)
63
+
64
+ async def post(
65
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
66
+ ) -> Response:
67
+ return await self._send_request(self.http_client.post, path, body, content_type)
68
+
69
+ async def put(
70
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
71
+ ) -> Response:
72
+ return await self._send_request(self.http_client.put, path, body, content_type)
73
+
74
+ async def delete(self, path: str, body: dict | None = None) -> Response:
75
+ return await self._send_request(self.http_client.delete, path, body)
76
+
77
+
78
+ class HttpRequests:
79
+ def __init__(self, http_client: Client) -> None:
80
+ self.http_client = http_client
81
+
82
+ def _send_request(
83
+ self,
84
+ http_method: Callable,
85
+ path: str,
86
+ body: Any | None = None,
87
+ content_type: str = "applicaton/json",
88
+ ) -> Response:
89
+ headers = build_headers(content_type)
90
+ try:
91
+ if not body:
92
+ response = http_method(path)
93
+ elif content_type == "application/json":
94
+ response = http_method(path, json=body, headers=headers)
95
+ else:
96
+ response = http_method(path, content=body, headers=headers)
97
+
98
+ response.raise_for_status()
99
+ return response
100
+
101
+ except (ConnectError, ConnectTimeout, RemoteProtocolError) as err:
102
+ raise MeilisearchCommunicationError(str(err)) from err
103
+ except HTTPError as err:
104
+ if "response" in locals():
105
+ raise MeilisearchApiError(str(err), response) from err
106
+ else:
107
+ # Fail safe just in case error happens before response is created
108
+ raise MeilisearchError(str(err)) # pragma: no cover
109
+
110
+ def get(self, path: str) -> Response:
111
+ return self._send_request(self.http_client.get, path)
112
+
113
+ def patch(
114
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
115
+ ) -> Response:
116
+ return self._send_request(self.http_client.patch, path, body, content_type)
117
+
118
+ def post(
119
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
120
+ ) -> Response:
121
+ return self._send_request(self.http_client.post, path, body, content_type)
122
+
123
+ def put(
124
+ self, path: str, body: Any | None = None, content_type: str = "application/json"
125
+ ) -> Response:
126
+ return self._send_request(self.http_client.put, path, body, content_type)
127
+
128
+ def delete(self, path: str, body: dict | None = None) -> Response:
129
+ return self._send_request(self.http_client.delete, path, body)
130
+
131
+
132
+ def build_headers(content_type: str) -> dict[str, str]:
133
+ return {"user-agent": user_agent(), "Content-Type": content_type}
134
+
135
+
136
+ @lru_cache(maxsize=1)
137
+ def user_agent() -> str:
138
+ return f"Meilisearch Python Async (v{VERSION})"