Skip to content

Commit 1e8a35a

Browse files
committed
Modification after review
1 parent 68d915b commit 1e8a35a

File tree

3 files changed

+103
-113
lines changed

3 files changed

+103
-113
lines changed

meilisearch/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from meilisearch.index import Index
44
from meilisearch.config import Config
5-
from meilisearch.task import Task
5+
from meilisearch.task import get_task, get_tasks, wait_for_task
66
from meilisearch._httprequests import HttpRequests
77
from meilisearch.errors import MeiliSearchError
88

@@ -370,7 +370,7 @@ def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
370370
MeiliSearchApiError
371371
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
372372
"""
373-
return Task(self.config).get_tasks()
373+
return get_tasks(self.config)
374374

375375
def get_task(self, uid: int) -> Dict[str, Any]:
376376
"""Get one task.
@@ -390,7 +390,7 @@ def get_task(self, uid: int) -> Dict[str, Any]:
390390
MeiliSearchApiError
391391
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
392392
"""
393-
return Task(self.config).get_task(uid)
393+
return get_task(self.config, uid)
394394

395395
def wait_for_task(
396396
self, uid: int,
@@ -418,4 +418,4 @@ def wait_for_task(
418418
MeiliSearchTimeoutError
419419
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
420420
"""
421-
return Task(self.config).wait_for_task(uid, timeout_in_ms, interval_in_ms)
421+
return wait_for_task(self.config, uid, timeout_in_ms, interval_in_ms)

meilisearch/index.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from meilisearch._httprequests import HttpRequests
66
from meilisearch.config import Config
7-
from meilisearch.task import Task
7+
from meilisearch.task import get_task, get_tasks, wait_for_task
88

99
# pylint: disable=too-many-public-methods
1010
class Index():
@@ -156,7 +156,7 @@ def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
156156
MeiliSearchApiError
157157
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
158158
"""
159-
return Task(self.config, self.uid).get_tasks()
159+
return get_tasks(self.config, self.uid)
160160

161161
def get_task(self, uid: int) -> Dict[str, Any]:
162162
"""Get one task through the route of a specific index.
@@ -176,7 +176,7 @@ def get_task(self, uid: int) -> Dict[str, Any]:
176176
MeiliSearchApiError
177177
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
178178
"""
179-
return Task(self.config, self.uid).get_task(uid)
179+
return get_task(self.config, uid, self.uid)
180180

181181
def wait_for_task(
182182
self, uid: int,
@@ -204,7 +204,7 @@ def wait_for_task(
204204
MeiliSearchTimeoutError
205205
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
206206
"""
207-
return Task(self.config).wait_for_task(uid, timeout_in_ms, interval_in_ms)
207+
return wait_for_task(self.config, uid, timeout_in_ms, interval_in_ms)
208208

209209
def get_stats(self) -> Dict[str, Any]:
210210
"""Get stats of the index.

meilisearch/task.py

Lines changed: 95 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,101 @@
66
from meilisearch.config import Config
77
from meilisearch.errors import MeiliSearchTimeoutError
88

9-
class Task:
9+
def get_tasks(config: Config, index_id: Optional[str] = None) -> Dict[str, List[Dict[str, Any]]]:
10+
"""Get all tasks.
11+
12+
Parameters
13+
----------
14+
config:
15+
Config object containing permission and location of MeiliSearch.
16+
index_id:
17+
The id of the `Index`.
18+
19+
Returns
20+
-------
21+
task:
22+
Dictionary containing a list of all enqueued, processing, succeeded or failed tasks.
23+
24+
Raises
25+
------
26+
MeiliSearchApiError
27+
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
1028
"""
11-
Tasks routes wrapper.
12-
13-
Task class gives access to all Task routes and child routes via an Index (inherited).
14-
https://docs.meilisearch.com/reference/api/task.html
15-
"""
16-
17-
def __init__(
18-
self,
19-
config: Config,
20-
uid: Optional[str] = None,
21-
) -> None:
22-
"""
23-
Parameters
24-
----------
25-
config:
26-
Config object containing permission and location of MeiliSearch.
27-
"""
28-
self.config = config
29-
self.http = HttpRequests(config)
30-
self.uid = uid
31-
32-
def get_tasks(self) -> Dict[str, List[Dict[str, Any]]]:
33-
"""Get all tasks.
34-
35-
Returns
36-
-------
37-
task:
38-
Dictionary containing a list of all enqueued, processing, succeeded or failed tasks.
39-
40-
Raises
41-
------
42-
MeiliSearchApiError
43-
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
44-
"""
45-
if self.uid is None:
46-
return self.http.get(
47-
f'{self.config.paths.task}'
48-
)
49-
return self.http.get(
50-
f'{self.config.paths.index}/{self.uid}/{self.config.paths.task}'
29+
http = HttpRequests(config)
30+
if index_id is None:
31+
return http.get(
32+
f'{config.paths.task}'
5133
)
52-
53-
54-
def get_task(self, uid: int) -> Dict[str, Any]:
55-
"""Get one task.
56-
57-
Parameters
58-
----------
59-
uid:
60-
Identifier of the task.
61-
62-
Returns
63-
-------
64-
task:
65-
Dictionary containing information about the processed asynchronous task.
66-
67-
Raises
68-
------
69-
MeiliSearchApiError
70-
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
71-
"""
72-
if self.uid is None:
73-
return self.http.get(
74-
f'{self.config.paths.task}/{uid}'
75-
)
76-
return self.http.get(
77-
f'{self.config.paths.index}/{self.uid}/{self.config.paths.task}/{uid}'
34+
return http.get(
35+
f'{config.paths.index}/{index_id}/{config.paths.task}'
36+
)
37+
38+
def get_task(config: Config, uid: int, index_id: Optional[str] = None) -> Dict[str, Any]:
39+
"""Get one task.
40+
41+
Parameters
42+
----------
43+
config:
44+
Config object containing permission and location of MeiliSearch.
45+
uid:
46+
Identifier of the task.
47+
index_id:
48+
The id of the `Index`.
49+
50+
Returns
51+
-------
52+
task:
53+
Dictionary containing information about the status of the asynchronous task.
54+
55+
Raises
56+
------
57+
MeiliSearchApiError
58+
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
59+
"""
60+
http = HttpRequests(config)
61+
if index_id is None:
62+
return http.get(
63+
f'{config.paths.task}/{uid}'
7864
)
79-
80-
81-
def wait_for_task(
82-
self, uid: int,
83-
timeout_in_ms: int = 5000,
84-
interval_in_ms: int = 50,
85-
) -> Dict[str, Any]:
86-
"""Wait until MeiliSearch processes a task until it fails or succeeds.
87-
88-
Parameters
89-
----------
90-
uid:
91-
Identifier of the task to wait for being processed.
92-
timeout_in_ms (optional):
93-
Time the method should wait before raising a MeiliSearchTimeoutError.
94-
interval_in_ms (optional):
95-
Time interval the method should wait (sleep) between requests.
96-
97-
Returns
98-
-------
99-
task:
100-
Dictionary containing information about the processed asynchronous task.
101-
102-
Raises
103-
------
104-
MeiliSearchTimeoutError
105-
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
106-
"""
107-
start_time = datetime.now()
108-
elapsed_time = 0.
109-
while elapsed_time < timeout_in_ms:
110-
get_task = self.get_task(uid)
111-
if get_task['status'] not in ('enqueued', 'processing'):
112-
return get_task
113-
sleep(interval_in_ms / 1000)
114-
time_delta = datetime.now() - start_time
115-
elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000
116-
raise MeiliSearchTimeoutError(f'timeout of ${timeout_in_ms}ms has exceeded on process ${uid} when waiting for task to be resolve.')
65+
return http.get(
66+
f'{config.paths.index}/{index_id}/{config.paths.task}/{uid}'
67+
)
68+
69+
70+
def wait_for_task(
71+
config: Config,
72+
uid: int,
73+
timeout_in_ms: int = 5000,
74+
interval_in_ms: int = 50,
75+
) -> Dict[str, Any]:
76+
"""Wait until the task fails or succeeds in MeiliSearch.
77+
78+
Parameters
79+
----------
80+
uid:
81+
Identifier of the task to wait for being processed.
82+
timeout_in_ms (optional):
83+
Time the method should wait before raising a MeiliSearchTimeoutError.
84+
interval_in_ms (optional):
85+
Time interval the method should wait (sleep) between requests.
86+
87+
Returns
88+
-------
89+
task:
90+
Dictionary containing information about the processed asynchronous task.
91+
92+
Raises
93+
------
94+
MeiliSearchTimeoutError
95+
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
96+
"""
97+
start_time = datetime.now()
98+
elapsed_time = 0.
99+
while elapsed_time < timeout_in_ms:
100+
task = get_task(config, uid)
101+
if task['status'] not in ('enqueued', 'processing'):
102+
return task
103+
sleep(interval_in_ms / 1000)
104+
time_delta = datetime.now() - start_time
105+
elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000
106+
raise MeiliSearchTimeoutError(f'timeout of ${timeout_in_ms}ms has exceeded on process ${uid} when waiting for task to be resolve.')

0 commit comments

Comments
 (0)