Skip to content

Commit 7df661c

Browse files
authored
Merge pull request #112 from sanders41/docstrings
Improving docstrings
2 parents 75a9056 + 597500a commit 7df661c

File tree

10 files changed

+800
-42
lines changed

10 files changed

+800
-42
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from async_search_client.errors import MeiliSearchApiError, MeiliSearchCommunicationError
1313

1414

15-
class HttpRequests:
15+
class _HttpRequests:
1616
def __init__(self, http_client: AsyncClient) -> None:
1717
self.http_client = http_client
1818

async_search_client/client.py

Lines changed: 169 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,55 @@ 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+
"""
4060
response = await self._http_requests.post("dumps")
4161
return DumpInfo(**response.json())
4262

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

4679
async def delete_index_if_exists(self, uid: str) -> bool:
80+
"""Deletes an index if it already exists.
81+
82+
Args:
83+
uid: The index's unique identifier.
84+
85+
Returns:
86+
True if an index was deleted for False if not.
87+
88+
Raises:
89+
MeilisearchCommunicationError: If there was an error communicating with the server.
90+
MeilisearchApiError: If the MeiliSearch API returned an error.
91+
"""
4792
try:
4893
url = f"indexes/{uid}"
4994
await self._http_requests.delete(url)
@@ -54,6 +99,15 @@ async def delete_index_if_exists(self, uid: str) -> bool:
5499
return False
55100

56101
async def get_indexes(self) -> Optional[list[Index]]:
102+
"""Get all indexes.
103+
104+
Returns:
105+
A list of all indexes.
106+
107+
Raises:
108+
MeilisearchCommunicationError: If there was an error communicating with the server.
109+
MeilisearchApiError: If the MeiliSearch API returned an error.
110+
"""
57111
response = await self._http_requests.get("indexes")
58112

59113
if not response.json():
@@ -71,25 +125,84 @@ async def get_indexes(self) -> Optional[list[Index]]:
71125
]
72126

73127
async def get_index(self, uid: str) -> Index:
128+
"""Gets a single index based on the uid of the index.
129+
130+
Args:
131+
uid: The index's unique identifier.
132+
133+
Returns:
134+
An Index instance containing the information of the fetched index.
135+
136+
Raises:
137+
MeilisearchCommunicationError: If there was an error communicating with the server.
138+
MeilisearchApiError: If the MeiliSearch API returned an error.
139+
"""
74140
return await Index(self._http_client, uid).fetch_info()
75141

76142
def index(self, uid: str) -> Index:
77-
"""Create a local reference to an index identified by UID, without doing an HTTP call.
143+
"""Create a local reference to an index identified by UID, without making an HTTP call.
144+
78145
Because no network call is made this method is not awaitable.
79-
"""
80146
147+
Args:
148+
uid: The index's unique identifier.
149+
150+
Returns:
151+
An Index instance.
152+
153+
Raises:
154+
MeilisearchCommunicationError: If there was an error communicating with the server.
155+
MeilisearchApiError: If the MeiliSearch API returned an error.
156+
"""
81157
return Index(self._http_client, uid=uid)
82158

83159
async def get_all_stats(self) -> ClientStats:
160+
"""Get stats for all indexes.
161+
162+
Returns:
163+
Information about database size and all indexes.
164+
https://docs.meilisearch.com/reference/api/stats.html
165+
166+
Raises:
167+
MeilisearchCommunicationError: If there was an error communicating with the server.
168+
MeilisearchApiError: If the MeiliSearch API returned an error.
169+
"""
84170
response = await self._http_requests.get("stats")
85171
return ClientStats(**response.json())
86172

87173
async def get_dump_status(self, uid: str) -> DumpInfo:
174+
"""Retrieve the status of a MeiliSearch dump creation.
175+
176+
Args:
177+
uid: The index's unique identifier.
178+
179+
Returns:
180+
Information about the dump status.
181+
https://docs.meilisearch.com/reference/api/dump.html#get-dump-status
182+
183+
Raises:
184+
MeilisearchCommunicationError: If there was an error communicating with the server.
185+
MeilisearchApiError: If the MeiliSearch API returned an error.
186+
"""
88187
url = f"dumps/{uid}/status"
89188
response = await self._http_requests.get(url)
90189
return DumpInfo(**response.json())
91190

92191
async def get_or_create_index(self, uid: str, primary_key: Optional[str] = None) -> Index:
192+
"""Get an index, or create it if it doesn't exist.
193+
194+
Args:
195+
uid: The index's unique identifier.
196+
primary_key: The primary key of the documents. Defaults to None.
197+
198+
Returns:
199+
An instance of Index containing the information of the retrieved or newly created index.
200+
201+
Raises:
202+
MeilisearchCommunicationError: If there was an error communicating with the server.
203+
MeilisearchApiError: If the MeiliSearch API returned an error.MeiliSearchTimeoutError: If the connection times out.
204+
MeiliSearchTimeoutError: If the connection times out.
205+
"""
93206
try:
94207
index_instance = await self.get_index(uid)
95208
except MeiliSearchApiError as err:
@@ -99,10 +212,32 @@ async def get_or_create_index(self, uid: str, primary_key: Optional[str] = None)
99212
return index_instance
100213

101214
async def get_keys(self) -> Keys:
215+
"""Gets the MeiliSearch public and private keys.
216+
217+
Returns:
218+
The public and private keys.
219+
https://docs.meilisearch.com/reference/api/keys.html#get-keys
220+
221+
Raises:
222+
MeilisearchCommunicationError: If there was an error communicating with the server.
223+
MeilisearchApiError: If the MeiliSearch API returned an error.
224+
"""
102225
response = await self._http_requests.get("keys")
103226
return Keys(**response.json())
104227

105228
async def get_raw_index(self, uid: str) -> Optional[IndexInfo]:
229+
"""Gets the index and returns all the index information rather than an Index instance.
230+
231+
Args:
232+
uid: The index's unique identifier.
233+
234+
Returns:
235+
Index information rather than an Index instance.
236+
237+
Raises:
238+
MeilisearchCommunicationError: If there was an error communicating with the server.
239+
MeilisearchApiError: If the MeiliSearch API returned an error.
240+
"""
106241
response = await self._http_client.get(f"indexes/{uid}")
107242

108243
if response.status_code == 404:
@@ -111,6 +246,17 @@ async def get_raw_index(self, uid: str) -> Optional[IndexInfo]:
111246
return IndexInfo(**response.json())
112247

113248
async def get_raw_indexes(self) -> Optional[list[IndexInfo]]:
249+
"""Gets all the indexes.
250+
251+
Returns all the index information rather than an Index instance.
252+
253+
Returns:
254+
A list of the Index information rather than an Index instances.
255+
256+
Raises:
257+
MeilisearchCommunicationError: If there was an error communicating with the server.
258+
MeilisearchApiError: If the MeiliSearch API returned an error.
259+
"""
114260
response = await self._http_requests.get("indexes")
115261

116262
if not response.json():
@@ -119,14 +265,28 @@ async def get_raw_indexes(self) -> Optional[list[IndexInfo]]:
119265
return [IndexInfo(**x) for x in response.json()]
120266

121267
async def get_version(self) -> Version:
122-
"""Get version MeiliSearch that is running"""
268+
"""Get the MeiliSearch version.
269+
270+
Returns:
271+
Information about the version of MeiliSearch.
123272
273+
Raises:
274+
MeilisearchCommunicationError: If there was an error communicating with the server.
275+
MeilisearchApiError: If the MeiliSearch API returned an error.
276+
"""
124277
response = await self._http_requests.get("version")
125278
return Version(**response.json())
126279

127280
async def health(self) -> Health:
128-
"""Get health of the MeiliSearch server"""
281+
"""Get health of the MeiliSearch server.
282+
283+
Returns:
284+
The status of the MeiliSearch server.
129285
286+
Raises:
287+
MeilisearchCommunicationError: If there was an error communicating with the server.
288+
MeilisearchApiError: If the MeiliSearch API returned an error.
289+
"""
130290
response = await self._http_requests.get("health")
131291
return Health(**response.json())
132292

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)