|
3 | 3 | import com.meilisearch.sdk.exceptions.MeilisearchException; |
4 | 4 | import com.meilisearch.sdk.exceptions.MeilisearchTimeoutException; |
5 | 5 | import com.meilisearch.sdk.http.URLBuilder; |
| 6 | +import com.meilisearch.sdk.model.CancelTasksQuery; |
| 7 | +import com.meilisearch.sdk.model.DeleteTasksQuery; |
6 | 8 | import com.meilisearch.sdk.model.Task; |
| 9 | +import com.meilisearch.sdk.model.TaskInfo; |
7 | 10 | import com.meilisearch.sdk.model.TasksQuery; |
8 | 11 | import com.meilisearch.sdk.model.TasksResults; |
9 | 12 | import java.util.Date; |
@@ -88,18 +91,40 @@ TasksResults getTasks(String indexUid) throws MeilisearchException { |
88 | 91 | * @throws MeilisearchException if client request causes an error |
89 | 92 | */ |
90 | 93 | TasksResults getTasks(String indexUid, TasksQuery param) throws MeilisearchException { |
91 | | - String[] newIndexUid = new String[param.getIndexUid().length + 1]; |
92 | | - if (param != null && param.getIndexUid() != null) { |
93 | | - for (int i = 0; i < param.getIndexUid().length; i++) |
94 | | - newIndexUid[i] = param.getIndexUid()[i]; |
95 | | - newIndexUid[param.getIndexUid().length] = indexUid; |
96 | | - } |
| 94 | + param = addIndexUidToQuery(indexUid, param); |
97 | 95 |
|
98 | 96 | TasksResults result = |
99 | 97 | httpClient.get(tasksPath().addQuery(param.toQuery()).getURL(), TasksResults.class); |
100 | 98 | return result; |
101 | 99 | } |
102 | 100 |
|
| 101 | + /** |
| 102 | + * Delete tasks from the client |
| 103 | + * |
| 104 | + * @param param accept by the tasks route |
| 105 | + * @return Meilisearch API response as TaskInfo |
| 106 | + * @throws MeilisearchException if client request causes an error |
| 107 | + */ |
| 108 | + TaskInfo cancelTasks(CancelTasksQuery param) throws MeilisearchException { |
| 109 | + URLBuilder urlb = tasksPath().addSubroute("cancel"); |
| 110 | + TaskInfo result = |
| 111 | + httpClient.post(urlb.addQuery(param.toQuery()).getURL(), null, TaskInfo.class); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Delete tasks from the client |
| 117 | + * |
| 118 | + * @param param accept by the tasks route |
| 119 | + * @return Meilisearch API response as TaskInfo |
| 120 | + * @throws MeilisearchException if client request causes an error |
| 121 | + */ |
| 122 | + TaskInfo deleteTasks(DeleteTasksQuery param) throws MeilisearchException { |
| 123 | + TaskInfo result = |
| 124 | + httpClient.delete(tasksPath().addQuery(param.toQuery()).getURL(), TaskInfo.class); |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
103 | 128 | /** |
104 | 129 | * Waits for a task to be processed |
105 | 130 | * |
@@ -143,4 +168,20 @@ void waitForTask(int taskUid, int timeoutInMs, int intervalInMs) throws Meilisea |
143 | 168 | private URLBuilder tasksPath() { |
144 | 169 | return new URLBuilder("/tasks"); |
145 | 170 | } |
| 171 | + |
| 172 | + /** Add index uid to index uids list in task query */ |
| 173 | + TasksQuery addIndexUidToQuery(String indexUid, TasksQuery param) { |
| 174 | + if (param != null && param.getIndexUids() != null) { |
| 175 | + String[] newIndexUid = new String[param.getIndexUids().length + 1]; |
| 176 | + for (int i = 0; i < param.getIndexUids().length; i++) |
| 177 | + newIndexUid[i] = param.getIndexUids()[i]; |
| 178 | + newIndexUid[param.getIndexUids().length] = indexUid; |
| 179 | + param.setIndexUids(newIndexUid); |
| 180 | + } else if (param != null) { |
| 181 | + param.setIndexUids(new String[] {indexUid}); |
| 182 | + } else { |
| 183 | + param = new TasksQuery().setIndexUids(new String[] {indexUid}); |
| 184 | + } |
| 185 | + return param; |
| 186 | + } |
146 | 187 | } |
0 commit comments