Skip to content

Commit

Permalink
Merge branch 'update_api_to_task_api' of github.com:meilisearch/meili…
Browse files Browse the repository at this point in the history
…search-swift into update_keys_api
  • Loading branch information
bidoubiwa committed Feb 8, 2022
2 parents 3245b68 + 3844ab8 commit 5f50a3a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 96 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ To do a simply search using the client, you can create a Swift script like this:
semaphore.wait()
```

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/reference/api/updates.html#get-an-update-status).
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).

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

Expand Down
15 changes: 15 additions & 0 deletions Sources/MeiliSearch/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@ struct Constants {
encoder.dateEncodingStrategy = JSONEncoder.DateEncodingStrategy.formatted(Formatter.iso8601)
return encoder
}()

static func resultDecoder<T: Decodable>(data: Data?) throws -> Result<T, Swift.Error> {
guard let data: Data = data else {
return .failure(MeiliSearch.Error.dataNotFound)
}
do {
let task: T = try Constants.customJSONDecoder.decode(
T.self,
from: data)
return .success(task)
} catch {
return .failure(error)
}
}

}
8 changes: 4 additions & 4 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public struct Indexes {
Get the index.

- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Index`
value. If the request was sucessful or `Error` if a failure occured.
completes the query request. It returns a `Result` object that contains `Index`
value if the request was successful or `Error` if a failure occurred.
*/
public func get(_ completion: @escaping (Result<Index, Swift.Error>) -> Void) {
self.request.get(api: "/indexes/\(self.uid)") { result in
Expand Down Expand Up @@ -98,8 +98,8 @@ public struct Indexes {
List all indexes.

- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `[Index]`
value. If the request was sucessful or `Error` if a failure occured.
completes the query request. It returns a `Result` object that contains `[Index]`
value if the request was successful or `Error` if a failure occurred.
*/
public static func getAll(config: Config, _ completion: @escaping (Result<[Index], Swift.Error>) -> Void) {
Request(config).get(api: "/indexes") { result in
Expand Down
20 changes: 10 additions & 10 deletions Sources/MeiliSearch/Model/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ public struct Task: Codable, Equatable {

// MARK: Properties

/// Unique ID for the current `Update`.
/// Unique ID for the current `Task`.
public let uid: Int

/// Unique ID for the current `Update`.
/// Unique ID for the current `Task`.
public let indexUid: String

/// Returns if the update has been sucessful or not.
/// Returns if the task has been successful or not.
public let status: Task.Status

/// Type of update.
/// Type of the task.
public let type: String

/// Type of update.
/// Details of the task.
public let details: Details?

/// Duration of the update process.
/// Duration of the task process.
public let duration: String?

/// Date when the update has been enqueued.
/// Date when the task has been enqueued.
public let enqueuedAt: String

/// Date when the update has been processed.
/// Date when the task has been processed.
// TODO: should this become a Date type?
public let processedAt: String?

/// Type of `Update`.
/// Type of `Task`.
public struct Details: Codable, Equatable {

// MARK: Properties
Expand Down Expand Up @@ -68,7 +68,7 @@ public struct Task: Codable, Equatable {
// Stop words on settings actions
public let stopWords: [String]?

// Stop words on settings actions
// Synonyms on settings actions
public let synonyms: [String: [String]]?

// Distinct attribute on settings actions
Expand Down
113 changes: 33 additions & 80 deletions Sources/MeiliSearch/Tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,92 +18,57 @@ struct Tasks {
func get(
taskUid: Int,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
self.request.get(api: "/tasks/\(taskUid)") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(
Task.self,
from: data)
completion(.success(task))
} catch {
dump(error)
completion(.failure(error))
}

case .failure(let error):
completion(.failure(error))
}
}
get(url: "/tasks/\(taskUid)", completion)
}

// Get on index
func get(
uid: String,
taskUid: Int,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
self.request.get(api: "/indexes/\(uid)/tasks/\(taskUid)") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let task: Task = try Constants.customJSONDecoder.decode(
Task.self,
from: data)
completion(.success(task))
} catch {
get(url: "/indexes/\(uid)/tasks/\(taskUid)", completion)
}

func get (
url: String,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
self.request.get(api: url) { result in
switch result {
case .success(let data):
do {
let task: Result<Task, Swift.Error> = try Constants.resultDecoder(data: data)
completion(task)
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}

case .failure(let error):
completion(.failure(error))
}
}
}

// get all on client
func getAll(
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.request.get(api: "/tasks") { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let result: Results<Task> = try Constants.customJSONDecoder.decode(Results<Task>.self, from: data)
completion(.success(result))
} catch {
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
getAll(url: "/tasks", completion)
}

// get all on index
func getAll(
uid: String,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.request.get(api: "/indexes/\(uid)/tasks") { result in
getAll(url: "/indexes/\(uid)/tasks", completion)
}

func getAll(
url: String,
_ completion: @escaping (Result<Results<Task>, Swift.Error>) -> Void) {
self.request.get(api: url) { result in
switch result {
case .success(let data):
guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}
do {
let result: Results<Task> = try Constants.customJSONDecoder.decode(Results<Task>.self, from: data)
completion(.success(result))
let task: Result<Results<Task>, Swift.Error> = try Constants.resultDecoder(data: data)
completion(task)
} catch {
completion(.failure(error))
}
Expand Down Expand Up @@ -136,36 +101,24 @@ struct Tasks {
}
}

// wait for task using taskUid
// wait for task using task structure
func waitForTask(
taskUid: Int,
task: Task,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
do {
let currentDate = Date()
let waitOptions: WaitOptions = options ?? WaitOptions()

self.checkStatus(taskUid, waitOptions, currentDate) { result in
switch result {
case .success(let status):
completion(.success(status))
case .failure(let error):
completion(.failure(error))
}
}
}
waitForTask(taskUid: task.uid, options: options, completion)
}

// wait for task using task structure
// wait for task using taskUid
func waitForTask(
task: Task,
taskUid: Int,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
do {
let currentDate = Date()
let waitOptions: WaitOptions = options ?? WaitOptions()

self.checkStatus(task.uid, waitOptions, currentDate) { result in
self.checkStatus(taskUid, waitOptions, currentDate) { result in
switch result {
case .success(let status):
completion(.success(status))
Expand Down
2 changes: 1 addition & 1 deletion Tests/MeiliSearchIntegrationTests/TaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private struct Movie: Codable, Equatable {
}
}

class UpdatesTests: XCTestCase {
class TasksTests: XCTestCase {
private var client: MeiliSearch!
private var index: Indexes!
private var session: URLSessionProtocol!
Expand Down

0 comments on commit 5f50a3a

Please sign in to comment.