Skip to content

Commit f5a5209

Browse files
bors[bot]bidoubiwa
andauthored
Merge #248
248: Update api to task api r=bidoubiwa a=bidoubiwa ### BREAKING: - Task structure introduced - all actions on Indexes except `get` now return a Task: - `updateIndex` - `createIndex` - `deleteIndex` - `waitForPendingUpdate` removed `waitForTask` introduced as replacement - `getStatusUpdate` and `getAllStatusUpdates` removed. - `getTask` and `getTasks` introduced on `Indexes` and `Client` - All routes returning an `Update` return now a `Task` ### Small changes - Replaced all `print(error)` with `dump(error)` - Used utils function in test to avoid code duplication - Added and removed newlines for readability - replaced `waitForTask` utils function with `client.waitForTask` in tests Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: cvermand <[email protected]>
2 parents 64ab72e + c11d24e commit f5a5209

34 files changed

+3381
-3054
lines changed

.code-samples.meilisearch.yaml

Lines changed: 909 additions & 881 deletions
Large diffs are not rendered by default.

.github/workflows/pre-release-tests.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,3 @@ jobs:
4141
./meilisearch --master-key=masterKey --no-analytics true &
4242
- name: Run tests
4343
run: swift test
44-
45-
linter:
46-
name: linter-check
47-
runs-on: macos-latest
48-
env:
49-
DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer
50-
steps:
51-
- uses: actions/checkout@v2
52-
- name: Run SwiftLint
53-
run: sh ./Scripts/swiftlint.sh

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ To do a simply search using the client, you can create a Swift script like this:
128128
primaryKey: nil
129129
) { result in
130130
switch result {
131-
case .success(let update):
132-
print(update) // => Update(updateId: 0)
131+
case .success(let task):
132+
print(task) // => Task(uid: 0, status: "enqueued", ...)
133133
case .failure(let error):
134134
print(error.localizedDescription)
135135
}
@@ -138,7 +138,7 @@ To do a simply search using the client, you can create a Swift script like this:
138138
semaphore.wait()
139139
```
140140

141-
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).
141+
With the `uid` of the task, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/learn/advanced/asynchronous_operations.html#task-status).
142142

143143
#### Basic Search <!-- omit in toc -->
144144

Sources/MeiliSearch/Client.swift

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct MeiliSearch {
2020
private let stats: Stats
2121
private let system: System
2222
private let dumps: Dumps
23+
private let tasks: Tasks
2324

2425
// MARK: Initializers
2526

@@ -41,6 +42,7 @@ public struct MeiliSearch {
4142
self.stats = Stats(self.request)
4243
self.system = System(self.request)
4344
self.dumps = Dumps(self.request)
45+
self.tasks = Tasks(self.request)
4446
}
4547

4648
// MARK: Index
@@ -65,7 +67,7 @@ public struct MeiliSearch {
6567
public func createIndex(
6668
uid: String,
6769
primaryKey: String? = nil,
68-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
70+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
6971
Indexes.create(uid: uid, primaryKey: primaryKey, config: self.config, completion)
7072
}
7173

@@ -79,13 +81,13 @@ public struct MeiliSearch {
7981
value. If the request was sucessful or `Error` if a failure occured.
8082
*/
8183

82-
// DONE
83-
public func getOrCreateIndex(
84-
uid: String,
85-
primaryKey: String? = nil,
86-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
87-
Indexes.getOrCreate(uid: uid, primaryKey: primaryKey, config: self.config, completion)
88-
}
84+
// TODO: remove in another PR
85+
// public func getOrCreateIndex(
86+
// uid: String,
87+
// primaryKey: String? = nil,
88+
// _ completion: @escaping (Result<Index, Swift.Error>) -> Void) {
89+
// Indexes.getOrCreate(uid: uid, primaryKey: primaryKey, config: self.config, completion)
90+
// }
8991

9092
/**
9193
Get an index.
@@ -97,7 +99,7 @@ public struct MeiliSearch {
9799
*/
98100
public func getIndex(
99101
_ uid: String,
100-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
102+
_ completion: @escaping (Result<Index, Swift.Error>) -> Void) {
101103
self.index(uid).get(completion)
102104
}
103105

@@ -109,7 +111,7 @@ public struct MeiliSearch {
109111
value. If the request was sucessful or `Error` if a failure occured.
110112
*/
111113
public func getIndexes(
112-
_ completion: @escaping (Result<[Indexes], Swift.Error>) -> Void) {
114+
_ completion: @escaping (Result<[Index], Swift.Error>) -> Void) {
113115
Indexes.getAll(config: self.config, completion)
114116
}
115117

@@ -125,7 +127,7 @@ public struct MeiliSearch {
125127
public func updateIndex(
126128
uid: String,
127129
primaryKey: String,
128-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
130+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
129131
self.index(uid).update(primaryKey: primaryKey, completion)
130132
}
131133

@@ -139,10 +141,74 @@ public struct MeiliSearch {
139141
*/
140142
public func deleteIndex(
141143
_ uid: String,
142-
_ completion: @escaping (Result<(), Swift.Error>) -> Void) {
144+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
143145
self.index(uid).delete(completion)
144146
}
145147

148+
// MARK: WAIT FOR TASK
149+
150+
/**
151+
Wait for a task to be succesfull or failed.
152+
153+
Using a task returned by an asynchronous route of MeiliSearch, wait for completion.
154+
155+
- parameter: taskId: The id of the task.
156+
- parameter: options Optionnal configuration for timeout and interval
157+
- parameter: completion: The completion closure used to notify when the server
158+
**/
159+
public func waitForTask(
160+
taskUid: Int,
161+
options: WaitOptions? = nil,
162+
_ completion: @escaping (Result<Task, Swift.Error>
163+
) -> Void) {
164+
self.tasks.waitForTask(taskUid: taskUid, options: options, completion)
165+
}
166+
167+
/**
168+
Wait for a task to be succeeded or failed.
169+
170+
Using a task returned by an asynchronous route of MeiliSearch, wait for completion.
171+
172+
- parameter task: The task.
173+
- parameter: options: Optionnal configuration for timeout and interval
174+
- parameter completion: The completion closure used to notify when the server
175+
**/
176+
public func waitForTask(
177+
task: Task,
178+
options: WaitOptions? = nil,
179+
_ completion: @escaping (Result<Task, Swift.Error>
180+
) -> Void) {
181+
self.tasks.waitForTask(task: task, options: options, completion)
182+
}
183+
184+
// MARK: Tasks
185+
186+
/**
187+
Get the information of a task.
188+
189+
- parameter taskUid: The task identifier.
190+
- parameter completion: The completion closure used to notify when the server
191+
completes the query request, it returns a `Result` object that contains `Key` value.
192+
If the request was sucessful or `Error` if a failure occured.
193+
*/
194+
public func getTask(
195+
taskUid: Int,
196+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
197+
self.tasks.get(taskUid: taskUid, completion)
198+
}
199+
200+
/**
201+
Get all tasks.
202+
203+
- parameter completion: The completion closure used to notify when the server
204+
completes the query request, it returns a `Result` object that contains `Key` value.
205+
If the request was sucessful or `Error` if a failure occured.
206+
*/
207+
public func getTasks(
208+
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
209+
self.tasks.getAll(completion)
210+
}
211+
146212
// MARK: Keys
147213

148214
/**

Sources/MeiliSearch/Constants.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ struct Constants {
1212
encoder.dateEncodingStrategy = JSONEncoder.DateEncodingStrategy.formatted(Formatter.iso8601)
1313
return encoder
1414
}()
15+
16+
static func resultDecoder<T: Decodable>(data: Data?) throws -> Result<T, Swift.Error> {
17+
guard let data: Data = data else {
18+
return .failure(MeiliSearch.Error.dataNotFound)
19+
}
20+
do {
21+
let task: T = try self.customJSONDecoder.decode(
22+
T.self,
23+
from: data)
24+
return .success(task)
25+
} catch {
26+
return .failure(error)
27+
}
28+
}
29+
1530
}

Sources/MeiliSearch/Documents.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Documents {
1919
_ completion: @escaping (Result<T, Swift.Error>) -> Void)
2020
where T: Codable, T: Equatable {
2121
let query: String = "/indexes/\(uid)/documents/\(identifier)"
22-
request.get(api: query) { result in
22+
self.request.get(api: query) { result in
2323
switch result {
2424
case .success(let data):
2525
guard let data: Data = data else {
@@ -69,7 +69,8 @@ struct Documents {
6969
_ uid: String,
7070
_ document: Data,
7171
_ primaryKey: String? = nil,
72-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
72+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
73+
7374
var query: String = "/indexes/\(uid)/documents"
7475
if let primaryKey: String = primaryKey {
7576
query += "?primaryKey=\(primaryKey)"
@@ -90,7 +91,7 @@ struct Documents {
9091
_ documents: [T],
9192
_ encoder: JSONEncoder? = nil,
9293
_ primaryKey: String? = nil,
93-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) where T: Encodable {
94+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) where T: Encodable {
9495
var query: String = "/indexes/\(uid)/documents"
9596
if let primaryKey: String = primaryKey {
9697
query += "?primaryKey=\(primaryKey)"
@@ -119,7 +120,8 @@ struct Documents {
119120
_ uid: String,
120121
_ document: Data,
121122
_ primaryKey: String? = nil,
122-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
123+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
124+
123125
var query: String = "/indexes/\(uid)/documents"
124126
if let primaryKey: String = primaryKey {
125127
query += "?primaryKey=\(primaryKey)"
@@ -140,7 +142,8 @@ struct Documents {
140142
_ documents: [T],
141143
_ encoder: JSONEncoder? = nil,
142144
_ primaryKey: String? = nil,
143-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) where T: Encodable {
145+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) where T: Encodable {
146+
144147
var query: String = "/indexes/\(uid)/documents"
145148
if let primaryKey: String = primaryKey {
146149
query += "?primaryKey=\(primaryKey)"
@@ -170,18 +173,17 @@ struct Documents {
170173
func delete(
171174
_ uid: String,
172175
_ identifier: String,
173-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
176+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
177+
174178
self.request.delete(api: "/indexes/\(uid)/documents/\(identifier)") { result in
175179
switch result {
176-
case .success(let result):
177-
178-
guard let result: Data = result else {
180+
case .success(let data):
181+
guard let data: Data = data else {
179182
completion(.failure(MeiliSearch.Error.dataNotFound))
180183
return
181184
}
182185

183-
Documents.decodeJSON(result, completion: completion)
184-
186+
Documents.decodeJSON(data, completion: completion)
185187
case .failure(let error):
186188
completion(.failure(error))
187189
}
@@ -190,7 +192,8 @@ struct Documents {
190192

191193
func deleteAll(
192194
_ uid: String,
193-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
195+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
196+
194197
self.request.delete(api: "/indexes/\(uid)/documents") { result in
195198
switch result {
196199
case .success(let data):
@@ -211,7 +214,8 @@ struct Documents {
211214
func deleteBatch(
212215
_ uid: String,
213216
_ documentsIdentifiers: [Int],
214-
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
217+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
218+
215219
let data: Data
216220

217221
do {

0 commit comments

Comments
 (0)