Skip to content

Commit faf9e90

Browse files
committed
Redesign update API to task API
1 parent c90b725 commit faf9e90

22 files changed

+603
-441
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ documents = [
9090
index.add_documents(documents) # => { "updateId": 0 }
9191
```
9292

93-
With the `updateId`, you can check the status (`enqueued`, `processing`, `processed` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
93+
With the `updateId`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
9494

9595
#### Basic Search <!-- omit in toc -->
9696

meilisearch/client.py

Lines changed: 132 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from meilisearch.index import Index
44
from meilisearch.config import Config
55
from meilisearch._httprequests import HttpRequests
6-
from meilisearch.errors import MeiliSearchApiError, MeiliSearchError
6+
from meilisearch.errors import MeiliSearchError
77

88
class Client():
99
"""
@@ -28,7 +28,7 @@ def __init__(
2828

2929
self.http = HttpRequests(self.config)
3030

31-
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Index:
31+
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
3232
"""Create an index.
3333
3434
Parameters
@@ -40,8 +40,9 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> In
4040
4141
Returns
4242
-------
43-
index : Index
44-
An instance of Index containing the information of the newly created index.
43+
task:
44+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
45+
https://docs.meilisearch.com/reference/api/tasks.html#get-a-task
4546
4647
Raises
4748
------
@@ -50,31 +51,52 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> In
5051
"""
5152
return Index.create(self.config, uid, options)
5253

53-
def delete_index_if_exists(self, uid: str) -> bool:
54-
"""Deletes an index if it already exists
54+
# def delete_index_if_exists(self, uid: str) -> bool:
55+
# """Deletes an index if it already exists
56+
57+
# Parameters
58+
# ----------
59+
# uid:
60+
# UID of the index.
61+
62+
# Returns
63+
# --------
64+
# Returns True if an index was deleted or False if not
65+
66+
# Raises
67+
# ------
68+
# MeiliSearchApiError
69+
# 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
70+
# """
71+
72+
# try:
73+
# self.http.delete(f'{self.config.paths.index}/{uid}')
74+
# return True
75+
# except MeiliSearchApiError as error:
76+
# if error.code != "index_not_found":
77+
# raise error
78+
# return False
79+
80+
def delete_index(self, uid: str) -> Dict[str, Any]:
81+
"""Deletes an index
5582
5683
Parameters
5784
----------
5885
uid:
5986
UID of the index.
6087
6188
Returns
62-
--------
63-
Returns True if an index was deleted or False if not
89+
-------
90+
task:
91+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
92+
https://docs.meilisearch.com/reference/api/tasks.html#get-a-task
6493
6594
Raises
6695
------
6796
MeiliSearchApiError
6897
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
6998
"""
70-
71-
try:
72-
self.http.delete(f'{self.config.paths.index}/{uid}')
73-
return True
74-
except MeiliSearchApiError as error:
75-
if error.code != "index_not_found":
76-
raise error
77-
return False
99+
return self.http.delete(f'{self.config.paths.index}/{uid}')
78100

79101
def get_indexes(self) -> List[Index]:
80102
"""Get all indexes.
@@ -177,33 +199,33 @@ def index(self, uid: str) -> Index:
177199
return Index(self.config, uid=uid)
178200
raise Exception('The index UID should not be None')
179201

180-
def get_or_create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Index:
181-
"""Get an index, or create it if it doesn't exist.
182-
183-
Parameters
184-
----------
185-
uid:
186-
UID of the index
187-
options (optional): dict
188-
Options passed during index creation (ex: primaryKey)
189-
190-
Returns
191-
-------
192-
index:
193-
An instance of Index containing the information of the retrieved or newly created index.
194-
195-
Raises
196-
------
197-
MeiliSearchApiError
198-
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
199-
"""
200-
try:
201-
index_instance = self.get_index(uid)
202-
except MeiliSearchApiError as err:
203-
if err.code != 'index_not_found':
204-
raise err
205-
index_instance = self.create_index(uid, options)
206-
return index_instance
202+
# def get_or_create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Index:
203+
# """Get an index, or create it if it doesn't exist.
204+
205+
# Parameters
206+
# ----------
207+
# uid:
208+
# UID of the index
209+
# options (optional): dict
210+
# Options passed during index creation (ex: primaryKey)
211+
212+
# Returns
213+
# -------
214+
# index:
215+
# An instance of Index containing the information of the retrieved or newly created index.
216+
217+
# Raises
218+
# ------
219+
# MeiliSearchApiError
220+
# 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
221+
# """
222+
# try:
223+
# index_instance = self.get_index(uid)
224+
# except MeiliSearchApiError as err:
225+
# if err.code != 'index_not_found':
226+
# raise err
227+
# index_instance = self.create_index(uid, options)
228+
# return index_instance
207229

208230
def get_all_stats(self) -> Dict[str, Any]:
209231
"""Get all stats of MeiliSearch
@@ -333,3 +355,70 @@ def get_dump_status(self, uid: str) -> Dict[str, str]:
333355
return self.http.get(
334356
self.config.paths.dumps + '/' + str(uid) + '/status'
335357
)
358+
359+
def get_tasks(self) -> List[Dict[str, Any]]:
360+
"""Get all tasks.
361+
362+
Returns
363+
-------
364+
task:
365+
List of all enqueued, processing, succeeded or failed actions of the index.
366+
367+
Raises
368+
------
369+
MeiliSearchApiError
370+
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
371+
"""
372+
return self.http.get(
373+
f'{self.config.paths.task}'
374+
)
375+
376+
def get_task(self, uid: int) -> Dict[str, Any]:
377+
"""Get one task.
378+
379+
Parameters
380+
----------
381+
uid:
382+
identifier of the task to retrieve
383+
384+
Returns
385+
-------
386+
task:
387+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
388+
389+
Raises
390+
------
391+
MeiliSearchApiError
392+
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
393+
"""
394+
return self.http.get(
395+
f'{self.config.paths.task}/{uid}'
396+
)
397+
398+
def wait_for_task(
399+
self, uid: int,
400+
timeout_in_ms: int = 5000,
401+
interval_in_ms: int = 50,
402+
) -> Dict[str, Any]:
403+
"""Wait until MeiliSearch processes a task, and get its status as failed or succeeded.
404+
405+
Parameters
406+
----------
407+
uid:
408+
identifier of the task to retrieve
409+
timeout_in_ms (optional):
410+
time the method should wait before raising a MeiliSearchTimeoutError
411+
interval_in_ms (optional):
412+
time interval the method should wait (sleep) between requests
413+
414+
Returns
415+
-------
416+
task:
417+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
418+
419+
Raises
420+
------
421+
MeiliSearchTimeoutError
422+
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
423+
"""
424+
return Index(self.config, str(uid)).wait_for_task(uid, timeout_in_ms, interval_in_ms)

meilisearch/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Paths():
1111
keys = 'keys'
1212
version = 'version'
1313
index = 'indexes'
14-
update = 'updates'
14+
task = 'tasks'
1515
stat = 'stats'
1616
search = 'search'
1717
document = 'documents'

0 commit comments

Comments
 (0)