Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Results<Key>, Swift.Error>) -> Void) {
self.keys.getAll(completion)
self.keys.getAll(params: params, completion)
}

/**
Expand Down Expand Up @@ -244,7 +245,7 @@ public struct MeiliSearch {
*/
public func updateKey(
key: String,
keyParams: KeyParams,
keyParams: KeyUpdateParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
self.keys.update(
key: key,
Expand Down
7 changes: 4 additions & 3 deletions Sources/MeiliSearch/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ struct Keys {
}
}

func getAll(_ completion: @escaping (Result<Results<Key>, Swift.Error>) -> Void) {
self.request.get(api: "/keys") { result in
func getAll(params: KeysQuery?, _ completion: @escaping (Result<Results<Key>, 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 {
Expand Down Expand Up @@ -81,7 +81,7 @@ struct Keys {

public func update(
key: String,
keyParams: KeyParams,
keyParams: KeyUpdateParams,
_ completion: @escaping (Result<Key, Swift.Error>) -> Void) {
let data: Data
do {
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/Model/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 26 additions & 1 deletion Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,42 @@ import Foundation
*/
public struct KeyParams: Codable, Equatable {
public let description: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think description is optional as well

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)
}
}
}
18 changes: 18 additions & 0 deletions Sources/MeiliSearch/QueryParameters/KeysQuery.swift
Original file line number Diff line number Diff line change
@@ -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
]
}
}
17 changes: 7 additions & 10 deletions Tests/MeiliSearchIntegrationTests/KeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions Tests/MeiliSearchUnitTests/KeysTests.swift
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions Tests/MeiliSearchUnitTests/QueryParameters/KeysQueryTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}