Skip to content

Commit 09e4d0e

Browse files
authored
Merge branch 'main' into bidoubiwa-patch-1
2 parents ccf105c + 0f0eac2 commit 09e4d0e

39 files changed

+3042
-2423
lines changed

.code-samples.meilisearch.yaml

Lines changed: 277 additions & 148 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: 4 additions & 4 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

@@ -190,7 +190,7 @@ Since Meilisearch is typo-tolerant, the movie `philadelphia` is a valid search r
190190

191191
## 🤖 Compatibility with Meilisearch
192192

193-
This package only guarantees the compatibility with the [version v0.24.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.24.0).
193+
This package only guarantees the compatibility with the [version v0.25.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.25.0).
194194

195195
## 💡 Learn More
196196

Sources/MeiliSearch/Client.swift

Lines changed: 139 additions & 28 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,28 +67,10 @@ 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

72-
/**
73-
Get or create an index.
74-
75-
- parameter uid: The unique identifier for the `Index` to be created.
76-
- parameter primaryKey: the unique field of a document.
77-
- parameter completion: The completion closure used to notify when the server
78-
completes the write request, it returns a `Result` object that contains `Index`
79-
value. If the request was sucessful or `Error` if a failure occured.
80-
*/
81-
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-
}
89-
9074
/**
9175
Get an index.
9276

@@ -97,7 +81,7 @@ public struct MeiliSearch {
9781
*/
9882
public func getIndex(
9983
_ uid: String,
100-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
84+
_ completion: @escaping (Result<Index, Swift.Error>) -> Void) {
10185
self.index(uid).get(completion)
10286
}
10387

@@ -109,7 +93,7 @@ public struct MeiliSearch {
10993
value. If the request was sucessful or `Error` if a failure occured.
11094
*/
11195
public func getIndexes(
112-
_ completion: @escaping (Result<[Indexes], Swift.Error>) -> Void) {
96+
_ completion: @escaping (Result<[Index], Swift.Error>) -> Void) {
11397
Indexes.getAll(config: self.config, completion)
11498
}
11599

@@ -125,7 +109,7 @@ public struct MeiliSearch {
125109
public func updateIndex(
126110
uid: String,
127111
primaryKey: String,
128-
_ completion: @escaping (Result<Indexes, Swift.Error>) -> Void) {
112+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
129113
self.index(uid).update(primaryKey: primaryKey, completion)
130114
}
131115

@@ -139,24 +123,151 @@ public struct MeiliSearch {
139123
*/
140124
public func deleteIndex(
141125
_ uid: String,
142-
_ completion: @escaping (Result<(), Swift.Error>) -> Void) {
126+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
143127
self.index(uid).delete(completion)
144128
}
145129

130+
// MARK: WAIT FOR TASK
131+
132+
/**
133+
Wait for a task to be succesfull or failed.
134+
135+
Using a task returned by an asynchronous route of MeiliSearch, wait for completion.
136+
137+
- parameter: taskId: The id of the task.
138+
- parameter: options Optionnal configuration for timeout and interval
139+
- parameter: completion: The completion closure used to notify when the server
140+
**/
141+
public func waitForTask(
142+
taskUid: Int,
143+
options: WaitOptions? = nil,
144+
_ completion: @escaping (Result<Task, Swift.Error>
145+
) -> Void) {
146+
self.tasks.waitForTask(taskUid: taskUid, options: options, completion)
147+
}
148+
149+
/**
150+
Wait for a task to be succeeded or failed.
151+
152+
Using a task returned by an asynchronous route of MeiliSearch, wait for completion.
153+
154+
- parameter task: The task.
155+
- parameter: options: Optionnal configuration for timeout and interval
156+
- parameter completion: The completion closure used to notify when the server
157+
**/
158+
public func waitForTask(
159+
task: Task,
160+
options: WaitOptions? = nil,
161+
_ completion: @escaping (Result<Task, Swift.Error>
162+
) -> Void) {
163+
self.tasks.waitForTask(task: task, options: options, completion)
164+
}
165+
166+
// MARK: Tasks
167+
168+
/**
169+
Get the information of a task.
170+
171+
- parameter taskUid: The task identifier.
172+
- parameter completion: The completion closure used to notify when the server
173+
completes the query request, it returns a `Result` object that contains `Key` value.
174+
If the request was sucessful or `Error` if a failure occured.
175+
*/
176+
public func getTask(
177+
taskUid: Int,
178+
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
179+
self.tasks.get(taskUid: taskUid, completion)
180+
}
181+
182+
/**
183+
Get all tasks.
184+
185+
- parameter completion: The completion closure used to notify when the server
186+
completes the query request, it returns a `Result` object that contains `Key` value.
187+
If the request was sucessful or `Error` if a failure occured.
188+
*/
189+
public func getTasks(
190+
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
191+
self.tasks.getAll(completion)
192+
}
193+
146194
// MARK: Keys
147195

148196
/**
149-
Protected instances of Meilisearch require API keys to perform certain actions. Only the master key has
150-
the right to access the list of all API keys.
197+
Get all keys.
198+
199+
- parameter completion: The completion closure used to notify when the server
200+
completes the query request, it returns a `Result` object that contains `Key` value.
201+
If the request was sucessful or `Error` if a failure occured.
202+
*/
203+
public func getKeys(
204+
_ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
205+
self.keys.getAll(completion)
206+
}
207+
208+
/**
209+
Get one key's information using the key value.
151210

152-
- parameter masterKey: Master key to access the `keys` function.
211+
- parameter key: The key value.
153212
- parameter completion: The completion closure used to notify when the server
154213
completes the query request, it returns a `Result` object that contains `Key` value.
155214
If the request was sucessful or `Error` if a failure occured.
156215
*/
157-
public func keys(
216+
public func getKey(
217+
key: String,
218+
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
219+
self.keys.get(key: key, completion)
220+
}
221+
222+
/**
223+
Create an API key.
224+
225+
- parameter keyParams: Parameters object required to create a key.
226+
- parameter completion: The completion closure used to notify when the server
227+
completes the query request, it returns a `Result` object that contains `Key` value.
228+
If the request was sucessful or `Error` if a failure occured.
229+
*/
230+
public func createKey(
231+
_ keyParams: KeyParams,
158232
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
159-
self.keys.get(completion)
233+
self.keys.create(keyParams, completion)
234+
}
235+
236+
/**
237+
Update an API key.
238+
239+
- parameter key: The key value.
240+
- parameter keyParams: Parameters object required to update a key.
241+
- parameter completion: The completion closure used to notify when the server
242+
completes the query request, it returns a `Result` object that contains `Key` value.
243+
If the request was sucessful or `Error` if a failure occured.
244+
*/
245+
public func updateKey(
246+
key: String,
247+
keyParams: KeyParams,
248+
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
249+
self.keys.update(
250+
key: key,
251+
keyParams: keyParams,
252+
completion
253+
)
254+
}
255+
256+
/**
257+
Delete an API key.
258+
259+
- parameter key: The key value.
260+
- parameter completion: The completion closure used to notify when the server
261+
completes the query request, it returns a `Result` object that contains `Key` value.
262+
If the request was sucessful or `Error` if a failure occured.
263+
*/
264+
public func deleteKey(
265+
key: String,
266+
_ completion: @escaping (Result<(), Swift.Error>) -> Void) {
267+
self.keys.delete(
268+
key: key,
269+
completion
270+
)
160271
}
161272

162273
// MARK: Stats

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)