Skip to content

Commit af643df

Browse files
bors[bot]alallema
andauthored
Merge #372
372: Redesign update API to task API r=alallema a=alallema ### Breaking Changes All the actions on indexes are now asynchronous: - `create_index()`, `update_index()`, `delete_index()` return a `task` response instead of an `Index`. - `index.create` and `index.delete` from index return a `task`. - `wait_for_pending_update()` - is renamed into `wait_for_task` - the current `index.wait_for_task()` method call `/tasks/:uid` - `index.get_update_status` is renamed `index.get_task` - `index.get_all_update_status` is renamed `index.get_tasks` **Notes:** The only two methods that now return an `Index` are `client.index()` and `client.get_index()` ### Enhancements - new method `client.wait_for_task()` call `/tasks/:uid` - new method `client.get_tasks` that calls `/tasks` - new method `client.get_task` that calls `/tasks/:uid` ### Misc - `delete_if_exists` and `get_or_create_index` are commented since we don't know yet if we will remove them or not. Co-authored-by: alallema <[email protected]> Co-authored-by: Amélie <[email protected]>
2 parents e04de9e + 270ec3d commit af643df

23 files changed

+588
-363
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 task `uid`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [task endpoint](https://docs.meilisearch.com/reference/api/tasks.html#get-one-task).
9494

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

meilisearch/client.py

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from meilisearch.index import Index
44
from meilisearch.config import Config
5+
from meilisearch.task import get_task, get_tasks, wait_for_task
56
from meilisearch._httprequests import HttpRequests
6-
from meilisearch.errors import MeiliSearchApiError, MeiliSearchError
7+
from meilisearch.errors import MeiliSearchError
78

89
class Client():
910
"""
@@ -28,7 +29,7 @@ def __init__(
2829

2930
self.http = HttpRequests(self.config)
3031

31-
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Index:
32+
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
3233
"""Create an index.
3334
3435
Parameters
@@ -40,8 +41,9 @@ def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> In
4041
4142
Returns
4243
-------
43-
index : Index
44-
An instance of Index containing the information of the newly created index.
44+
task:
45+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
46+
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
4547
4648
Raises
4749
------
@@ -59,8 +61,10 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
5961
UID of the index.
6062
6163
Returns
62-
--------
63-
Returns True if an index was deleted or False if not
64+
-------
65+
task:
66+
Dictionary containing a task to track the informations about the progress of an asynchronous process.
67+
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task
6468
6569
Raises
6670
------
@@ -299,3 +303,66 @@ def get_dump_status(self, uid: str) -> Dict[str, str]:
299303
return self.http.get(
300304
self.config.paths.dumps + '/' + str(uid) + '/status'
301305
)
306+
307+
def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
308+
"""Get all tasks.
309+
310+
Returns
311+
-------
312+
task:
313+
Dictionary containing a list of all enqueued, processing, succeeded or failed tasks.
314+
315+
Raises
316+
------
317+
MeiliSearchApiError
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
319+
"""
320+
return get_tasks(self.config)
321+
322+
def get_task(self, uid: int) -> Dict[str, Any]:
323+
"""Get one task.
324+
325+
Parameters
326+
----------
327+
uid:
328+
Identifier of the task.
329+
330+
Returns
331+
-------
332+
task:
333+
Dictionary containing information about the processed asynchronous task.
334+
335+
Raises
336+
------
337+
MeiliSearchApiError
338+
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
339+
"""
340+
return get_task(self.config, uid)
341+
342+
def wait_for_task(
343+
self, uid: int,
344+
timeout_in_ms: int = 5000,
345+
interval_in_ms: int = 50,
346+
) -> Dict[str, Any]:
347+
"""Wait until MeiliSearch processes a task until it fails or succeeds.
348+
349+
Parameters
350+
----------
351+
uid:
352+
Identifier of the task to wait for being processed.
353+
timeout_in_ms (optional):
354+
Time the method should wait before raising a MeiliSearchTimeoutError
355+
interval_in_ms (optional):
356+
Time interval the method should wait (sleep) between requests
357+
358+
Returns
359+
-------
360+
task:
361+
Dictionary containing information about the processed asynchronous task.
362+
363+
Raises
364+
------
365+
MeiliSearchTimeoutError
366+
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
367+
"""
368+
return wait_for_task(self.config, 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)