Skip to content

Commit 98c0b80

Browse files
authored
Merge branch 'main' into object-orient
2 parents 71d7699 + 3b5c2e8 commit 98c0b80

29 files changed

+634
-470
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ types-requests = "*"
1515
black = "*"
1616
isort = "*"
1717
importlib_metadata = {version = "*", markers="python_version < '3.8'"}
18-
zipp = {version = "*", markers="python_version < '3.8'"}
18+
zipp = {version = "==3.12.1", markers="python_version < '3.8'"}
1919
exceptiongroup = {version = "*", markers="python_version < '3.11'"}
2020
tomli = {version = "*", markers="python_version < '3.11'"}
2121
wrapt = {version = "*", markers="python_version < '3.11'"}

Pipfile.lock

Lines changed: 232 additions & 123 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ index.search(
197197

198198
## 🤖 Compatibility with Meilisearch
199199

200-
This package only guarantees the compatibility with the [version v0.30.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.30.0).
200+
This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-python/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
201201

202202
## 💡 Learn more
203203

meilisearch/client.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from meilisearch.config import Config
1414
from meilisearch.errors import MeiliSearchError
1515
from meilisearch.index import Index
16+
from meilisearch.models.key import Key, KeysResults
1617
from meilisearch.models.task import TaskInfo
1718
from meilisearch.task import TaskHandler
1819

@@ -42,7 +43,7 @@ def __init__(
4243

4344
self.task_handler = TaskHandler(self.config)
4445

45-
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
46+
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> TaskInfo:
4647
"""Create an index.
4748
4849
Parameters
@@ -54,8 +55,8 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Di
5455
5556
Returns
5657
-------
57-
task:
58-
Dictionary containing a task to track the informations about the progress of an asynchronous process.
58+
task_info:
59+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
5960
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
6061
6162
Raises
@@ -65,7 +66,7 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Di
6566
"""
6667
return Index.create(self.config, uid, options)
6768

68-
def delete_index(self, uid: str) -> Dict[str, Any]:
69+
def delete_index(self, uid: str) -> TaskInfo:
6970
"""Deletes an index
7071
7172
Parameters
@@ -75,7 +76,7 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
7576
7677
Returns
7778
-------
78-
task:
79+
task_info:
7980
Dictionary containing a task to track the informations about the progress of an asynchronous process.
8081
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
8182
@@ -85,7 +86,9 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
8586
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
8687
"""
8788

88-
return self.http.delete(f"{self.config.paths.index}/{uid}")
89+
task = self.http.delete(f"{self.config.paths.index}/{uid}")
90+
91+
return TaskInfo(**task)
8992

9093
def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
9194
"""Get all indexes.
@@ -200,7 +203,7 @@ def index(self, uid: str) -> Index:
200203
"""
201204
if uid is not None:
202205
return Index(self.config, uid=uid)
203-
raise Exception("The index UID should not be None")
206+
raise ValueError("The index UID should not be None")
204207

205208
def get_all_stats(self) -> Dict[str, Any]:
206209
"""Get all stats of Meilisearch
@@ -243,7 +246,7 @@ def is_healthy(self) -> bool:
243246
return False
244247
return True
245248

246-
def get_key(self, key_or_uid: str) -> Dict[str, Any]:
249+
def get_key(self, key_or_uid: str) -> Key:
247250
"""Gets information about a specific API key.
248251
249252
Parameters
@@ -262,9 +265,11 @@ def get_key(self, key_or_uid: str) -> Dict[str, Any]:
262265
MeiliSearchApiError
263266
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
264267
"""
265-
return self.http.get(f"{self.config.paths.keys}/{key_or_uid}")
268+
key = self.http.get(f"{self.config.paths.keys}/{key_or_uid}")
266269

267-
def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
270+
return Key(**key)
271+
272+
def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> KeysResults:
268273
"""Gets the Meilisearch API keys.
269274
270275
Parameters
@@ -275,7 +280,7 @@ def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any
275280
Returns
276281
-------
277282
keys:
278-
Dictionary with limit, offset, total and results a list of dictionaries containing the key information.
283+
API keys.
279284
https://docs.meilisearch.com/reference/api/keys.html#get-keys
280285
281286
Raises
@@ -285,9 +290,11 @@ def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any
285290
"""
286291
if parameters is None:
287292
parameters = {}
288-
return self.http.get(f"{self.config.paths.keys}?{parse.urlencode(parameters)}")
293+
keys = self.http.get(f"{self.config.paths.keys}?{parse.urlencode(parameters)}")
294+
295+
return KeysResults(**keys)
289296

290-
def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
297+
def create_key(self, options: Dict[str, Any]) -> Key:
291298
"""Creates a new API key.
292299
293300
Parameters
@@ -301,7 +308,7 @@ def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
301308
302309
Returns
303310
-------
304-
keys:
311+
key:
305312
The new API key.
306313
https://docs.meilisearch.com/reference/api/keys.html#get-keys
307314
@@ -310,14 +317,16 @@ def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
310317
MeiliSearchApiError
311318
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
312319
"""
313-
return self.http.post(f"{self.config.paths.keys}", options)
320+
task = self.http.post(f"{self.config.paths.keys}", options)
314321

315-
def update_key(self, key_or_uid: str, options: Dict[str, Any]) -> Dict[str, Any]:
322+
return Key(**task)
323+
324+
def update_key(self, key_or_uid: str, options: Dict[str, Any]) -> Key:
316325
"""Update an API key.
317326
318327
Parameters
319328
----------
320-
key:
329+
key_or_uid:
321330
The key or the uid of the key for which to update the information.
322331
options:
323332
The information to use in creating the key (ex: { 'description': 'Search Key', 'expiresAt': '22-01-01' }). Note that if an
@@ -335,9 +344,11 @@ def update_key(self, key_or_uid: str, options: Dict[str, Any]) -> Dict[str, Any]
335344
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
336345
"""
337346
url = f"{self.config.paths.keys}/{key_or_uid}"
338-
return self.http.patch(url, options)
347+
key = self.http.patch(url, options)
348+
349+
return Key(**key)
339350

340-
def delete_key(self, key_or_uid: str) -> Dict[str, int]:
351+
def delete_key(self, key_or_uid: str) -> int:
341352
"""Deletes an API key.
342353
343354
Parameters
@@ -356,7 +367,9 @@ def delete_key(self, key_or_uid: str) -> Dict[str, int]:
356367
MeiliSearchApiError
357368
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
358369
"""
359-
return self.http.delete(f"{self.config.paths.keys}/{key_or_uid}")
370+
response = self.http.delete(f"{self.config.paths.keys}/{key_or_uid}")
371+
372+
return response.status_code
360373

361374
def get_version(self) -> Dict[str, str]:
362375
"""Get version Meilisearch
@@ -388,7 +401,7 @@ def version(self) -> Dict[str, str]:
388401
"""
389402
return self.get_version()
390403

391-
def create_dump(self) -> Dict[str, str]:
404+
def create_dump(self) -> TaskInfo:
392405
"""Trigger the creation of a Meilisearch dump.
393406
394407
Returns
@@ -402,7 +415,9 @@ def create_dump(self) -> Dict[str, str]:
402415
MeiliSearchApiError
403416
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
404417
"""
405-
return self.http.post(self.config.paths.dumps)
418+
task = self.http.post(self.config.paths.dumps)
419+
420+
return TaskInfo(**task)
406421

407422
def swap_indexes(self, parameters: List[Dict[str, List[str]]]) -> TaskInfo:
408423
"""Swap two indexes.
@@ -568,15 +583,15 @@ def generate_tenant_token(
568583
"""
569584
# Validate all fields
570585
if api_key == "" or api_key is None and self.config.api_key is None:
571-
raise Exception(
586+
raise ValueError(
572587
"An api key is required in the client or should be passed as an argument."
573588
)
574589
if api_key_uid == "" or api_key_uid is None or self._valid_uuid(api_key_uid) is False:
575-
raise Exception("An uid is required and must comply to the uuid4 format.")
590+
raise ValueError("An uid is required and must comply to the uuid4 format.")
576591
if not search_rules or search_rules == [""]:
577-
raise Exception("The search_rules field is mandatory and should be defined.")
592+
raise ValueError("The search_rules field is mandatory and should be defined.")
578593
if expires_at and expires_at < datetime.datetime.utcnow():
579-
raise Exception("The date expires_at should be in the future.")
594+
raise ValueError("The date expires_at should be in the future.")
580595

581596
# Standard JWT header for encryption with SHA256/HS256 algorithm
582597
header = {"typ": "JWT", "alg": "HS256"}

0 commit comments

Comments
 (0)