Skip to content

Commit 7e63986

Browse files
committed
Improving docstrings
1 parent 75a9056 commit 7e63986

File tree

11 files changed

+877
-51
lines changed

11 files changed

+877
-51
lines changed

.flake8

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[flake8]
2-
ignore = E203, E231, E501
3-
max-line-length = 100
2+
ignore = E203, E231, E501, D100, D101, D102, D103, D104, D105, D106, D107, D401

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ repos:
2828
hooks:
2929
- id: flake8
3030
additional_dependencies: [flake8-print]
31-
args: ["--max-line-length=100", "--ignore=E203,E231,E501"]
31+
args: ["--ignore=E203,E231,E501,D100,D101,D102,D103,D104,D105,D106,D107,D401"]

async_search_client/_http_requests.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
Response,
1010
)
1111

12-
from async_search_client.errors import MeiliSearchApiError, MeiliSearchCommunicationError
12+
from async_search_client.errors import (
13+
MeiliSearchApiError,
14+
MeiliSearchCommunicationError,
15+
MeiliSearchTimeoutError,
16+
)
1317

1418

15-
class HttpRequests:
19+
class _HttpRequests:
1620
def __init__(self, http_client: AsyncClient) -> None:
1721
self.http_client = http_client
1822

@@ -36,7 +40,7 @@ async def _send_request(
3640
except ConnectError as err:
3741
raise MeiliSearchCommunicationError(str(err)) from err
3842
except ConnectTimeout as err:
39-
raise MeiliSearchCommunicationError(str(err)) from err
43+
raise MeiliSearchTimeoutError(str(err)) from err
4044
except HTTPError as err:
4145
raise MeiliSearchApiError(str(err), response) from err
4246

async_search_client/client.py

Lines changed: 182 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@
55

66
from httpx import AsyncClient
77

8-
from async_search_client._http_requests import HttpRequests
8+
from async_search_client._http_requests import _HttpRequests
99
from async_search_client.errors import MeiliSearchApiError
1010
from async_search_client.index import Index
1111
from async_search_client.models import ClientStats, DumpInfo, Health, IndexInfo, Keys, Version
1212

1313

1414
class Client:
15+
"""The client to connect to the MeiliSearchApi."""
16+
1517
def __init__(self, url: str, api_key: str = None, timeout: int = 5) -> None:
18+
"""Class initializer.
19+
20+
Args:
21+
url: The url to the MeiliSearch API (ex: http://localhost:7700)
22+
api_key: The optional API key for MeiliSearch. Defaults to None.
23+
timeout: The amount of time in seconds that the client will wait for a response before
24+
timing out. Defaults to 5.
25+
"""
1626
self._http_client = AsyncClient(
1727
base_url=url, timeout=timeout, headers=self._set_headers(api_key)
1828
)
19-
self._http_requests = HttpRequests(self._http_client)
29+
self._http_requests = _HttpRequests(self._http_client)
2030

2131
async def __aenter__(self) -> Client:
2232
return self
@@ -30,20 +40,58 @@ async def __aexit__(
3040
await self.aclose()
3141

3242
async def aclose(self) -> None:
33-
"""Closes the client. This only needs to be used if the client was not created with a
34-
context manager
35-
"""
43+
"""Closes the client.
3644
45+
This only needs to be used if the client was not created with context manager.
46+
"""
3747
await self._http_client.aclose()
3848

3949
async def create_dump(self) -> DumpInfo:
50+
"""Trigger the creation of a MeiliSearch dump.
51+
52+
Returns:
53+
Information about the dump.
54+
https://docs.meilisearch.com/reference/api/dump.html#create-a-dump
55+
56+
Raises:
57+
MeilisearchCommunicationError: If there was an error communicating with the server.
58+
MeilisearchApiError: If the MeiliSearch API returned an error.
59+
MeiliSearchTimeoutError: If the connection times out.
60+
"""
4061
response = await self._http_requests.post("dumps")
4162
return DumpInfo(**response.json())
4263

4364
async def create_index(self, uid: str, primary_key: Optional[str] = None) -> Index:
65+
"""Creates a new index.
66+
67+
Args:
68+
uid: The index's unique identifier.
69+
primary_key: The primary key of the documents. Defaults to None.
70+
71+
Returns:
72+
An instance of Index containing the information of the newly created index.
73+
74+
Raises:
75+
MeilisearchCommunicationError: If there was an error communicating with the server.
76+
MeilisearchApiError: If the MeiliSearch API returned an error.
77+
MeiliSearchTimeoutError: If the connection times out.
78+
"""
4479
return await Index.create(self._http_client, uid, primary_key)
4580

4681
async def delete_index_if_exists(self, uid: str) -> bool:
82+
"""Deletes an index if it already exists.
83+
84+
Args:
85+
uid: The index's unique identifier.
86+
87+
Returns:
88+
True if an index was deleted for False if not.
89+
90+
Raises:
91+
MeilisearchCommunicationError: If there was an error communicating with the server.
92+
MeilisearchApiError: If the MeiliSearch API returned an error.
93+
MeiliSearchTimeoutError: If the connection times out.
94+
"""
4795
try:
4896
url = f"indexes/{uid}"
4997
await self._http_requests.delete(url)
@@ -54,6 +102,16 @@ async def delete_index_if_exists(self, uid: str) -> bool:
54102
return False
55103

56104
async def get_indexes(self) -> Optional[list[Index]]:
105+
"""Get all indexes.
106+
107+
Returns:
108+
A list of all indexes.
109+
110+
Raises:
111+
MeilisearchCommunicationError: If there was an error communicating with the server.
112+
MeilisearchApiError: If the MeiliSearch API returned an error.
113+
MeiliSearchTimeoutError: If the connection times out.
114+
"""
57115
response = await self._http_requests.get("indexes")
58116

59117
if not response.json():
@@ -71,25 +129,88 @@ async def get_indexes(self) -> Optional[list[Index]]:
71129
]
72130

73131
async def get_index(self, uid: str) -> Index:
132+
"""Gets a single index based on the uid of the index.
133+
134+
Args:
135+
uid: The index's unique identifier.
136+
137+
Returns:
138+
An Index instance containing the information of the fetched index.
139+
140+
Raises:
141+
MeilisearchCommunicationError: If there was an error communicating with the server.
142+
MeilisearchApiError: If the MeiliSearch API returned an error.
143+
MeiliSearchTimeoutError: If the connection times out.
144+
"""
74145
return await Index(self._http_client, uid).fetch_info()
75146

76147
def index(self, uid: str) -> Index:
77-
"""Create a local reference to an index identified by UID, without doing an HTTP call.
148+
"""Create a local reference to an index identified by UID, without making an HTTP call.
149+
78150
Because no network call is made this method is not awaitable.
79-
"""
80151
152+
Args:
153+
uid: The index's unique identifier.
154+
155+
Returns:
156+
An Index instance.
157+
158+
Raises:
159+
MeilisearchCommunicationError: If there was an error communicating with the server.
160+
MeilisearchApiError: If the MeiliSearch API returned an error.
161+
MeiliSearchTimeoutError: If the connection times out.
162+
"""
81163
return Index(self._http_client, uid=uid)
82164

83165
async def get_all_stats(self) -> ClientStats:
166+
"""Get stats for all indexes.
167+
168+
Returns:
169+
Information about database size and all indexes.
170+
https://docs.meilisearch.com/reference/api/stats.html
171+
172+
Raises:
173+
MeilisearchCommunicationError: If there was an error communicating with the server.
174+
MeilisearchApiError: If the MeiliSearch API returned an error.
175+
MeiliSearchTimeoutError: If the connection times out.
176+
"""
84177
response = await self._http_requests.get("stats")
85178
return ClientStats(**response.json())
86179

87180
async def get_dump_status(self, uid: str) -> DumpInfo:
181+
"""Retrieve the status of a MeiliSearch dump creation.
182+
183+
Args:
184+
uid: The index's unique identifier.
185+
186+
Returns:
187+
Information about the dump status.
188+
https://docs.meilisearch.com/reference/api/dump.html#get-dump-status
189+
190+
Raises:
191+
MeilisearchCommunicationError: If there was an error communicating with the server.
192+
MeilisearchApiError: If the MeiliSearch API returned an error.
193+
MeiliSearchTimeoutError: If the connection times out.
194+
"""
88195
url = f"dumps/{uid}/status"
89196
response = await self._http_requests.get(url)
90197
return DumpInfo(**response.json())
91198

92199
async def get_or_create_index(self, uid: str, primary_key: Optional[str] = None) -> Index:
200+
"""Get an index, or create it if it doesn't exist.
201+
202+
Args:
203+
uid: The index's unique identifier.
204+
primary_key: The primary key of the documents. Defaults to None.
205+
206+
Returns:
207+
An instance of Index containing the information of the retrieved or newly created index.
208+
209+
Raises:
210+
MeilisearchCommunicationError: If there was an error communicating with the server.
211+
MeilisearchApiError: If the MeiliSearch API returned an error.MeiliSearchTimeoutError: If the connection times out.
212+
MeiliSearchTimeoutError: If the connection times out.
213+
"""
93214
try:
94215
index_instance = await self.get_index(uid)
95216
except MeiliSearchApiError as err:
@@ -99,10 +220,34 @@ async def get_or_create_index(self, uid: str, primary_key: Optional[str] = None)
99220
return index_instance
100221

101222
async def get_keys(self) -> Keys:
223+
"""Gets the MeiliSearch public and private keys.
224+
225+
Returns:
226+
The public and private keys.
227+
https://docs.meilisearch.com/reference/api/keys.html#get-keys
228+
229+
Raises:
230+
MeilisearchCommunicationError: If there was an error communicating with the server.
231+
MeilisearchApiError: If the MeiliSearch API returned an error.
232+
MeiliSearchTimeoutError: If the connection times out.
233+
"""
102234
response = await self._http_requests.get("keys")
103235
return Keys(**response.json())
104236

105237
async def get_raw_index(self, uid: str) -> Optional[IndexInfo]:
238+
"""Gets the index and returns all the index information rather than an Index instance.
239+
240+
Args:
241+
uid: The index's unique identifier.
242+
243+
Returns:
244+
Index information rather than an Index instance.
245+
246+
Raises:
247+
MeilisearchCommunicationError: If there was an error communicating with the server.
248+
MeilisearchApiError: If the MeiliSearch API returned an error.
249+
MeiliSearchTimeoutError: If the connection times out.
250+
"""
106251
response = await self._http_client.get(f"indexes/{uid}")
107252

108253
if response.status_code == 404:
@@ -111,6 +256,18 @@ async def get_raw_index(self, uid: str) -> Optional[IndexInfo]:
111256
return IndexInfo(**response.json())
112257

113258
async def get_raw_indexes(self) -> Optional[list[IndexInfo]]:
259+
"""Gets all the indexes.
260+
261+
Returns all the index information rather than an Index instance.
262+
263+
Returns:
264+
A list of the Index information rather than an Index instances.
265+
266+
Raises:
267+
MeilisearchCommunicationError: If there was an error communicating with the server.
268+
MeilisearchApiError: If the MeiliSearch API returned an error.
269+
MeiliSearchTimeoutError: If the connection times out.
270+
"""
114271
response = await self._http_requests.get("indexes")
115272

116273
if not response.json():
@@ -119,14 +276,30 @@ async def get_raw_indexes(self) -> Optional[list[IndexInfo]]:
119276
return [IndexInfo(**x) for x in response.json()]
120277

121278
async def get_version(self) -> Version:
122-
"""Get version MeiliSearch that is running"""
279+
"""Get the MeiliSearch version.
280+
281+
Returns:
282+
Information about the version of MeiliSearch.
123283
284+
Raises:
285+
MeilisearchCommunicationError: If there was an error communicating with the server.
286+
MeilisearchApiError: If the MeiliSearch API returned an error.
287+
MeiliSearchTimeoutError: If the connection times out.
288+
"""
124289
response = await self._http_requests.get("version")
125290
return Version(**response.json())
126291

127292
async def health(self) -> Health:
128-
"""Get health of the MeiliSearch server"""
293+
"""Get health of the MeiliSearch server.
294+
295+
Returns:
296+
The status of the MeiliSearch server.
129297
298+
Raises:
299+
MeilisearchCommunicationError: If there was an error communicating with the server.
300+
MeilisearchApiError: If the MeiliSearch API returned an error.
301+
MeiliSearchTimeoutError: If the connection times out.
302+
"""
130303
response = await self._http_requests.get("health")
131304
return Health(**response.json())
132305

async_search_client/errors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class MeiliSearchError(Exception):
5-
"""Generic class for MeiliSearch error handling"""
5+
"""Generic class for MeiliSearch error handling."""
66

77
def __init__(self, message: str) -> None:
88
self.message = message
@@ -13,7 +13,7 @@ def __str__(self) -> str:
1313

1414

1515
class MeiliSearchApiError(MeiliSearchError):
16-
"""Error sent by MeiliSearch API"""
16+
"""Error sent by MeiliSearch API."""
1717

1818
def __init__(self, error: str, response: Response) -> None:
1919
self.status_code = response.status_code
@@ -33,14 +33,14 @@ def __str__(self) -> str:
3333

3434

3535
class MeiliSearchCommunicationError(MeiliSearchError):
36-
"""Error when connecting to MeiliSearch"""
36+
"""Error when connecting to MeiliSearch."""
3737

3838
def __str__(self) -> str:
3939
return f"MeiliSearchCommunicationError, {self.message}"
4040

4141

4242
class MeiliSearchTimeoutError(MeiliSearchError):
43-
"""Error when MeiliSearch operation takes longer than expected"""
43+
"""Error when MeiliSearch operation takes longer than expected."""
4444

4545
def __str__(self) -> str:
4646
return f"MeiliSearchTimeoutError, {self.message}"

0 commit comments

Comments
 (0)