diff --git a/Sources/MeiliSearch/Client.swift b/Sources/MeiliSearch/Client.swift index cc4cef35..9edb07a4 100755 --- a/Sources/MeiliSearch/Client.swift +++ b/Sources/MeiliSearch/Client.swift @@ -201,8 +201,9 @@ public struct MeiliSearch { If the request was sucessful or `Error` if a failure occured. */ public func getKeys( + params: KeysQuery? = nil, _ completion: @escaping (Result, Swift.Error>) -> Void) { - self.keys.getAll(completion) + self.keys.getAll(params: params, completion) } /** @@ -244,7 +245,7 @@ public struct MeiliSearch { */ public func updateKey( key: String, - keyParams: KeyParams, + keyParams: KeyUpdateParams, _ completion: @escaping (Result) -> Void) { self.keys.update( key: key, diff --git a/Sources/MeiliSearch/Keys.swift b/Sources/MeiliSearch/Keys.swift index b8a36ff8..e334e870 100644 --- a/Sources/MeiliSearch/Keys.swift +++ b/Sources/MeiliSearch/Keys.swift @@ -32,8 +32,8 @@ struct Keys { } } - func getAll(_ completion: @escaping (Result, Swift.Error>) -> Void) { - self.request.get(api: "/keys") { result in + func getAll(params: KeysQuery?, _ completion: @escaping (Result, Swift.Error>) -> Void) { + self.request.get(api: "/keys", param: params?.toQuery()) { result in switch result { case .success(let data): guard let data: Data = data else { @@ -81,7 +81,7 @@ struct Keys { public func update( key: String, - keyParams: KeyParams, + keyParams: KeyUpdateParams, _ completion: @escaping (Result) -> Void) { let data: Data do { @@ -91,6 +91,7 @@ struct Keys { completion(.failure(MeiliSearch.Error.invalidJSON)) return } + self.request.patch(api: "/keys/\(key)", data) { result in switch result { case .success(let result): diff --git a/Sources/MeiliSearch/Model/Key.swift b/Sources/MeiliSearch/Model/Key.swift index b32b1c83..6017e9a7 100644 --- a/Sources/MeiliSearch/Model/Key.swift +++ b/Sources/MeiliSearch/Model/Key.swift @@ -6,6 +6,8 @@ import Foundation public struct Key: Codable, Equatable { // MARK: Properties + public let uid: String + public let name: String? public let description: String public let key: String public let actions: [String] diff --git a/Sources/MeiliSearch/Model/KeyParams.swift b/Sources/MeiliSearch/Model/KeyParams.swift index 4dbff5bc..4424b55a 100644 --- a/Sources/MeiliSearch/Model/KeyParams.swift +++ b/Sources/MeiliSearch/Model/KeyParams.swift @@ -5,17 +5,42 @@ import Foundation */ public struct KeyParams: Codable, Equatable { public let description: String + public var name: String? + public var uid: String? public let actions: [String] public let indexes: [String] public let expiresAt: String? public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) + if uid != nil { + try container.encode(uid, forKey: .uid) + } + + if name != nil { + try container.encode(name, forKey: .name) + } + try container.encode(description, forKey: .description) try container.encode(actions, forKey: .actions) try container.encode(indexes, forKey: .indexes) try container.encode(expiresAt, forKey: .expiresAt) } +} - // MARK: Properties +public struct KeyUpdateParams: Codable, Equatable { + public var description: String? + public var name: String? + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + if description != nil { + try container.encode(description, forKey: .description) + } + + if name != nil { + try container.encode(name, forKey: .name) + } + } } diff --git a/Sources/MeiliSearch/QueryParameters/KeysQuery.swift b/Sources/MeiliSearch/QueryParameters/KeysQuery.swift new file mode 100644 index 00000000..d651c65b --- /dev/null +++ b/Sources/MeiliSearch/QueryParameters/KeysQuery.swift @@ -0,0 +1,18 @@ +import Foundation + +public class KeysQuery: Queryable { + private var limit: Int? + private var offset: Int? + + init(limit: Int? = nil, offset: Int? = nil) { + self.offset = offset + self.limit = limit + } + + internal func buildQuery() -> [String: Codable?] { + [ + "limit": limit, + "offset": offset + ] + } +} diff --git a/Tests/MeiliSearchIntegrationTests/KeysTests.swift b/Tests/MeiliSearchIntegrationTests/KeysTests.swift index 711bfc01..4eb2ef50 100644 --- a/Tests/MeiliSearchIntegrationTests/KeysTests.swift +++ b/Tests/MeiliSearchIntegrationTests/KeysTests.swift @@ -121,21 +121,18 @@ class KeysTests: XCTestCase { func testUpdateKey() { let keyExpectation = XCTestExpectation(description: "Update a key") - let keyParams = KeyParams(description: "Custom", actions: ["*"], indexes: ["*"], expiresAt: nil) + let keyParams = KeyParams(description: "Custom", name: "old name", actions: ["*"], indexes: ["index"], expiresAt: nil) self.client.createKey(keyParams) { result in switch result { case .success(let key): - let formatter = DateFormatter() - formatter.dateFormat = "yyyy-MM-dd" - let newDate = formatter.string(from: Date.distantFuture) - let keyParams = KeyParams(description: "Custom", actions: ["*"], indexes: ["*"], expiresAt: newDate) - self.client.updateKey(key: key.key, keyParams: keyParams) { result in + let updateParams = KeyUpdateParams(description: "new name") + self.client.updateKey(key: key.key, keyParams: updateParams) { result in switch result { case .success(let key): - XCTAssertEqual(key.description, keyParams.description) - XCTAssertEqual(key.actions, keyParams.actions) - XCTAssertEqual(key.indexes, keyParams.indexes) - XCTAssertNotNil(key.expiresAt) + XCTAssertEqual(key.description, "new name") + XCTAssertEqual(key.name, "old name") + XCTAssertEqual(key.indexes, ["index"]) + XCTAssertNil(key.expiresAt) keyExpectation.fulfill() case .failure(let error): dump(error) diff --git a/Tests/MeiliSearchUnitTests/KeysTests.swift b/Tests/MeiliSearchUnitTests/KeysTests.swift new file mode 100644 index 00000000..9db15635 --- /dev/null +++ b/Tests/MeiliSearchUnitTests/KeysTests.swift @@ -0,0 +1,53 @@ +@testable import MeiliSearch +import XCTest + +// swiftlint:disable force_try +class KeysTests: XCTestCase { + private var client: MeiliSearch! + private var index: Indexes! + private let uid: String = "movies_test" + private let session = MockURLSession() + + override func setUp() { + super.setUp() + + client = try! MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey", session: session) + index = client.index(self.uid) + } + + func testGetKeysWithParameters() { + let jsonString = """ + { + "results": [], + "offset": 10, + "limit": 2, + "total": 0 + } + """ + + // Prepare the mock server + session.pushData(jsonString) + + // Start the test with the mocked server + let expectation = XCTestExpectation(description: "Get keys with parameters") + + self.client.getKeys(params: KeysQuery(limit: 2, offset: 10)) { result in + switch result { + case .success: + let requestQuery = self.session.nextDataTask.request?.url?.query + + XCTAssertEqual(requestQuery, "limit=2&offset=10") + + expectation.fulfill() + case .failure(let error): + dump(error) + XCTFail("Failed to get all Indexes") + expectation.fulfill() + } + } + + self.wait(for: [expectation], timeout: TESTS_TIME_OUT) + } +} +// swiftlint:enable force_unwrapping +// swiftlint:enable force_try diff --git a/Tests/MeiliSearchUnitTests/QueryParameters/KeysQueryTests.swift b/Tests/MeiliSearchUnitTests/QueryParameters/KeysQueryTests.swift new file mode 100644 index 00000000..48ac2051 --- /dev/null +++ b/Tests/MeiliSearchUnitTests/QueryParameters/KeysQueryTests.swift @@ -0,0 +1,20 @@ +@testable import MeiliSearch + +import XCTest + +class KeysQueryTests: XCTestCase { + func testRenderedQuery() { + let data: [[String: KeysQuery]] = [ + ["?limit=2": KeysQuery(limit: 2)], + ["?limit=2&offset=99": KeysQuery(limit: 2, offset: 99)], + ["?limit=2": KeysQuery(limit: 2, offset: nil)], + ["?offset=2": KeysQuery(offset: 2)], + ["?limit=10&offset=0": KeysQuery(limit: 10, offset: 0)], + ["": KeysQuery()] + ] + + data.forEach { dict in + XCTAssertEqual(dict.first?.value.toQuery(), dict.first?.key) + } + } +}