|
27 | 27 | import base64 |
28 | 28 | import random |
29 | 29 | import sys |
| 30 | +import time |
| 31 | +import copy |
30 | 32 | from platform import python_version |
31 | 33 |
|
32 | 34 | try: |
|
36 | 38 |
|
37 | 39 | from .version import VERSION |
38 | 40 | from .index import Index |
| 41 | +from .analytics import Analytics |
39 | 42 |
|
40 | 43 | from .transport import Transport |
41 | 44 | from .helpers import deprecated |
@@ -257,6 +260,31 @@ def batch(self, requests, request_options=None): |
257 | 260 | path = '/1/indexes/*/batch' |
258 | 261 | return self._req(False, path, 'POST', request_options, data=requests) |
259 | 262 |
|
| 263 | + def wait_task(self, index_name, task_id, time_before_retry=100, request_options=None): |
| 264 | + """ |
| 265 | + Wait the publication of a task on the server. |
| 266 | + All server task are asynchronous and you can check with this method |
| 267 | + that the task is published. |
| 268 | +
|
| 269 | + @param index_name the index name associated with the taskID |
| 270 | + @param task_id the id of the task returned by server |
| 271 | + @param time_before_retry the time in milliseconds before retry (default = 100ms) |
| 272 | + @param request_options |
| 273 | + """ |
| 274 | + while True: |
| 275 | + task = self.get_task(index_name, task_id, request_options) |
| 276 | + if task['status'] == 'published': |
| 277 | + return task |
| 278 | + time.sleep(time_before_retry / 1000.0) |
| 279 | + |
| 280 | + def get_task(self, index_name, task_id, request_options=None): |
| 281 | + path = '/1/indexes/%s/task/%d' % (safe(index_name), task_id) |
| 282 | + return self._req(True, path, 'GET', request_options) |
| 283 | + |
| 284 | + def is_task_published(self, index_name, task_id, request_options=None): |
| 285 | + task = self.get_task(index_name, task_id, request_options) |
| 286 | + return task['status'] == 'published' |
| 287 | + |
260 | 288 | @deprecated |
261 | 289 | def listIndexes(self): |
262 | 290 | return self.list_indexes() |
@@ -351,6 +379,9 @@ def init_index(self, index_name): |
351 | 379 | """ |
352 | 380 | return Index(self, index_name) |
353 | 381 |
|
| 382 | + def init_analytics(self): |
| 383 | + return Analytics(self, copy.deepcopy(self._transport)) |
| 384 | + |
354 | 385 | @deprecated |
355 | 386 | def listUserKeys(self): |
356 | 387 | return self.list_user_keys() |
|
0 commit comments