Skip to content

Commit 88f9b98

Browse files
feat(task): move wait_task and cie to the Client
1 parent 1098048 commit 88f9b98

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55

66
<Contributors, please add your changes below this line>
77

8+
* 2 methods about taskID initially available in the `Index` moved to the `Client`.
9+
You could get some taskID from the engine without necessarily have an instance of Index,
10+
instead of instantiating an index that you won't need, you can now call wait_task and get_task_status on the client.
11+
The original methods on the index still work and are **not** deprecated.
12+
13+
```python
14+
client.wait_ask(index_name, taskID)
15+
client.get_task_status(index_name, taskID)
16+
```
17+
818
### 1.16.0 - 2018-06-07
919

1020
🎉 Note to contributors:

algoliasearch/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import base64
2828
import random
2929
import sys
30+
import time
3031
from platform import python_version
3132

3233
try:
@@ -257,6 +258,31 @@ def batch(self, requests, request_options=None):
257258
path = '/1/indexes/*/batch'
258259
return self._req(False, path, 'POST', request_options, data=requests)
259260

261+
def wait_task(self, index_name, task_id, time_before_retry=100, request_options=None):
262+
"""
263+
Wait the publication of a task on the server.
264+
All server task are asynchronous and you can check with this method
265+
that the task is published.
266+
267+
@param index_name the index name associated with the taskID
268+
@param task_id the id of the task returned by server
269+
@param time_before_retry the time in milliseconds before retry (default = 100ms)
270+
@param request_options
271+
"""
272+
while True:
273+
task = self.get_task(index_name, task_id, request_options)
274+
if task['status'] == 'published':
275+
return task
276+
time.sleep(time_before_retry / 1000.0)
277+
278+
def get_task(self, index_name, task_id, request_options=None):
279+
path = '/1/indexes/%s/task/%d' % (safe(index_name), task_id)
280+
return self._req(True, path, 'GET', request_options)
281+
282+
def is_task_published(self, index_name, task_id, request_options=None):
283+
task = self.get_task(index_name, task_id, request_options)
284+
return task['status'] == 'published'
285+
260286
@deprecated
261287
def listIndexes(self):
262288
return self.list_indexes()

algoliasearch/index.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -718,31 +718,10 @@ def waitTask(self, task_id, time_before_retry=100):
718718
return self.wait_task(task_id, time_before_retry)
719719

720720
def wait_task(self, task_id, time_before_retry=100, request_options=None):
721-
"""
722-
Wait the publication of a task on the server.
723-
All server task are asynchronous and you can check with this method
724-
that the task is published.
725-
726-
@param task_id the id of the task returned by server
727-
@param time_before_retry the time in milliseconds before retry (default = 100ms)
728-
"""
729-
path = '/task/%d' % task_id
730-
while True:
731-
res = self._req(True, path, 'GET', request_options)
732-
if res['status'] == 'published':
733-
return res
734-
time.sleep(time_before_retry / 1000.0)
721+
return self.client.wait_task(self.index_name, task_id, time_before_retry, request_options)
735722

736723
def is_task_published(self, task_id, request_options=None):
737-
'''
738-
Return True if the task on the server has been published
739-
740-
@param task_id the id of the task returned by server
741-
'''
742-
743-
path = '/task/{0}'.format(task_id)
744-
res = self._req(True, path, 'GET', request_options)
745-
return res['status'] == 'published'
724+
return self.client.wait_task(self.index_name, task_id, request_options)
746725

747726
@deprecated
748727
def getSettings(self):

0 commit comments

Comments
 (0)