13
13
from meilisearch .config import Config
14
14
from meilisearch .errors import MeiliSearchError
15
15
from meilisearch .index import Index
16
+ from meilisearch .models .key import Key , KeysResults
16
17
from meilisearch .models .task import TaskInfo
17
18
from meilisearch .task import TaskHandler
18
19
@@ -42,7 +43,7 @@ def __init__(
42
43
43
44
self .task_handler = TaskHandler (self .config )
44
45
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 :
46
47
"""Create an index.
47
48
48
49
Parameters
@@ -54,8 +55,8 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Di
54
55
55
56
Returns
56
57
-------
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.
59
60
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
60
61
61
62
Raises
@@ -65,7 +66,7 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Di
65
66
"""
66
67
return Index .create (self .config , uid , options )
67
68
68
- def delete_index (self , uid : str ) -> Dict [ str , Any ] :
69
+ def delete_index (self , uid : str ) -> TaskInfo :
69
70
"""Deletes an index
70
71
71
72
Parameters
@@ -75,7 +76,7 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
75
76
76
77
Returns
77
78
-------
78
- task :
79
+ task_info :
79
80
Dictionary containing a task to track the informations about the progress of an asynchronous process.
80
81
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
81
82
@@ -85,7 +86,9 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
85
86
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
86
87
"""
87
88
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 )
89
92
90
93
def get_indexes (self , parameters : Optional [Dict [str , Any ]] = None ) -> Dict [str , List [Index ]]:
91
94
"""Get all indexes.
@@ -200,7 +203,7 @@ def index(self, uid: str) -> Index:
200
203
"""
201
204
if uid is not None :
202
205
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" )
204
207
205
208
def get_all_stats (self ) -> Dict [str , Any ]:
206
209
"""Get all stats of Meilisearch
@@ -243,7 +246,7 @@ def is_healthy(self) -> bool:
243
246
return False
244
247
return True
245
248
246
- def get_key (self , key_or_uid : str ) -> Dict [ str , Any ] :
249
+ def get_key (self , key_or_uid : str ) -> Key :
247
250
"""Gets information about a specific API key.
248
251
249
252
Parameters
@@ -262,9 +265,11 @@ def get_key(self, key_or_uid: str) -> Dict[str, Any]:
262
265
MeiliSearchApiError
263
266
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
264
267
"""
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 } " )
266
269
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 :
268
273
"""Gets the Meilisearch API keys.
269
274
270
275
Parameters
@@ -275,7 +280,7 @@ def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any
275
280
Returns
276
281
-------
277
282
keys:
278
- Dictionary with limit, offset, total and results a list of dictionaries containing the key information .
283
+ API keys .
279
284
https://docs.meilisearch.com/reference/api/keys.html#get-keys
280
285
281
286
Raises
@@ -285,9 +290,11 @@ def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any
285
290
"""
286
291
if parameters is None :
287
292
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 )
289
296
290
- def create_key (self , options : Dict [str , Any ]) -> Dict [ str , Any ] :
297
+ def create_key (self , options : Dict [str , Any ]) -> Key :
291
298
"""Creates a new API key.
292
299
293
300
Parameters
@@ -301,7 +308,7 @@ def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
301
308
302
309
Returns
303
310
-------
304
- keys :
311
+ key :
305
312
The new API key.
306
313
https://docs.meilisearch.com/reference/api/keys.html#get-keys
307
314
@@ -310,14 +317,16 @@ def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
310
317
MeiliSearchApiError
311
318
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
312
319
"""
313
- return self .http .post (f"{ self .config .paths .keys } " , options )
320
+ task = self .http .post (f"{ self .config .paths .keys } " , options )
314
321
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 :
316
325
"""Update an API key.
317
326
318
327
Parameters
319
328
----------
320
- key :
329
+ key_or_uid :
321
330
The key or the uid of the key for which to update the information.
322
331
options:
323
332
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]
335
344
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
336
345
"""
337
346
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 )
339
350
340
- def delete_key (self , key_or_uid : str ) -> Dict [ str , int ] :
351
+ def delete_key (self , key_or_uid : str ) -> int :
341
352
"""Deletes an API key.
342
353
343
354
Parameters
@@ -356,7 +367,9 @@ def delete_key(self, key_or_uid: str) -> Dict[str, int]:
356
367
MeiliSearchApiError
357
368
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
358
369
"""
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
360
373
361
374
def get_version (self ) -> Dict [str , str ]:
362
375
"""Get version Meilisearch
@@ -388,7 +401,7 @@ def version(self) -> Dict[str, str]:
388
401
"""
389
402
return self .get_version ()
390
403
391
- def create_dump (self ) -> Dict [ str , str ] :
404
+ def create_dump (self ) -> TaskInfo :
392
405
"""Trigger the creation of a Meilisearch dump.
393
406
394
407
Returns
@@ -402,7 +415,9 @@ def create_dump(self) -> Dict[str, str]:
402
415
MeiliSearchApiError
403
416
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
404
417
"""
405
- return self .http .post (self .config .paths .dumps )
418
+ task = self .http .post (self .config .paths .dumps )
419
+
420
+ return TaskInfo (** task )
406
421
407
422
def swap_indexes (self , parameters : List [Dict [str , List [str ]]]) -> TaskInfo :
408
423
"""Swap two indexes.
@@ -568,15 +583,15 @@ def generate_tenant_token(
568
583
"""
569
584
# Validate all fields
570
585
if api_key == "" or api_key is None and self .config .api_key is None :
571
- raise Exception (
586
+ raise ValueError (
572
587
"An api key is required in the client or should be passed as an argument."
573
588
)
574
589
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." )
576
591
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." )
578
593
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." )
580
595
581
596
# Standard JWT header for encryption with SHA256/HS256 algorithm
582
597
header = {"typ" : "JWT" , "alg" : "HS256" }
0 commit comments