From e5fafcea7fed54e73888e9e3221c4e28f2e5aebb Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Tue, 8 Feb 2022 11:33:59 +0100 Subject: [PATCH 1/5] Remove code duplication --- Sources/MeiliSearch/Constants.swift | 15 ++++ Sources/MeiliSearch/Tasks.swift | 115 +++++++++------------------- 2 files changed, 50 insertions(+), 80 deletions(-) diff --git a/Sources/MeiliSearch/Constants.swift b/Sources/MeiliSearch/Constants.swift index 48162d34..296a699b 100644 --- a/Sources/MeiliSearch/Constants.swift +++ b/Sources/MeiliSearch/Constants.swift @@ -12,4 +12,19 @@ struct Constants { encoder.dateEncodingStrategy = JSONEncoder.DateEncodingStrategy.formatted(Formatter.iso8601) return encoder }() + + static func resultDecoder(data: Data?) throws -> Result { + 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) + } + } + } diff --git a/Sources/MeiliSearch/Tasks.swift b/Sources/MeiliSearch/Tasks.swift index 818628a0..ef67c036 100644 --- a/Sources/MeiliSearch/Tasks.swift +++ b/Sources/MeiliSearch/Tasks.swift @@ -18,27 +18,7 @@ struct Tasks { func get( taskUid: Int, _ completion: @escaping (Result) -> 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 @@ -46,64 +26,49 @@ struct Tasks { uid: String, taskUid: Int, _ completion: @escaping (Result) -> 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) -> Void) { + self.request.get(api: url) { result in + switch result { + case .success(let data): + do { + let task: Result = 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, 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 = try Constants.customJSONDecoder.decode(Results.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, 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, 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 = try Constants.customJSONDecoder.decode(Results.self, from: data) - completion(.success(result)) + let task: Result, Swift.Error> = try Constants.resultDecoder(data: data) + completion(task) } catch { completion(.failure(error)) } @@ -136,36 +101,26 @@ 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) -> 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) -> 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)) From dd00e73116d72d85c4fb974ceb314e371cb529de Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Tue, 8 Feb 2022 11:41:26 +0100 Subject: [PATCH 2/5] Fix linting --- Sources/MeiliSearch/Constants.swift | 2 +- Sources/MeiliSearch/Tasks.swift | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/MeiliSearch/Constants.swift b/Sources/MeiliSearch/Constants.swift index 296a699b..c4851d38 100644 --- a/Sources/MeiliSearch/Constants.swift +++ b/Sources/MeiliSearch/Constants.swift @@ -13,7 +13,7 @@ struct Constants { return encoder }() - static func resultDecoder(data: Data?) throws -> Result { + static func resultDecoder(data: Data?) throws -> Result { guard let data: Data = data else { return .failure(MeiliSearch.Error.dataNotFound) } diff --git a/Sources/MeiliSearch/Tasks.swift b/Sources/MeiliSearch/Tasks.swift index ef67c036..f5a67026 100644 --- a/Sources/MeiliSearch/Tasks.swift +++ b/Sources/MeiliSearch/Tasks.swift @@ -101,8 +101,6 @@ struct Tasks { } } - - // wait for task using task structure func waitForTask( task: Task, From 214d102e1bbfcc195b3acbec44129545d787046f Mon Sep 17 00:00:00 2001 From: Charlotte Vermandel Date: Tue, 8 Feb 2022 11:59:01 +0100 Subject: [PATCH 3/5] Change task tests name --- Tests/MeiliSearchIntegrationTests/TaskTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/MeiliSearchIntegrationTests/TaskTests.swift b/Tests/MeiliSearchIntegrationTests/TaskTests.swift index 85590f79..429279b9 100644 --- a/Tests/MeiliSearchIntegrationTests/TaskTests.swift +++ b/Tests/MeiliSearchIntegrationTests/TaskTests.swift @@ -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! From ae7ad35829ebaedebc0dd050c05703207d55c819 Mon Sep 17 00:00:00 2001 From: cvermand <33010418+bidoubiwa@users.noreply.github.com> Date: Tue, 8 Feb 2022 15:09:34 +0100 Subject: [PATCH 4/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amélie --- Sources/MeiliSearch/Indexes.swift | 9 ++++----- Sources/MeiliSearch/Model/Task.swift | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Sources/MeiliSearch/Indexes.swift b/Sources/MeiliSearch/Indexes.swift index d07f6576..b06e2a02 100755 --- a/Sources/MeiliSearch/Indexes.swift +++ b/Sources/MeiliSearch/Indexes.swift @@ -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) -> Void) { self.request.get(api: "/indexes/\(self.uid)") { result in @@ -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 @@ -253,7 +253,6 @@ public struct Indexes { } catch { completion(.failure(error)) } - // } case .failure(let error): completion(.failure(error)) } diff --git a/Sources/MeiliSearch/Model/Task.swift b/Sources/MeiliSearch/Model/Task.swift index 5742bf30..80212bcc 100644 --- a/Sources/MeiliSearch/Model/Task.swift +++ b/Sources/MeiliSearch/Model/Task.swift @@ -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 @@ -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 From 3844ab866453957a7001d23604961fa4c6902d43 Mon Sep 17 00:00:00 2001 From: cvermand <33010418+bidoubiwa@users.noreply.github.com> Date: Tue, 8 Feb 2022 15:13:40 +0100 Subject: [PATCH 5/5] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amélie --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d622cdc3..f036116c 100644 --- a/README.md +++ b/README.md @@ -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